View unanswered posts | View active topics It is currently Sun Jul 24, 2016 10:52 am

 Page 1 of 1 [ 4 posts ]
 Print view Previous topic | Next topic
Modulus or Mike bug?
Author Message
Rookie

Joined: Thu Sep 13, 2012 9:43 am
Posts: 47
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
Moderator

Joined: Tue Sep 14, 2010 9:19 pm
Posts: 496
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
Rookie

Joined: Thu Sep 13, 2012 9:43 am
Posts: 47
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
Moderator

Joined: Tue Sep 14, 2010 9:19 pm
Posts: 496
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
Display posts from previous:  Sort by
 Page 1 of 1 [ 4 posts ]

#### Who is online

Users browsing this forum: No registered users and 1 guest

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

Search for:
 Jump to:  Select a forum ------------------ ROBOTC Applications    ROBOTC for LEGO MINDSTORMS       Third-party sensors    ROBOTC for CORTEX & PIC    ROBOTC for VEX IQ    ROBOTC for Arduino    Robot Virtual Worlds    Multi-Robot Communications    Issues and Bugs Competitions & Partners    Mini Urban Challenge    CS2N Robot Virtual Worlds Competitions       VEX Skyrise Competition 2014-2015       VEX Toss Up 2013-2014       FTC Block Party! 2013-2014    Competitions using VEX - BEST, TSA, VEX, and RoboFest!    FTC Programming    RoboCup Junior and Other ROBOT Competitions Virtual Brick Robotics Discussions    General Discussions    Project Discussions Off-Topic ROBOTC Forum & ROBOTC.net Suggestions/Feedback    ROBOTC Forums Suggestions/Comments    ROBOTC.net Suggestions/Comments       NXT Programming: Tips for Beginning with ROBOTC       VEX Programming: Tips for Beginning with ROBOTC    2013 Robotics Summer Of Learning       VEX Toss Up Programming Challenge       FTC Ring It Up! Programming Challenge    International Forums       Spanish Forums          ROBOTC for MINDSTORMS          ROBOTC for VEX       French Forums          ROBOTC pour Mindstorms          ROBOTC pour IFI VEX       Japanese Forums （日本語のフォーラム）       German Forums    2015 Spring Carnival Event    PLTW (Project Lead The Way)    Robotics Merit Badge    2014 Robotics Academy Summer of Learning