Best way to see if a number is an integer

Started by
12 comments, last by Koshmaar 19 years, 4 months ago
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.
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.

throw table_exception("(? ???)? ? ???");

const float EPSILON = 0.00001f; bool IsInt(float f){	return (fabsf(f - (float)(int)(f + EPSILON)) < EPSILON);}
bool IsInt( float Test ){if( float( Test ) == int( Test ) )return true;elsereturn 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
My bad. Above post is mine.

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

- Jordan
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.
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke
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
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 :]
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]
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 :)

throw table_exception("(? ???)? ? ???");

This topic is closed to new replies.

Advertisement