How to represent floating point in binary. IEEE
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?
![Admin](/assets/logo_square_200-5d0d61d6853298bd2a4fe063103715b4daf2819fc21225efa21dfb93e61952ea.png)
Admin
Updated on June 05, 2022Comments
-
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 over 11 yearsnow it is very much possible that two floating points can have the same representation right?
-
Chris Seymour over 11 yearsNo, one bit string has only one value using this representation.
-
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.