|
-
August 21st, 2009, 01:48 PM
#1
Modulus Error
I'm having trouble with modulus giving me the wrong answer.
It's taking 734170 and dividing by 7 and is coming up with a remainder of 3, which is wrong.
Here's the bugged out code:
Code:
// firstDayCount is set to 734190
// and dayNum is set to 21 at this point
firstDayCount = firstDayCount - (dayNum - 1) ;
firstDayCount = firstDayCount % 7;
// for some reason, after this line of code it comes the answer of 3 when it should be 4
I was wondering if there is a special way modulus works that I'm not fully aware of that could be causing it to come up with this answer.
-
August 21st, 2009, 02:13 PM
#2
Re: Modulus Error
 Originally Posted by ace10414
I'm having trouble with modulus giving me the wrong answer.
It's taking 734170 and dividing by 7 and is coming up with a remainder of 3, which is wrong.
The code is working fine. The remainder is 3.
Your calculation that it should be 4 is wrong.
734170 = 104881*7+3
-
August 21st, 2009, 04:22 PM
#3
Re: Modulus Error
Thank you for your clarification. 
It seems I was misunderstanding how modulus worked.
My teacher had told me that modulus would take the first digit to the right of the decimal as the remainder. Which is why I thought the answer was 4 because
734170/7 is 104881.4286
-
August 21st, 2009, 04:26 PM
#4
Re: Modulus Error
That would be correct only for (mod 10) operations.
-
August 21st, 2009, 04:28 PM
#5
Re: Modulus Error
 Originally Posted by ace10414
Thank you for your clarification.
It seems I was misunderstanding how modulus worked.
My teacher had told me that modulus would take the first digit to the right of the decimal as the remainder. Which is why I thought the answer was 4 because
734170/7 is 104881.4286
You must perform the above calculation then take the decimal expansion:
0.428571 ...
Then multiply this by what you divided by, which in this case is 7:
Which comes to 3 assuming the decimal expansion is to a great enough degree.
Rich
Visual Studio 2010 Professional | Windows 7 (x64)
Ubuntu
-
August 21st, 2009, 05:06 PM
#6
Re: Modulus Error
Thanks a lot for all the help. : D
I'm sure I can get my program to work with all of this advice.
-
August 21st, 2009, 05:18 PM
#7
Re: Modulus Error
 Originally Posted by ace10414
Thank you for your clarification.
It seems I was misunderstanding how modulus worked.
My teacher had told me that modulus would take the first digit to the right of the decimal as the remainder. Which is why I thought the answer was 4 because
734170/7 is 104881.4286
By the way, you can get the first decimal digit by doing this:
static_cast<int>((734170.0/7.0)*10.0)%10 = 4
(make sure you are using floating point calculation for everything inside the cast)
-
August 21st, 2009, 05:28 PM
#8
Re: Modulus Error
Oh, sweet! I'll use that instead since modulus works different than I had anticipated. Thanks!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|