Mathmatical formula for integer checking
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?
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.
Sorry, I meant an equation that could be duplicated in any language capable of mathematical manipulation.
Are you asking:
If you have a value in a variable that can hold fractional numbers (i.e. a float in Clike 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:
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.
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".
If you have a value in a variable that can hold fractional numbers (i.e. a float in Clike 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".
It was bugging me, so here's a faster version.
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;
Well, I think I solved the problem...
I was trying to see if a string containing "3" was an integer. *duh!*
I was trying to see if a string containing "3" was an integer. *duh!*
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 )
Well, I just learned that the language rounds floats into integers by itself anyway... so I'm ok.
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.
Possibly Related Threads...
Thread:  Author  Replies:  Views:  Last Post  
Dictionary Checking for word games  avidgamer101  5  4,871 
Jan 14, 2013 02:28 AM Last Post: avidgamer101 

Formula for converting angle to vector?  komirad  2  10,365 
Jul 29, 2011 07:29 AM Last Post: ThemsAllTook 

Use an integer value as part of a variable name  PHANTOMIAS  5  4,003 
Dec 8, 2009 09:24 AM Last Post: PHANTOMIAS 

Fast Distance formula?  mikey  11  7,903 
Nov 23, 2009 10:43 AM Last Post: mikey 

Direction formula?  TimMcD  2  5,485 
Nov 11, 2009 11:42 PM Last Post: TimMcD 