25

How would you write a fraction in binary numbers; for example, $1/4$, which is $.25$? I know how to write binary of whole numbers such as 6, being $110$, but how would one write fractions?

amWhy
  • 209,954
Fernando Martinez
  • 6,698
  • 19
  • 74
  • 108

8 Answers8

28

Note: $$\dfrac 14_{\,\text{ ten}} = .25 = \color{blue}{\bf 2} \times 10^{-1} + \color{blue}{\bf 5} \times 10^{-2} $$ $$ \frac14= \dfrac{1}{2^2}_{\,\text{ten}} = 2^{-2} = \color{blue}{\bf 0} \times 2^{-1} + \color{blue}{\bf 1}\cdot 2^{-2} = .01_{\text{ two}}$$

amWhy
  • 209,954
  • Yes I like this answer the most. – Fernando Martinez Feb 12 '13 at 18:58
  • So I I had $\frac{1}{8}$ would I do $(2^{-3}+2^{-2}+2^{-1}+2^0)$ – Fernando Martinez Feb 12 '13 at 19:00
  • 4
    Yup, but start with $\frac 18 = 2^{-3} = 0 \times 2^{-1} + 0\times 2^{-2} + 1\times 2^{-3} = 0.001_{\text{two}}$ – amWhy Feb 12 '13 at 19:02
  • yes that makes sense. – Fernando Martinez Feb 12 '13 at 19:04
  • 1
    $2^0 \iff ;$ the "one's place". $2^{-1}$ corresponds to the first digit to the right of the decimal point, then the place to the right of that corresponds to $2^{-2}$...and so on. – amWhy Feb 12 '13 at 19:05
  • .01 is to binary what a "decimal fraction" or "decimal" is to decimal. Hence, it is a "binimal?" – Kaz Feb 13 '13 at 03:23
  • Might be worth mentioning that it works precisely the same as in decimal to the extent that the same algorithms we use for doing long division by hand also work in binary. So if you want the "binimal" representation of some fractional number, just do the long division. – Shufflepants May 02 '17 at 18:34
  • Thank you for your comment, @Shufflepants! Oh, my goodness, this is an answer I wrote four years ago! Feel free to post an answer here, as well! – amWhy May 02 '17 at 18:37
  • @amWhy I believe some one else already has an answer mentioning long-division. But I was under the impression we should want that the top/accepted answer to be the best/include as much as possible. – Shufflepants May 02 '17 at 18:42
  • I'll try and work on it, @Shufflepants But one of the beauties of this site is the collage of answers. I'm a bit worried to change the accepted answer, to include information that has been provided in another post (in terms of not wanting to hijack that answer). But I do thank you for your interest. I'll favorite this question above, so I can easily return to it relatively soon! – amWhy May 02 '17 at 18:47
27

As you mentioned, $$6 = {\color{red}1}\cdot 2^2+ {\color{red}1}\cdot 2^1+{\color{red}0}\cdot 2^0 = {\color{red}{110}}_B.$$ Analogously $$\frac{1}{4} = \frac{1}{2^2} = {\color{red}0}\cdot2^0 + {\color{red}0}\cdot 2^{-1} + {\color{red}1}\cdot 2^{-2} = {\color{red}{0.01}}_B.$$

Edit:

These pictures might give you some more intuition ;-) Here $\frac{5}{16} = 0.0101_B$, as the denominator is of form $2^n$, the representation is finite (process ends when you hit zero); $\frac{1}{6} = 0.0010\overline{10}_B$ as the denominator is not of form $2^n$, but the number is rational, so representation is infinite and periodic.

binary fraction 5/15 binary fraction 1/6

I hope this helps ;-)

dtldarek
  • 37,381
6

Three basic ways, all seen in binary number systems:

Fixed-Point: One integer holds the "integer part"; another holds the "fractional part". This is simple to store and display, and has very high magnitude and precision with virtually no error, but doing real math with the numbers involved can get hairy. Decimal numbers aren't often seen in this form, but it is a possibility.

Maintained Floating Point: a large integer holds the entire value, and a second smaller number maintains the relative place of the decimal point from the right (or left) side of the number. Much easier to manipulate for mathematical operations, same maintenance of precision, zero error, and used in many implementations of "BigDecimal" object types where the "built-in" floating point mechanisms aren't available. Can be more difficult to represent in base-10 form on-screen. If implemented with normal integer types, this method can be more limited in magnitude than the previous one; instead, many implementations use a byte array to store the number, allowing the numbers to be as big as system memory allows.

