Wednesday, November 10, 2010

How to do modulus with floats and doubles in C

You may be tempted to use the '%' operator but that's only for integers.
To do modulus arithmetic with doubles and floats use the math.h library.
Use the following functions:

double fmod(double x, double y);
float fmodf(float x, float y);
long double fmodl(long double x, long double y);

Use -lm to link to the library.

No comments: