# How to add or multiply decimal numbers?

The tag ''calculus'' may not fit for this question but I couldn't think of anything else. Please feel free to change it.

Suppose $a=0,a_{-1}a_{-2}a_{-3}\ldots$ and $b=0,b_{-1}b_{-2}b_{-3}\ldots$ are two decimal numbers. To be precise, I should have add ''representation of'' before ''decimal numbers'' but this is not of what the question is about.

How can I practically add and multiply $a$ and $b$?

If $a$ and $b$ are rational and thus periodic, you could answer, that I should convert them to fractions $a={a'\over a''}$ and $b={b'\over b''}$ and do the obvious thing, but I don't want to do this.

Starting with addition, the problem is about digit sums bigger then $10$. If $a=b=0,111\ldots$ you have no problems adding them digit by digit from the left to $a+b=0,222\ldots$. But when you have something like $a=0,0888\ldots$ and $0,0111\ldots$ you can't write down any digit of the sum until you know what comes next if you start from the left since there may occur a digit sum $>10$ as with $a=0,088890\ldots$ and $0,011110\ldots$, can you? What happens here exactly, if $a$ and $b$ are rational? How does the length of the period of $a+b$ relies to the period lengths of $a$ and $b$?

The problem gets more complicated, if you think of multiplication. For ''finite'' numbers like $a=b=0,2=0,200\ldots$ you just write down a multiplication table and that's it (despite the fact, that $0,2=0,1999\ldots$, but let's ignore this). But how about arbitrary or rational numbers? I have the impression, that the length of the period may be much much bigger in the product. Is there an upper bound depending on the period lengths of $a$ and $b$?

As you define a decimal number as a certain absolutely convergent row, you define the multiplication by the Cauchy product, but this doesn't help when you want to do multiplication practically. Is there an easy algorithm?

Here is a concrete challenge: Multiply $a=b=0,\overline{142857}$.

-
Good problem. The difficuties you mention are one reason that in the formal definition of real number, we do not use the seemingly sensible infinite decimal. Beside, it might upset the Martians, who have $17$ fingers and would think the definition is earthist. – André Nicolas May 29 '12 at 21:12
$\TeX$ assumes the use of decimal points; decimal commas, as used in some countries, are treated as punctuation and thus lead to spurious spaces. – joriki May 29 '12 at 21:15
Given a fraction $\frac{p}{q}$ let $q'$ be the number obtained from $q$ by removing all factors of $2$ and $5$. Then the period of the decimal expansion of $\frac{p}{q}$ divides the value of the Carmichael function $\lambda(q)$ (en.wikipedia.org/wiki/Carmichael_function). So when adding or multiplying the period won't be substantially larger than the product of the periods of the individual terms, and will often be smaller. – Qiaochu Yuan May 29 '12 at 21:15
The common way of adding decimal numbers is to use "excess-6" arithmetic, where 6 is added if the digit exceeds 9. There are various clever ways to do this, some more "clever" than they should be, but since the operation tends to be highly optimized the cleverness is usually merited. – Daniel R Hicks May 29 '12 at 21:16
@Daniel: You are talking about arithmetic operations on Binary-Coded Decimal (BCD) numbers, which have nothing to do with the subject of the OP. – TonyK May 29 '12 at 21:20

The problems you mention are, in a strict sense, insurmountable. In general, there is no way to compute the first $n$ decimal digits of $a+b$ from the first $m$ decimal digits of $a$ and $b$, however big $m$ may be with respect to $n$. For example, if $a = 0.444444...$ and $b = 0.555555...$, then $a+b$ may be $0.999999...$ or $1.000000....$ depending on the first digit that deviates from the pattern; but without knowing more about $a$ and $b$, the first deviating digit could be anywhere.

That's the bad news. The good news is that you can compute $a+b$ and $a \times b$ to any required accuracy, if you can compute $a$ and $b$ to any required accuracy. But you have to use a different representation system; for instance, rational approximations.

In the special case that you give, decimal representation works just fine, because the result (1/49) is a recurring, non-terminating decimal. So you can compute the successive digits just using high-school multiplication.

-
This comes up as a practical matter in certain types of computation. For example, one sometimes represents real numbers in the computer as lazily-evaluated continued fractions. In this representation, it is quite simple to have an "exact" representation of $\sqrt2$ from which one can exatract arbitrarily many decimal digits. But multiplying $\sqrt2\cdot\sqrt2$ enters an unproductive infinite loop in which the computer calculates forever, trying to decide whether the integer part of the result is 1 or 2. – MJD May 29 '12 at 22:06
Which is why I suggested rational approximations! – TonyK May 29 '12 at 22:13