3

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?

Chris Seymour
  • 83,387
  • 30
  • 160
  • 202

2 Answers2

6

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?

Community
  • 1
  • 1
Chris Seymour
  • 83,387
  • 30
  • 160
  • 202
  • now it is very much possible that two floating points can have the same representation right? –  Dec 01 '12 at 19:51
  • No, one bit string has only one value using this representation. – Chris Seymour Dec 01 '12 at 20:43
  • 3
    @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. – Eric Postpischil Dec 01 '12 at 23:10
0

Another observation that could be helpful. The 'integer part' of a floating point number is present in the binary in its 'normal' form, for instance if the value is 25.7482 you will have the bits '11001' (25) in the floating point, with the bits following representing the fraction (actually the first '1' is never stored, it's implied in the format). If you subtract 25.0 from that number, and multiply by 10, you get 7.482, and by examining the integer part of that value, you can obtain the first fractional digit, '7'.

Subtract 7.0, multiply by 10 gives 4.82 , thus the next digit '4', and so on. This process will in theory end eventually with a zero result, since all values that can be exactly represented in floating-point format have an exact decimal representation; however, that 'exact' result could have far more digits than are actually reasonable given the precision of the original floating point (and you may need extra precision internally to obtain that fully exact result, anyhow - you need to ensure the multiplication by 10 does not generate a rounding error).

And, for numbers like 6.432e-200 ,this method is workable but not very efficient (you'd be generating 199 zeros before the first '6' emerged).

greggo
  • 3,009
  • 2
  • 23
  • 22