#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    24
    Rep Power
    0

    Need help about integer division remainder


    I was try to get the remainder of an integer division but when compiling i get the error "Invalid operands to binary % (have 'int' and 'double'). So here's the code I used:

    #include <stdlib.h>
    #include <stdio.h>
    #include <math.h>
    #include <time.h>
    #include <string.h>

    double x;

    void converter(x)
    {double a; x = x*0.0174532925;a = x%(2*3.141592654); x = x-(a*(2*3.141592654));}
    //converting x to radians and then to an interval of [0;2pi[

    void main()
    {
    double e;
    printf("\n\nGive a value for argument x: ");
    scanf("%lf",&x);
    converter(x);
    printf("%lf",x);
    }

    Can anyone help me? :confused:
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    Integer division means integer division. Both operands of modulo (%) need to be integer types, which double is not.

    Is modulo really what you want? For modulo to work, you'd have to convert both double operands to an integer type. But rather what you appear to be trying to do is to get the equivalent angle (ie, by subtracting full revolutions around the circle). You might consider simply subtracting a full circle from the angle. I normally put it into a loop so that I repeatedly subtract a full circle until the value is less that two_pi; that test will also only do the subtraction if the value is greater or equal to two_pi.

    Also, please stop making your code listings unreadable. Piling several statements onto the same line is extremely poor style that just makes the code unreadable. Plus it hides problems from the person who's the easiest for you to confuse and deceive, yourself. Indent your code properly. And use code tags to preserve that indentation when posting here (HTML strips out extra white space, including leading spaces and tabs).
  4. #3
  5. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,117
    Rep Power
    1803
    I think this does what you appear to want:

    C Code:
     
    #include <stdio.h>
     
    #define PI 3.141592654
    #define PI2  (PI * 2) 
     
    double radiansNormalised( double degrees )
    {
        double radians = degrees * PI / 180 ;
        return  radians - (PI2 * (int)(radians / PI2) ) ; 
    }
     
    int main()
    {
        double deg ;
        printf("Enter total degrees: ");
        scanf( "%lf", &deg ) ;
     
        double rad = radiansNormalised( deg ) ;
        printf( "Radians (normalised [0..2PI]) = %lf", rad ) ;
     
        return 0;
    }


    Note the use of a function return value rather than the clumsy global variable, and how code layout and appropriate variable and function names bring clarity. Also note the inclusion of only those headers necessary.

    But even that is long-winded, math.h defines a function fmod() that does exactly what you want.

    C Code:
     
    #include <math.h>
     
    double radiansNormalised( double degrees )
    {
        return  fmod( degrees * PI / 180, PI2 ) ; 
    }
    Last edited by clifford; October 23rd, 2013 at 04:52 PM.

IMN logo majestic logo threadwatch logo seochat tools logo