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; 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". 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; 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;``` 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);