October 23rd, 2013, 08:30 AM

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:
October 23rd, 2013, 09:25 AM

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).
October 23rd, 2013, 03:21 PM

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", ° ) ;
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 longwinded, 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 03:52 PM.