How to get the modulus of a float?

Started by
10 comments, last by Inmate2993 18 years, 10 months ago
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.
-Dan- Can't never could do anything | DansKingdom.com | Dynamic Particle System Framework for XNA
Advertisement
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;
There's the function fmodf.

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

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
Awesome, that's exactly what I was looking for (fmodf). Thanks!
-Dan- Can't never could do anything | DansKingdom.com | Dynamic Particle System Framework for XNA
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.
-Dan- Can't never could do anything | DansKingdom.com | Dynamic Particle System Framework for XNA
Quote:Original post by deadlydog
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.


You can also just get the decimal points like this:

float number = 1.23456
float decimal = number - (int)number;

Quote:Original post by deadlydog
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.

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.
"Walk not the trodden path, for it has borne it's burden." -John, Flying Monk
Quote:Original post by invective
You can also just get the decimal points like this:

float number = 1.23456
float decimal = number - (int)number;


Will (int)number always round down though? Like, if the number was 1.8 would decimal = 1.8 - 1, or 1.8 - 2?

Thanks
-Dan- Can't never could do anything | DansKingdom.com | Dynamic Particle System Framework for XNA

This topic is closed to new replies.

Advertisement