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. 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).
3. I think this does what you appear to want:

C Code:

#include <stdio.h>

#define PI 3.141592654
#define PI2  (PI * 2)

{
double radians = degrees * PI / 180 ;
}

int main()
{
double deg ;
printf("Enter total degrees: ");
scanf( "%lf", &deg ) ;

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>