Home AMX User Forum NetLinx Studio

Bitwise Composition of a FLOAT

Greetings,

When looking at the bit structure of a float variable, what are the values of the bits?

Thanks.

Comments

  • cwpartridgecwpartridge Posts: 120
    The floating point implementation is IEEE 754-1985 standard format.
  • HedbergHedberg Posts: 671
    The floating point implementation is IEEE 754-1985 standard format.

    Looked at that one time and decided to look elsewhere -- not something to mess with just for fun.
  • ericmedleyericmedley Posts: 4,177
    The floating point implementation is IEEE 754-1985 standard format.

    I know I'm going to sleep more soundly tonight...
  • jweatherjweather Posts: 320
    The Wikipedia article on IEEE754 explains the conversion process step by step: http://en.wikipedia.org/wiki/IEEE_754

    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
  • PhreaKPhreaK Posts: 966
    Hedberg wrote: »
    not something to mess with just for fun.
    We obviously have different views on what's fun :p

    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.
  • TurnipTruckTurnipTruck Posts: 1,485
    PhreaK wrote: »
    We obviously have different views on what's fun :p
    Agreed! In my case I am trying to find a way to determine if my float variable has any non-integer value. (Anything after the decimal point)
  • ericmedleyericmedley Posts: 4,177
    Agreed! In my case I am trying to find a way to determine if my float variable has any non-integer value. (Anything after the decimal point)

    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.
  • jweatherjweather Posts: 320
    Agreed! In my case I am trying to find a way to determine if my float variable has any non-integer value. (Anything after the decimal point)

    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
    }
  • PhreaKPhreaK Posts: 966
    In my case I am trying to find a way to determine if my float variable has any non-integer value. (Anything after the decimal point)

    NetLinx Common Libraries - Math.axi

    Have a look at is_int(), line 175. It will also handle NaN's, +/-inf and subnormals.
Sign In or Register to comment.