View unanswered posts | View active topics It is currently Wed Oct 01, 2014 8:50 am






Reply to topic  [ 4 posts ] 
Modulus or Mike bug? 
Author Message
Rookie

Joined: Thu Sep 13, 2012 9:43 am
Posts: 47
Post Modulus or Mike bug?
If I do -10 mod(%) 3 in Python, excel, google calc etc I get result 2.

If i do -10 mod(%) 3 in RobotC I get -1.

I have obviously missed something somewhere as I would expect 1! I am clearly wrong, but why different results in RobotC?

Why?


Mon Sep 24, 2012 6:41 am
Profile
Moderator
Moderator
User avatar

Joined: Tue Sep 14, 2010 9:19 pm
Posts: 496
Post Re: Modulus or Mike bug?
This is a fun one. Lets start with what the modulo operator does: Lets say you do 10 % 3. It finds the largest multiple of 3 that is less than 10, which is 9. Think of it like this: 10 / 3 = 3.333, so if you round down, the answer is 3, and 3 * 3 = 9. Then it gives you the difference between 10 and 9, which is (10 - 9) = 1. Now when you do (-10) % 3, (-10) / 3 = -3.333. When you round down, that equals -4. 3 * -4 = -12. The difference is (-10 - (-12)) = 2. Now some compilers will round toward zero, not down, which is what you might expect. So, in that case, when you do (-10) % 3, (-10) / 3 = -3.333, rounded toward zero = -3, 3 * -3 = -9, and (-10 - (-9)) = -1. Does that answer your question?

_________________
sudo rm -rf /


Mon Sep 24, 2012 10:25 am
Profile
Rookie

Joined: Thu Sep 13, 2012 9:43 am
Posts: 47
Post Re: Modulus or Mike bug?
magicode wrote:
This is a fun one. Lets start with what the modulo operator does: Lets say you do 10 % 3. It finds the largest multiple of 3 that is less than 10, which is 9. Think of it like this: 10 / 3 = 3.333, so if you round down, the answer is 3, and 3 * 3 = 9. Then it gives you the difference between 10 and 9, which is (10 - 9) = 1. Now when you do (-10) % 3, (-10) / 3 = -3.333. When you round down, that equals -4. 3 * -4 = -12. The difference is (-10 - (-12)) = 2. Now some compilers will round toward zero, not down, which is what you might expect. So, in that case, when you do (-10) % 3, (-10) / 3 = -3.333, rounded toward zero = -3, 3 * -3 = -9, and (-10 - (-9)) = -1. Does that answer your question?


Yes thanks, it's not a bug, it's a feature!

I assumed this was a standard mathematical operation that would be the same in every compiler. You know what they say about assumption? Useful to know, thanks for the detailed explanation.


Mon Sep 24, 2012 11:32 am
Profile
Moderator
Moderator
User avatar

Joined: Tue Sep 14, 2010 9:19 pm
Posts: 496
Post Re: Modulus or Mike bug?
Yep, some compilers may not play nice with your code.
P.S: It took me quite a while to see that was a reference to my signature.

_________________
sudo rm -rf /


Mon Sep 24, 2012 1:21 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 4 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.