CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: Modulus Error

  1. #1
    Join Date
    Aug 2009
    Posts
    6

    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.

  2. #2
    Join Date
    Aug 2009
    Posts
    14

    Re: Modulus Error

    Quote Originally Posted by ace10414 View Post
    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

  3. #3
    Join Date
    Aug 2009
    Posts
    6

    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

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Modulus Error

    That would be correct only for (mod 10) operations.

  5. #5
    Join Date
    Oct 2005
    Location
    England
    Posts
    803

    Re: Modulus Error

    Quote Originally Posted by ace10414 View Post
    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

  6. #6
    Join Date
    Aug 2009
    Posts
    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.

  7. #7
    Join Date
    Aug 2009
    Posts
    14

    Re: Modulus Error

    Quote Originally Posted by ace10414 View Post
    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)

  8. #8
    Join Date
    Aug 2009
    Posts
    6

    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
  •  





Click Here to Expand Forum to Full Width

Featured