24
$\begingroup$

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?

$\endgroup$
6
  • 12
    $\begingroup$ The same way. $\frac{n}{m}$. So a quarter would be $\frac{1}{100}$. $\endgroup$
    – copper.hat
    Feb 12, 2013 at 18:47
  • 1
    $\begingroup$ You might want to have a look at: cs.furman.edu/digitaldomain/more/ch6/dec_frac_to_bin.htm. You can then use WA to test examples. Regards $\endgroup$
    – Amzoti
    Feb 12, 2013 at 18:49
  • $\begingroup$ I think this is a duplicate. Will have a look. $\endgroup$
    – Tara B
    Feb 12, 2013 at 18:52
  • $\begingroup$ If it is it made by another person $\endgroup$ Feb 12, 2013 at 18:54
  • 1
    $\begingroup$ Si me gustan todos esto thumbs up.... $\endgroup$ Feb 15, 2013 at 18:41

8 Answers 8

27
$\begingroup$

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}}$$

$\endgroup$
10
  • $\begingroup$ Yes I like this answer the most. $\endgroup$ Feb 12, 2013 at 18:58
  • $\begingroup$ So I I had $\frac{1}{8}$ would I do $(2^{-3}+2^{-2}+2^{-1}+2^0)$ $\endgroup$ Feb 12, 2013 at 19:00
  • 4
    $\begingroup$ Yup, but start with $\frac 18 = 2^{-3} = 0 \times 2^{-1} + 0\times 2^{-2} + 1\times 2^{-3} = 0.001_{\text{two}}$ $\endgroup$
    – amWhy
    Feb 12, 2013 at 19:02
  • $\begingroup$ yes that makes sense. $\endgroup$ Feb 12, 2013 at 19:04
  • 1
    $\begingroup$ $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. $\endgroup$
    – amWhy
    Feb 12, 2013 at 19:05
27
$\begingroup$

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 ;-)

$\endgroup$
0
5
$\begingroup$

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.

$\endgroup$
2
  • $\begingroup$ This is a thorough answer, but I don't think the OP was asking about computer implementations. $\endgroup$
    – LarsH
    Feb 12, 2013 at 21:36
  • 1
    $\begingroup$ 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. $\endgroup$
    – Kaz
    Feb 13, 2013 at 3:25
3
$\begingroup$

$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,...$

$\endgroup$
2
$\begingroup$

0.01 in binary is 0.25 in decimal

$\endgroup$
2
$\begingroup$

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.

$\endgroup$
2
$\begingroup$

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.

$\endgroup$
1
$\begingroup$

use the euclidean algorithm, like for the integers

$\endgroup$
5
  • $\begingroup$ The Euclidean Algorithm is usually used to find greatest common factors and continued fractions, not to divide or convert bases. $\endgroup$
    – robjohn
    Feb 12, 2013 at 19:26
  • $\begingroup$ @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. $\endgroup$
    – ex0du5
    Feb 12, 2013 at 20:46
  • $\begingroup$ @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. $\endgroup$
    – robjohn
    Feb 12, 2013 at 21:17
  • $\begingroup$ @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 $\endgroup$
    – robjohn
    Feb 12, 2013 at 21:19
  • $\begingroup$ @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. $\endgroup$
    – ex0du5
    Feb 14, 2013 at 18:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .