Thread: Modulus problem...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    84

    Modulus problem...

    Problem with Modulus

    I am doing fmod(-20, 360) and not getting the correct answer (340).

    Code:
    #include <math.h>
    #include <stdlib.h>
    #include <stdio.h>
    
    int main()
    {
    
      float x = -20.0;
      float y = 11.0;
      float z = 390.0;
    
      float x_new, y_new, z_new;
    
      x_new = fmod(x, 360.0);
      y_new = fmod(y, 360.0);
      z_new = fmod(z, 360.0);
    
      printf("Using modulus, x = %f becomes x = %f.\n",
             x, x_new);
      printf("Using modulus, y = %f becomes y = %f.\n",
             y, y_new);
      printf("Using modulus, y = %f becomes y = %f.\n",
             z, z_new);
    
      return 0;
    
    }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    You didn't say what answer you're getting, but I'm guessing it's -20. I think you just don't quite understand what fmod is supposed to do. From the standard:
    Quote Originally Posted by C99 7.12.10.1 p3
    The fmod functions return the value x − ny, for some integer n such that, if y is nonzero,
    the result has the same sign as x and magnitude less than the magnitude of y.If y is zero,
    whether a domain error occurs or the fmod functions return zero is implementation-defined
    x/y is some small fraction, which, rounded to an integer, toward 0, gives n = 0. -20 - 0 * 360 = -20.

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    84
    Yeah I get -20... But how can I get 340? That is the right answer (i.e., from google).

  4. #4
    Registered User gardhr's Avatar
    Join Date
    Apr 2011
    Posts
    151
    Quote Originally Posted by towed View Post
    Yeah I get -20... But how can I get 340? That is the right answer (i.e., from google).
    Subtract the result from 360, of course.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by gardhr View Post
    Subtract the result from 360, of course.
    Don't you mean add the results to 360?

    360 - -20 = 380
    360 + - 20 = 340


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User gardhr's Avatar
    Join Date
    Apr 2011
    Posts
    151
    Quote Originally Posted by quzah View Post
    Don't you mean add the results to 360?

    360 - -20 = 380
    360 + - 20 = 340


    Quzah.
    [sarcasm]Hey thanks for the math lesson Quzah.[/sarcasm]

    Yes, obviously I meant "add".

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by gardhr View Post
    [sarcasm]Hey thanks for the math lesson Quzah.[/sarcasm]

    Yes, obviously I meant "add".
    You may have meant it, but you said something else, so your post was misleading. Quzah's post correcting you was therefore justified.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  8. #8
    Registered User gardhr's Avatar
    Join Date
    Apr 2011
    Posts
    151
    Quote Originally Posted by grumpy View Post
    Quzah's post correcting you was therefore justified.
    I have no problem with being corrected. What annoys me is the patronizing tone; asking someone to "confirm the contrary" followed by a graphic illustration of why it must indeed be so implies an ignorant subject - not someone who merely mixed up some words. An appropriate correction, in this case, would have simply been to address the error itself.

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    From what evidence I can see, you are responding to a black cat in a dark room that is not there.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  10. #10
    Registered User gardhr's Avatar
    Join Date
    Apr 2011
    Posts
    151
    Quote Originally Posted by grumpy View Post
    From what evidence I can see, you are responding to a black cat in a dark room that is not there.
    Maybe, maybe not. It wouldn't be the first time Quzah's exhibited passive-aggressive behavior, though. I can never be too sure...
    Last edited by gardhr; 10-02-2011 at 01:11 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. %(Modulus)
    By cuo741 in forum C Programming
    Replies: 19
    Last Post: 05-14-2010, 08:50 AM
  2. modulus
    By nicknack in forum C Programming
    Replies: 7
    Last Post: 12-15-2007, 09:45 AM
  3. Help with Modulus
    By boontune in forum C++ Programming
    Replies: 4
    Last Post: 01-20-2003, 10:26 AM
  4. overloading Modulus
    By weehoo in forum C++ Programming
    Replies: 2
    Last Post: 09-15-2002, 11:50 AM
  5. Modulus/Modulo?
    By Shadow12345 in forum C++ Programming
    Replies: 6
    Last Post: 03-13-2002, 11:29 PM