How to represent floating point in binary. IEEE

18,089

Given how many bits?

0.1b:

0.00011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011...

As you can see it's an approximation.

Binary                                          Decimal
0.1     == 1/2^1         == 1/2              == 0.5
0.01    == 1/2^2         == 1/4              == 0.25
0.11    == 1/2^1 + 1/2^2 == 1/2 + 1/4 == 3/4 == 0.75

Each bit after the radix point represents 1/2^(position_after_bit_string).

postion:   |1|2|3|4|5|6|7|
         0.|0|0|0|0|0|0|1|

So 0.0000001 = 1/2^7 = 0.0078125

Pseudocode:

decimal_value = 0 
for i, bit in enumerate(binary_string):
    if bit == 1
         decimal_value += 1/2**i

For more info Why can't decimal numbers be represented exactly in binary?

Share:
18,089
Admin
Author by

Admin

Updated on June 05, 2022

Comments

  • Admin
    Admin about 2 years

    Similar to decimal binary numbers can have represent floats too. Now I read that it can have floats of the sort

    0.5:0.1 , 0.25:0.01 , 0.125:0.001 ... and so on. But then, for example, how is the 0.1(in decimal) represented in binary?

    Also, given a decimal float, how to convert it to the decimal equivalent, (given it is not so straightforward).

    Edit: So I understand that the better question would have been ; how to convert a decimal float to binary? Now i get it that we multiply the decimal part, till it becomes zero. Now it is very much possible that two floating points can have the same representation right?

  • Admin
    Admin over 11 years
    now it is very much possible that two floating points can have the same representation right?
  • Chris Seymour
    Chris Seymour over 11 years
    No, one bit string has only one value using this representation.
  • Eric Postpischil
    Eric Postpischil over 11 years
    @Sahil: When numbers are converted to floating point (as by parsing a string containing a numeral and converting it to floating point), different numbers can produce the same result. It is imprecise to say that the floating-point result represents these numbers. Per the IEEE 754 standard, a floating-point value represents precisely one number, the one you get by interpreting its encoding as specified in the standard. You can say that two different numbers may have the same approximation in floating point, but it is important to remember it is only an approximation.