42

I have this example on how to convert from a base 10 number to IEEE 754 float representation

Number: 45.25 (base 10) = 101101.01 (base 2) Sign: 0
Normalized form N = 1.0110101 * 2^5
Exponent esp = 5  E = 5 + 127 = 132 (base 10) = 10000100 (base 2)
IEEE 754: 0 10000100 01101010000000000000000

This makes sense to me except one passage:

45.25 (base 10) = 101101.01 (base 2)

45 is 101101 in binary and that's okay.. but how did they obtain the 0.25 as .01 ?

Steve Jessop
  • 273,490
  • 39
  • 460
  • 699
Johnny Pauling
  • 12,701
  • 18
  • 65
  • 108

7 Answers7

124

Simple place value. In base 10, you have these places:

... 103 102 101 100 . 10-1 10-2 10-3 ...

... thousands, hundreds, tens, ones . tenths, hundredths, thousandths ...

Similarly, in binary (base 2) you have:

... 23 22 21 20 . 2-1 2-2 2-3 ...

... eights, fours, twos, ones . halves, quarters, eighths ...

So the second place after the . in binary is units of 2-2, well known to you as units of 1/4 (or alternately, 0.25).

Community
  • 1
  • 1
Carl Norum
  • 219,201
  • 40
  • 422
  • 469
  • 3
    +1, although I found it a bit humorous to refer to the binary places as "eights, fours..." when there is never more than one of those values per place. – David Kaczynski Mar 08 '13 at 16:00
  • 1
    @David, that's a good point. I was waffling on what to do there, but I went with the plurals to match common decimal usage, hopefully to make the explanationeasy to understand. – Carl Norum Mar 08 '13 at 16:54
30

You can convert the part after the decimal point to another base by repeatedly multiplying by the new base (in this case the new base is 2), like this:

0.25 * 2 = 0.5

-> The first binary digit is 0 (take the integral part, i.e. the part before the decimal point).

Continue multiplying with the part after the decimal point:

0.5 * 2 = 1.0

-> The second binary digit is 1 (again, take the integral part).

This is also where we stop because the part after the decimal point is now zero, so there is nothing more to multiply.

Therefore the final binary representation of the fractional part is: 0.012.

Edit:

Might also be worth noting that it's quite often that the binary representation is infinite even when starting with a finite fractional part in base 10. Example: converting 0.210 to binary:

0.2 * 2 = 0.4   ->   0
0.4 * 2 = 0.8   ->   0
0.8 * 2 = 1.6   ->   1
0.6 * 2 = 1.2   ->   1
0.2 * 2 = ...

So we end up with: 0.001100110011...2.

Using this method you see quite easily if the binary representation ends up being infinite.

mkataja
  • 970
  • 2
  • 10
  • 28
13

"Decimals" (fractional bits) in other bases are surprisingly unintuitive considering they work in exactly the same way as integers.

base 10
scinot 10e2  10e1  10e0 10e-1 10e-2 10e-3
weight 100.0 10.0   1.0  0.1   0.01  0.001
value  0     4      5     .2      5      0

base 2
scinot 2e6 2e5 2e4 2e3 2e2 2e1 2e0 2e-1 2e-2 2e-3
weight 64  32  16   8   4   2   1   .5   .25 .125
value   0   1   0   1   1   0   1   .0    1    0   

If we start with 45.25, that's bigger/equal than 32, so we add a binary 1, and subtract 32.
We're left with 13.25, which is smaller than 16, so we add a binary 0.
We're left with 13.25, which is bigger/equal than 8, so we add a binary 1, and subtract 8.
We're left with 05.25, which is bigger/equal than 4, so we add a binary 1, and subtract 4.
We're left with 01.25, which is smaller than 2, so we add a binary 0.
We're left with 01.25, which is bigger/equal than 1, so we add a binary 1, and subtract 1.
With integers, we'd have zero left, so we stop. But:
We're left with 00.25, which is smaller than 0.5, so we add a binary 0.
We're left with 00.25, which is bigger/equal to 0.25, so we add a binary 1, and subtract 0.25.
Now we have zero, so we stop (or not, you can keep going and calculating zeros forever if you want)

Note that not all "easy" numbers in decimal always reach that zero stopping point. 0.1 (decimal) converted into base 2, is infinitely repeating: 0.0001100110011001100110011... However, all "easy" numbers in binary will always convert nicely into base 10.

You can also do this same process with fractional (2.5), irrational (pi), or even imaginary(2i) bases, except the base cannot be between -1 and 1 inclusive .

Mooing Duck
  • 64,318
  • 19
  • 100
  • 158
  • But we can't do a comparison of imaginary numbers, can we? – Rotsor Mar 08 '13 at 07:13
  • 1
    @Rotsor: I think we can, I just don't know how. Knuth figured out how to work with irrational bases: http://en.wikipedia.org/wiki/Quater-imaginary_base – Mooing Duck Mar 08 '13 at 17:21
  • @Rotsor & Mooing Duck: Rotsor is right in the sense that the mathematical inequalities (<, >, <=, >=) are not defined for complex numbers. A way around the problem would be to use Knuth's quater-imaginary base as complex numbers can be written in a single term in that base (example 7 + 4i (base 10) = 10323 (base 2i)). However, the comparisons in decimal base would not always yield the same results as in quater-imaginary. For example: 0 > -4 (base 10), while 0 < 1000 (base 2i). (-4 (base 10) = 1000 (base 2i)) – nijoakim Apr 05 '14 at 16:59
  • @nijoakim: Good to know that mathematical inequalities are not defined, and that the "obvious" way doesn't match up with comparisons of rational numbers. However, I retain my belief that there probably exists a way that makes sense. – Mooing Duck Apr 05 '14 at 17:15
  • @MooingDuck: Yes, I agree that that is quite possible. I've actually been trying to figure one out since you posted this, but it turned out to be quite hard. – nijoakim Apr 05 '14 at 18:45
11

2.00010 = 2+1 = 10.0002
1.00010 = 2+0 = 01.0002
0.50010 = 2-1 = 00.1002
0.25010 = 2-2 = 00.0102
0.12510 = 2-3 = 00.0012

Alexey Frunze
  • 61,140
  • 12
  • 83
  • 180
8

The fractions base 2 are .1 = 1/2, .01 = 1/4. ...

Peter Wooster
  • 6,009
  • 2
  • 27
  • 39
4

Think of it this way

(dot) 2^-1 2^-2 2^-3 etc

so

. 0/2 + 1/4 + 0/8 + 0/16 etc

See http://floating-point-gui.de/formats/binary/

1

You can think of 0.25 as 1/4.

Dividing by 2 in (base 2) moves the decimal point one step left, the same way dividing by 10 in (base 10) moves the decimal point one step left. Generally dividing by M in (base M) moves the decimal point one step left.

so

base 10                  base 2
--------------------------------------
1                      =>      1
1/2 = 0.5              =>    0.1
0.5/2 = 1/4 = 0.25     =>   0.01 
0.25/2 = 1/8 = 0.125   =>  0.001
.
.
.

etc.

Thanasis Ioannidis
  • 2,981
  • 1
  • 30
  • 50