Implicit Floating Point: The number is expressed in what amounts to "binary scientific notation". A "mantissa" is stored as an integer, with the decimal point implied to be on the far right. Then the exponent of a power of two is also stored. The actual value of this number is the mantissa, multiplied by two to the power of the exponent. This approach allows for the storage and calculation of truly massive numbers, and modern CPUs are designed with a Floating-Point Unit or FPU (sometimes called a "math co-processor", in the early days of its integration into 486-class CPUs) that accelerates calculations of numbers in this form. However, there are two problems; first, there's a tradeoff between extreme precision and extreme magnitude; the mantissa, and thus the number of digits that can be stored precisely, is fixed, so as magnitude increases, the number of possible decimal places decreases (in the extremes of magnitude you often can't get more granular than the millions place). Second, there's an amount of "rounding error" inherent in using floating-point numbers, with the inherent conversion to binary and back; this can cause errors in calculations requiring exact precision (such as when dealing with money), and so unless the extreme magnitude of a floating-point type is required, it's generally recommended to use a method of representation that does not introduce error.

KeithS
  • 2,759
  • This is a thorough answer, but I don't think the OP was asking about computer implementations. – LarsH Feb 12 '13 at 21:36
  • 1
    Although, what the OP is asking about is a typographic representation of numbers, which is very close to computer representation. Binary and decimal are just ways we can write down numbers with a pencil, and computer representations are how we write numbers into a memory with ones and zeros. – Kaz Feb 13 '13 at 03:25
3

$1/4=0\cdot(1/2)^0+0\cdot(1/2)^1+1\cdot(1/2)^2=0.01$ in base $2$, you just go in reverse with powers $(1/2)^n, n=0,1,2,...$

A.P.
  • 204
2

0.01 in binary is 0.25 in decimal

2

Just do long division!

11 | 10011 | 110.0101...
      1100
      ----
       111
       110
       ---
         100
          11
         ---
           100
            11
           ---
             1 (repeats)

So 10011 / 11 = 110.0101... (aka 19 / 3 = 6.33...)

Binary long division is a bit longer than decimal long division since you need more digits to write each number, but finding the largest multiple of your divisor that will fit is pretty trivial when it's either 0 or 1 times.

Joren
  • 121
2

Not sure how helpful this is but whenever I work with binary I always use a table.

So if you want 0.25 or 15.75 (base 10) in binary:

\begin{array}{r|rrrr|rrrr} & 8 & 4 & 2 & 1 & \frac{1}{2} & \frac{1}{4} & \frac{1}{8} & \frac{1}{16} \\ & 8 & 4 & 2 & 1 & 0.5 & 0.25 & 0.125 & 0.0625 \\ \hline 0.25 & 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 \\ 15.75 & 1 & 1 & 1 & 1 & 1 & 1 & 0 & 0 \end{array}

Gives 0.01 and 1111.11 respectively.

And another way:

15.375 to binary for example:

\begin{align} 15 - 8 & = 7, & \quad 7 \ge 0 & \to 1 \\ 7 - 4 & = 3, & 3 \ge 0 & \to 1 \\ 3 - 2 & = 1, & 1 \ge 0 & \to 1 \\ 1 - 1 & = 0, & 0 \ge 0 & \to 1 \\ \\ 0.375 - 0.5 & = -0.125, & -0.125 \lt 0 & \to 0 \\ 0.375 - 0.25 & = 0.125, & 0.125 \ge 0 & \to 1 \\ 0.125 - 0.125 & = 0, & 0 \ge 0 & \to 1 \end{align}

Gives 1111.011.

Dan
  • 21
1

use the euclidean algorithm, like for the integers

  • The Euclidean Algorithm is usually used to find greatest common factors and continued fractions, not to divide or convert bases. – robjohn Feb 12 '13 at 19:26
  • @robjohn: You use the euclidean algrotihm to express fractions in lowest terms. The OP asked about base 2 fractions, not other representations, so it's clear this answer is a valid response. It's also clear the OP intended to ask about other representations and just didn't use the right terms, so being pedantic is missing the point, but talking about the euclidean algorithm isn't as unrelated as you imply. – ex0du5 Feb 12 '13 at 20:46
  • @ex0du5: Since the OP mentioned converting $1/4$ to $0.25$, it seems that they are interested in converting binary fractions to the format using a binary point. Since Nicolò said "like for the integers", it really doesn't seem as if he was trying to reduce fractions to lowest terms. – robjohn Feb 12 '13 at 21:17
  • @ex0du5: I was not being pedantic; I was concerned that Nicolò was using the wrong term for the repeated-division method for converting bases (which is generally used for integers) as shown in the answers to these questions: Converting decimal (base 10) numbers to binary by repeatedly dividing by 2, Convert numbers from one base to another using repeated divisions – robjohn Feb 12 '13 at 21:19
  • @robjohn: Yes, that's exactly what I said. I said that the answer Nicolo wrote was addressing what was asked, but "It's also clear the OP intended to ask about other representations and just didn't use the right terms". I was the one who had a pedantic point that Nicolo's answer was relevant as stated. I didn't call you pedantic. I just pointed out your comment on the euclidean algorithm didn't seem to understand why it was relevant here. – ex0du5 Feb 14 '13 at 18:59