Mathmatical formula for integer checking

Jones
Unregistered
 
Post: #1
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?
Quote this message in a reply
Moderator
Posts: 613
Joined: 2004.09
Post: #2
Im not sure what you are asking for, are you trying to find a way to determine if something is a whole number?

Kyle Richter
DragonForged.com
Twitter: @kylerichter
Quote this message in a reply
Moderator
Posts: 1,140
Joined: 2005.07
Post: #3
if (floor(num) == num) will check if it's a whole number or not.
Quote this message in a reply
Jones
Unregistered
 
Post: #4
Sorry, I meant an equation that could be duplicated in any language capable of mathematical manipulation.
Quote this message in a reply
zKing
Unregistered
 
Post: #5
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".
Quote this message in a reply
zKing
Unregistered
 
Post: #6
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;
Quote this message in a reply
Jones
Unregistered
 
Post: #7
Well, I think I solved the problem...

I was trying to see if a string containing "3" was an integer. *duh!* Wacko
Quote this message in a reply
zKing
Unregistered
 
Post: #8
DOH!!

You never said anything about strings. Wink
Quote this message in a reply
Moderator
Posts: 1,140
Joined: 2005.07
Post: #9
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)
Quote this message in a reply
Jones
Unregistered
 
Post: #10
Well, I just learned that the language rounds floats into integers by itself anyway... so I'm ok. Smile
Quote this message in a reply
Moderator
Posts: 613
Joined: 2004.09
Post: #11
Most programs will do that if you pass a float to a int.

Kyle Richter
DragonForged.com
Twitter: @kylerichter
Quote this message in a reply
Moderator
Posts: 1,140
Joined: 2005.07
Post: #12
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.
Quote this message in a reply
zKing
Unregistered
 
Post: #13
Yep, to do a round, instead of a truncate:

float input;
...
int output = (int)(input + 0.5);
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Dictionary Checking for word games avidgamer101 5 3,870 Jan 14, 2013 02:28 AM
Last Post: avidgamer101
  Formula for converting angle to vector? komirad 2 9,468 Jul 29, 2011 07:29 AM
Last Post: ThemsAllTook
  Use an integer value as part of a variable name PHANTOMIAS 5 3,691 Dec 8, 2009 09:24 AM
Last Post: PHANTOMIAS
  Fast Distance formula? mikey 11 7,077 Nov 23, 2009 10:43 AM
Last Post: mikey
  Direction formula? TimMcD 2 5,016 Nov 11, 2009 11:42 PM
Last Post: TimMcD