Jump to content
  • Advertisement
Sign in to follow this  
load_bitmap_file

Best way to see if a number is an integer

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

I feel like an idiot asking this but I don't think I ever found out the best way to see if a number is an integer and the darn GameDev search is down again (Does anyone know why? I really miss it when it's down). Meaning, if we have
#include <cmath>

int main()
{
float number1 = std::sqrt(36.0f);
float number2 = std::sqrt(19.0f);

return 0;
}

What's the best method for seeing if number1 and number2 are integers? I've been using a float and integer cast hack, but it's very ugly and there's got to be a better solution.

Share this post


Link to post
Share on other sites
Advertisement
Well, since most numbers aren't able to be represented exactly in floating point format you're best bet is to see if the real floating point value is within an acceptable range with respect to the nearest integer value.

Something like:

if(value - FLOOR(value) < epsilon || CIELING(value) - value < epsilon)
return true;

return false;


Where epsilon is your acceptable range.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster

bool IsInt( float Test )
{
if( float( Test ) == int( Test ) )
return true;
else
return false;
}



It's a bit of a slow-down from the cast, don't think it'll cause too much overhead though. If you don't need too much precision, then that's what I'd suggest, if you're doing some funky stuff to the float though, you're going to have some rounding problems and should prolly use the first answer

- Jordan

Share this post


Link to post
Share on other sites
My bad. Above post is mine.

EDIT: Eh... Nevermind. You should prolly use the above examples, mine's pretty flakey. [smile]

- Jordan

Share this post


Link to post
Share on other sites
For what purpose is this needed anyway? I'm supposing that your sqrt() example was merely that, an example. If you need 100% reliable mathematical accuracy, then you're probably going to have to really on more mathematical methods, such as keeping rational numbers in exact, fractional form (integer over integer, rather than floating point), keeping irrational numbers in their representative form (sqrt(2) stays as that, not as 1.4142...), etcetera. Which means that for things such as sqrt(36), you're just going to have to know that it is exactly 6, or write something to factor it into its prime components. Things like that.

Share this post


Link to post
Share on other sites
This is off topic, but if the gdnet search is down, you can use google and type "<your search term(s)> site:gamedev.net" (without quotes or angle brackets)

Regards,
jflanglois

Share this post


Link to post
Share on other sites
Quote:
Original post by Ravyne
Well, since most numbers aren't able to be represented exactly in floating point format you're best bet is to see if the real floating point value is within an acceptable range with respect to the nearest integer value.

Something like:

if(value - FLOOR(value) < epsilon || CIELING(value) - value < epsilon)
return true;

return false;


Where epsilon is your acceptable range.


Quote:
Original post by Aprosenf
const float EPSILON = 0.00001f;

bool IsInt(float f)
{
return (fabsf(f - (float)(int)(f + EPSILON)) < EPSILON);
}


This method looks good, thanks.

Agony: The sqrt example was actually the thing I needed it for, but I'll definitely need to use it for more complex things eventually. Thanks everybody.

EDIT:
jflanglois: The google search doesn't as well as GameDev's but it's passable. I nearly forgot google can do that :]

Share this post


Link to post
Share on other sites
FYI
maybe it was just me but I remember once that FLOOR/CEIL ran incredibly slowly on my machine (considering what they did).. so the fabsf version is likly a lot better.

But then again this was a long time ago, and this predudice has stuck so I havn't tried it since [smile]

Share this post


Link to post
Share on other sites
Float to int is always a slow opperation. I believe theres finally a hardware instructuion for it in SSE3 though. Theres all sorts of interesting mathematical tricks to make it go as fast as possible though. Google for "fast float to int conversion" some time :)

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!