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?
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?
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).
来源:https://stackoverflow.com/questions/13663026/how-to-represent-floating-point-in-binary-ieee