How to get the modulus of a float?
Is there a way you can get the modulus (%) of a float. I'm using VSC++ .NET and whenever if I try getting the modulus of a float I get this:
error C2296: '%' : illegal, left operand has type 'float'
and if I try using a float for the modulus I get this:
error C2297: '%' : illegal, right operand has type 'float'
So this obviously means that floats aren't meant to be used with the modulus at all. Is there a way around this? Thanks.
Modulus only acts on integers. You could truncate the float to an int and then take the modulus though.
Modulus is what remains after a division between two integers: with floats you can get decimals so why calculating the modulus?
The only way I see is casting the float to an int:
(int)float_number % (int)another_float;
The only way I see is casting the float to an int:
(int)float_number % (int)another_float;
Quote:Original post by Endurion
There's the function fmodf.
Or just fmod. They're the same function, except fmodf is explicitly for floats while fmod is overloaded for several floating point types.
CM
Quote:Original post by cignox1
Modulus is what remains after a division between two integers: with floats you can get decimals so why calculating the modulus?
I'm using it with 1.0 as the modulus to get the decimal places. That's what I want it for.
Quote:Original post by deadlydogQuote:Original post by cignox1
Modulus is what remains after a division between two integers: with floats you can get decimals so why calculating the modulus?
I'm using it with 1.0 as the modulus to get the decimal places. That's what I want it for.
You can also just get the decimal points like this:
float number = 1.23456
float decimal = number - (int)number;
Quote:Original post by deadlydogQuote:Original post by cignox1
Modulus is what remains after a division between two integers: with floats you can get decimals so why calculating the modulus?
I'm using it with 1.0 as the modulus to get the decimal places. That's what I want it for.
To get the fraction part, use modf:
double modf(
double x,
double *intptr
);
It returns the fraction part, and send the integer part to the intprt out parameter.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement