# How to get the modulus of a float?

This topic is 4963 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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.

##### Share on other sites
Modulus only acts on integers. You could truncate the float to an int and then take the modulus though.

##### Share on other sites
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;

##### Share on other sites
There's the function fmodf.

##### Share on other sites
Quote:
 Original post by EndurionThere'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

##### Share on other sites
Awesome, that's exactly what I was looking for (fmodf). Thanks!

##### Share on other sites
Quote:
 Original post by cignox1Modulus 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.

##### Share on other sites
Quote:
Quote:
 Original post by cignox1Modulus 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;

##### Share on other sites
Quote:
Quote:
 Original post by cignox1Modulus 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.

##### Share on other sites
Quote:
 Original post by invectiveYou can also just get the decimal points like this:float number = 1.23456float 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

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 10
• 12
• 10
• 11
• 13
• ### Forum Statistics

• Total Topics
634095
• Total Posts
3015477
×