iDevGames Forums

Full Version: Mathmatical formula for integer checking
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

Jones

Let's just say that you were using programming language X (an unknown), and it had no commands for checking if an item was an integer.

Is there a mathematical formula for finding out if an item is an integer?
Im not sure what you are asking for, are you trying to find a way to determine if something is a whole number?
if (floor(num) == num) will check if it's a whole number or not.

Jones

Sorry, I meant an equation that could be duplicated in any language capable of mathematical manipulation.

zKing

Are you asking:
If you have a value in a variable that can hold fractional numbers (i.e. a float in C-like languages), but you have no function like "bool IsInteger(float)" in your libraries to determine if that number has a fractional part... how would you figure that out mathematically?

Most languages truncate on float point to integer type conversions, so usually you can do something like:

Code:
float unknown_value;
int integer_part = (int)unknown_value;
float fractional_part = unknown_value - (float)integer_part;


Now if you are saying just "in theory" and you had no integer truncation/round/etc... just float types and basic add/subtract math... the following could work, but would be _VERY_ slow.

Code:
float unknown_value;

// normalize it to a positive value (or zero)
if(unknown_value < 0.0)
    unknown_value = -unknown_value;

// keep pulling off integers until we hit zero or just fractional remainder
while(unknown_value >= 1.0)  
{
    unknown_value -= 1.0;
}

if(unknown_value > 0.0)   // will be less than 1, either zero or fractional part
    return false;   // not an integer
else
    return true;    // was exactly an integer.

That's the simple but slow version. You could speed it up by doing some powers of two subtraction stuff, but again this is just "in theory".

zKing

It was bugging me, so here's a faster version. Wink

Code:
float unknown_value;

if(unknown_value == 0.0)
    return true;

// normalize it to a positive value
if(unknown_value < 0.0)
    unknown_value = -unknown_value;

if(unknown_value < 1.0)
    return false;

float power_of_two = 1.0;

// find power of two that just one power of two smaller than unknown_value (or exactly equal)
while(power_of_two * 2.0 <= unknown_value)
{
    power_of_two *= 2.0;
}

// start subtracting off powers of two until unknown_value is less than 1
while(unknown_value >= 1.0)
{
    unknown_value -= power_of_two;
    while(power_of_two > unknown_value)
    {
        power_of_two /= 2.0;
    }
}

if(unknown_value == 0.0)
    return true;
else
    return false;

Jones

Well, I think I solved the problem...

I was trying to see if a string containing "3" was an integer. *duh!* Wacko

zKing

DOH!!

You never said anything about strings. Wink
If you were using C (or a derivative), you could just use strtol. For the last parameter, pass in a char * (it doesn't have to actually point to anything at the beginning), and if at the end it isn't pointing to NULL, it's not a string. (hooray for actually learning something in the programming assignment I was working on today Rasp)

Jones

Well, I just learned that the language rounds floats into integers by itself anyway... so I'm ok. Smile
Most programs will do that if you pass a float to a int.
Note that it doesn't round, it just truncates it. IE, if you have 2.9999, it will become 2. You will need to use a round function to get it up to 3.

zKing

Yep, to do a round, instead of a truncate:

float input;
...
int output = (int)(input + 0.5);
Reference URL's