Bitwise Composition of a FLOAT
TurnipTruck
Posts: 1,485
Greetings,
When looking at the bit structure of a float variable, what are the values of the bits?
Thanks.
When looking at the bit structure of a float variable, what are the values of the bits?
Thanks.
0
Comments
Looked at that one time and decided to look elsewhere -- not something to mess with just for fun.
I know I'm going to sleep more soundly tonight...
Here's a converter to have somebody else do it for you: http://people.rit.edu/meseec/eecc250-winter99/IEEE-754hex32.html
And here is AMX code to do the conversions: http://www.amxforums.com/showthread.php?3423-Ieee-754
If you're receiving a serialized floating point value from a device the easiest way (assuming it's IEEE-754) to bring it into a float variable is to simply use the string_to_variable() function - handles all the grunt work for you. The fun way is to write you're own function that grabs out the mantissa, exponent and sign then figure it out yourself.
perhaps you could use the method of multiplying the decimal part above the decimal point, then dividing and subtracting to remove the part to the left of the decimal. If there's a remainder it'll be a positive number.
example
x=123.456
x_integer=the integer of x or 123
check=x*1000 (now check=123456)
check_integer=x_integer*1000 or 123000
then take =check-x_integer=456 (123456-123000)
If the result is positive, you have numbers to the right of the decimal place.
No easy way to do that from looking at the bits. All floats are stored in the format 1.X * 2^Y where X is the fractional part (bits 0-22) and Y is the exponent (bits 23-30, minus 127). So 17 for instance would be stored as 1.0625 * 2^4 = 1.0625 * 16 = 17. In other words, a float always has a fractional part, even if the resulting decimal value is an integer (unless it's a power of 2).
However, it's easy enough to check mathematically if a float has a fractional part:
float value, diff;
integer rounded;
rounded = type_cast(value+0.5); // round to nearest int
diff = value - rounded;
if (-0.01 < diff && diff < 0.01) {
// more or less an integer
} else {
// not very integerish
}
NetLinx Common Libraries - Math.axi
Have a look at is_int(), line 175. It will also handle NaN's, +/-inf and subnormals.