Raised This Month: $ Target: $400
 0% 

Solved Integer division and modulo don't work correctly?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
PRoSToTeM@
Veteran Member
Join Date: Jan 2010
Location: Russia, Ivanovo
Old 07-10-2017 , 17:51   Integer division and modulo don't work correctly?
Reply With Quote #1

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
__________________

Last edited by PRoSToTeM@; 04-10-2018 at 17:43.
PRoSToTeM@ is offline
Send a message via ICQ to PRoSToTeM@ Send a message via Skype™ to PRoSToTeM@
SpawnerF
Member
Join Date: Apr 2017
Location: Morocco
Old 07-10-2017 , 18:53   Re: Integer division and modulo don't work correctly?
Reply With Quote #2

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.
__________________
XX was created just for giving evidence and not meant to damage public servers.
&We do not test on public servers.
Thank's.

Last edited by SpawnerF; 07-11-2017 at 09:42.
SpawnerF is offline
jimaway
Heeeere's Jimmy!
Join Date: Jan 2009
Location: Estonia
Old 07-11-2017 , 09:19   Re: Integer division and modulo don't work correctly?
Reply With Quote #3

Quote:
Originally Posted by SpawnerF View Post

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
jimaway is offline
SpawnerF
Member
Join Date: Apr 2017
Location: Morocco
Old 07-11-2017 , 09:41   Re: Integer division and modulo don't work correctly?
Reply With Quote #4

Ofc I was talking about %d since the result return a float num means that %d need to choose > .5 or < .5
__________________
XX was created just for giving evidence and not meant to damage public servers.
&We do not test on public servers.
Thank's.

Last edited by SpawnerF; 07-11-2017 at 09:43.
SpawnerF is offline
PRoSToTeM@
Veteran Member
Join Date: Jan 2010
Location: Russia, Ivanovo
Old 10-28-2017 , 11:11   Re: Integer division and modulo don't work correctly?
Reply With Quote #5

Oh, it's ok, because Pawn uses "floored division" instead of "truncated division".
__________________
PRoSToTeM@ is offline
Send a message via ICQ to PRoSToTeM@ Send a message via Skype™ to PRoSToTeM@
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 07:25.


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