AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Solved Integer division and modulo don't work correctly? (https://forums.alliedmods.net/showthread.php?t=299305)

PRoSToTeM@ 07-10-2017 17:51

Integer division and modulo don't work correctly?
 
Code:
server_print("5/3=%d 5%%3=%d", 5/3, 5%3); server_print("-5/3=%d -5%%3=%d", -5/3, -5%3); server_print("5/-3=%d 5%%-3=%d", 5/-3, 5%-3); server_print("-5/-3=%d -5%%-3=%d", -5/-3, -5%-3);
Code:

5/3=1 5%3=2
-5/3=-2 -5%3=1
5/-3=-2 5%-3=-1
-5/-3=1 -5%-3=-2

But in C++:
Code:
printf("5/3=%d 5%%3=%d\n", 5/3, 5%3); printf("-5/3=%d -5%%3=%d\n", -5/3, -5%3); printf("5/-3=%d 5%%-3=%d\n", 5/-3, 5%-3); printf("-5/-3=%d -5%%-3=%d\n", -5/-3, -5%-3);
Code:

5/3=1 5%3=2
-5/3=-1 -5%3=-2
5/-3=-1 5%-3=2
-5/-3=1 -5%-3=-2


SpawnerF 07-10-2017 18:53

Re: Integer division and modulo don't work correctly?
 
Hmm ... seems like a negative operand produce an incorrect value.
Look what it's writting :

Code:

/* forbidden operations */
forward operator%(Float:oper1, Float:oper2);
forward operator%(Float:oper1, oper2);
forward operator%(oper1, Float:oper2);

forbidden operations! lol

Edit : Sorry I was wrong :

So here it is the results :

In pawn
Code:

5/3  =        ( 1.66 =  1)        5  %  3 = 2 Correct ( wrong in the #2 Code )
-5/3 =  (-1.66 = -2)        -5 %  3 = 1 Correct \\ \\ \\
5/-3 =  (-1.66 = -2)        5  % -3 =-1 Correct \\ \\ \\
-5/-3=1  ( 1.66 =  1)        -5 % -3 =-2 Correct \\ \\ \\

It's using float round to get the result ( similar to floatround_round ) but in c++ it's using floatround_tozero ).
So one is returning the nearest number to X and c++ the opposite.

jimaway 07-11-2017 09:19

Re: Integer division and modulo don't work correctly?
 
Quote:

Originally Posted by SpawnerF (Post 2534524)

It's using float round to get the result ( similar to floatround_round ) but in c++ it's using floatround_tozero ).
So one is returning the nearest number to X and c++ the opsite.

no its not. the post is about integer calculations

SpawnerF 07-11-2017 09:41

Re: Integer division and modulo don't work correctly?
 
Ofc I was talking about %d since the result return a float num means that %d need to choose > .5 or < .5

PRoSToTeM@ 10-28-2017 11:11

Re: Integer division and modulo don't work correctly?
 
Oh, it's ok, because Pawn uses "floored division" instead of "truncated division".


All times are GMT -4. The time now is 23:09.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.