Jump to content
  • Advertisement
Sign in to follow this  

Checking if a value given to int is a whole number

This topic is 3601 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

As topic suggests, is there are a way to check if a number I gave to an int variable is a whole number or not(Which will make it return a whole value anyway...)?

Share this post


Link to post
Share on other sites
Advertisement
An integer will always hold a whole number. If you try and set a float to it, for example, it will get casted from a float to an int and precision will be lost.

Share this post


Link to post
Share on other sites
If the value you're assigning from is an integral type, then it's a whole number, necessarily. If it's a floating-point type, then you have to understand that these values are not exact.

/puts on psychic hat.

If you want to find out, for example, if x divides into y evenly, you do not want to do the division in floating point, and then check if the floating point result "is a whole number". Instead, you want to ask for the remainder of x divided by y in integer math - which is done using the modulus operator, '%' - and see if that's zero.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!