Mathmatical formula for integer checking - Printable Version +- iDevGames Forums ( http://www.idevgames.com/forums)+-- Forum: Development Zone ( /forum-3.html)+--- Forum: Game Programming Fundamentals ( /forum-7.html)+--- Thread: Mathmatical formula for integer checking ( /thread-4591.html) |

Mathmatical formula for integer checking - Jones - Jan 20, 2006 06:47 PM
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? Mathmatical formula for integer checking - kodex - Jan 20, 2006 06:51 PM
Im not sure what you are asking for, are you trying to find a way to determine if something is a whole number? Mathmatical formula for integer checking - akb825 - Jan 20, 2006 07:31 PM
if (floor(num) == num) will check if it's a whole number or not. Mathmatical formula for integer checking - Jones - Jan 20, 2006 07:39 PM
Sorry, I meant an equation that could be duplicated in any language capable of mathematical manipulation. Mathmatical formula for integer checking - zKing - Jan 20, 2006 07:44 PM
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;` 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;` 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". Mathmatical formula for integer checking - zKing - Jan 20, 2006 08:03 PM
It was bugging me, so here's a faster version. Code: `float unknown_value;` Mathmatical formula for integer checking - Jones - Jan 20, 2006 08:08 PM
Well, I think I solved the problem... I was trying to see if a string containing "3" was an integer. *duh!* Mathmatical formula for integer checking - zKing - Jan 20, 2006 08:15 PM
DOH!! You never said anything about strings. Mathmatical formula for integer checking - akb825 - Jan 21, 2006 01:42 AM
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 ) Mathmatical formula for integer checking - Jones - Jan 23, 2006 09:07 PM
Well, I just learned that the language rounds floats into integers by itself anyway... so I'm ok. Mathmatical formula for integer checking - kodex - Jan 23, 2006 09:09 PM
Most programs will do that if you pass a float to a int. Mathmatical formula for integer checking - akb825 - Jan 23, 2006 09:25 PM
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. Mathmatical formula for integer checking - zKing - Jan 24, 2006 01:58 AM
Yep, to do a round, instead of a truncate: float input; ... int output = (int)(input + 0.5); |