Introduction
Theory
HOWTO
Examples
Matlab
Maple

# Introduction

The use of a decimal numbering system is based on the average
human having ten fingers. Outside of this minor benefit, the decimal
number system has a number of drawbacks, the greatest being that ten
is divisible by only two primes, 2 and 5, and it is neither divisible
by 3 nor 4. (Of course, it would be absurd to suggest anything else
at this time.)

This topic covers decimal representation and arithmetic so that you
may compare it to binary representation and arithmetic.

# Background

None.

# References

# Interactive Maplet

A Fixed-Precision Decimal Calculator
# Theory

# Assumptions

We use ten ordered decimal digits, 0, 1, 2, ..., 9 to represent numbers. An (*n* + 1)-digit decimal integer
is a number of the form *d*_{n}⋅⋅⋅*d*_{2}*d*_{1}*d*_{0}
where each *d*_{i} is a decimal digit and *d*_{n} ≠ 0. This
represents the number

When a digit *d*_{i} = 9 is incremented, it is replaced by
0 and the digit *d*_{i + 1} is incremented by 1.

The digit *d*_{n} is called the *most-significant
digit* and the digit *d*_{0} is called the *least-significant
digit*.

Using these definitions, we derive the basic rules for decimal addition and
multiplication.

We may generalize this to represent arbitrary real numbers by including a decimal
point and allowing numbers of the form
*d*_{n}⋅⋅⋅*d*_{0}.*d*_{-1}*d*_{-2}⋅⋅⋅
which represents the number

In general, we will truncate such a number to the form:

where *m* < *n*. We will usually write such a number as
*d*_{n}.*d*_{n − 1}...*d*_{m} × 10^{n} or
`d`_{n}.d_{n − 1}...d_{m}en. This is often referred to as *scientific notation*. For example, 123.456
may be represented by 1.23456 × 10^{2} or `1.23456e2`.

Again, using this definition of a real number, we may derive rules for
decimal addition and multiplication.

## Terminology

Another term to describe number which is represented as a decimal number is to state
that it is written in *base ten* (or *base 10*).

# HOWTO

# Problem

Given two decimal numbers, add and multiply them.

# Assumptions

We will assume that the number of digits is finite. These rules can be
extended for an infinite number of digits, but this is unnecessary.

# Addition

Line up the decimal points and add each column, starting from the right and moving left.
If a digit is missing, assume it is zero. If a column sums to more than 9, write down the
least-significant digit and *carry* the remaining digits to the next column. Continue
until all columns, including any carries, have been added.

For example, 3.25 + 18.7 is calculated as follows:

1
3.25
+ 18.7
-----
21.95

# Multiplication

Given two decimal numbers, total the number of digits to the right
of the decimal points. Next, remove the decimal points and multiply the two
numbers as **integers**. Starting with the least significant digit *d*_{0} of the
second integer, multiply the first integer by that digit. Next, for each
higher digit *d*_{i} in the second integer, multiply
the first integer by *d*_{i}⋅10^{i}.
Finally, sum these products.

For example, 3.25 × 18.7 has three digits to the right of the
decimal point and thus we calculate:

24
13
325
× 187
---
1 carry for sum
2275 325 × 7
26000 325 × 80
32500 325 × 100
-----
60775

Adding back 3 digits to the right of the decimal point, we get 60.775.

Note that the zeros are often omitted for convenience:

24
13
325
× 187
---
1
2275
2600 ←
325 ←
-----
60775

# Examples

1. Add the decimal numbers 6.152 and 96.47 .

11 1 carry
6.152
+ 96.47
------
102.622

Lining up the decimal points, each column is added. A blank is interpreted as
a zero and the columns are added from right to left. If a column adds to more
than 9, the following digits are *carried* to the next column.

2. Multiply the decimal numbers 3.523 and 80.13

2412 carry for 8 × 3523
carry for 0 × 3523
carry for 1 × 3523
11 carry for 3 × 3523
3523
× 8013
----
1 carry for sum
10569
3523
0000
+ 28184
--------
28229799

Adding the number of decimal places (3 + 2 = 5) and using this offset, we get the result 282.29799 .

3. Represent the number 523.2345 in scientific notation.

This number is 500 + ⋅⋅⋅ or 5 × 10^{2}, and thus
we may write it as 5.232345 × 10^{2} or `5.232345e2`

4. What is the problem if we don't use scientific notation to represent `1.23e10`?

Using 12300000000, by convention, suggests that the maximum absolute error is 0.5 which
has a maximum relative error of 4.1 × 10^{-11}, while
the original scientific notation suggests the maximum absolute error is 50000000, which
has a maximum relative error of 0.0041.

# Matlab

Matlab uses binary (base 2) to store approximation of real numbers;
however, all integers up to 2^{53} (nine quadrillion) may be stored exactly. This
exact storage does not extend to all real numbers as is demonstrated here:

>> format long
>> 48 - 41
ans = 7
>> 51 - 44
ans = 7
>> 4.8 - 4.1
ans = 0.700000000000000
>> 5.1 - 4.4
ans = 0.699999999999999

# Maple

Maple already works with decimal numbers, so the following are
quite natural in Maple:

3.235 + 9.323 + 83.323;

Note that (by default) Maple uses 10 decimal digits of precision when performing
decimal arithmetic.