Jump to content
  • Advertisement
Sign in to follow this  
deadlydog

How to get the modulus of a float?

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

If you intended to correct an error in the post then please contact us.

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 this post


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

Share this post


Link to post
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 this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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;

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!