I don't understand how your code relates to the floating point modulo operator that you mention. Such an operator converts floats to integers; your code does not.
If such a conversion is desired, use function modff
from the standard library.
#include <math.h>
float a, dummy;
...
a = modff(a, &dummy);
If you don't want any such conversion, the answer of @devnull is a correct rewrite of your code. If in addition you want to "include the upper value" and, e.g. have 1 map to 1, then you're in a bit of trouble, as this would involve checking whether a floating point number is integer or not. To do this portably, I'd suggest again the use of modff
.
SYNOPSIS
float modff(float x, float *iptr);
Link with -lm
.
DESCRIPTION
The modf()
function breaks the argument x
into an integral part and a
fractional part, each of which has the same sign as x
. The integral
part is stored in the location pointed to by iptr
.