Mata course: Accuracy 1: multiplication and addition

1. Base 10 vs. base 2 (binary)

0
1/8 = .001
2/8 = .01
3/8 = .011
4/8 = .1
5/8 = .101
6/8 = .11
7/8 = .111

.1 = 0.00011001100110011001...
.2 = 0.0011001100110011001...
.3 = 0.010011001100110011...
.4 = 0.011001100110011001...
.5 = 0.1
.6 = 0.10011001100110011...
.7 = 0.10110011001100110...
.8 = 0.11001100110011001...
.9 = 0.11000110011001100...

2. Floating-point arithmetic (decimal)

Write number as

z = (s, x, y) = s*x*10^y

where
s = 1 or -1

0 <= x < 10

Example:

.5 = (1, 5, -1) = 5*10^(-1)

3. Fixed-precision floating-point arithmetic (decimal)

x is recorded as d digits, say 5.

Example:

.5 = (1, 5.0000, -1) = 5*10(^-1)

1/3 = (1, 3.3333, -1) = 3.3333*10^(-1)

4. Fixed-precision floating-point arithmetic (binary)

Write number as

z = (s, x, y) = s*x*2^y

where
s = 1 or -1

0 <= x < 2

x is recorded as d binary digits, say 24 (float) or 52 (double)

5. Multiplication

Method:

z1 = s1*x1*b^y1
z2 = s2*x2*b^y2

z1*z2 = s1*s2 * x1*x2*b^(y1+y2)

The x1*x2 part will have more than d digits.

Truncate or round or d digits.

Result:

In the worst case, the error is one binary digit.

If our precision is d, we have d-1 or d precision result.

6. Addition

Method:

z1 = s1*x1*10^y1
z2 = s2*x2*10^y2

Add the usual way.

Truncate or round to d digits.

Example:

12.345 + .12345:

12.345
.12345
----------
12.46845
-- ---

Result:

Given d digits of precision, our result will have between
0 and d digits of precision.

7. Summary

1.
Multiplication (and division) introduce little (next to none) inaccuracy.

2.
Addition (and subtraction) have the potential to introduce lots of
inaccuracy.

Example:

Do not code

1 - normal(z)

Code

normal(-z)

8. Implication for complex operators

1. What is said above about addition applies complex addition.

2. What is said above about addition applies to complex multiplication,
because complex multiplication involves addition:

(a + bi)(c + di) = (ac-bd) + (ad+bc)i

9. Implication for matrix operators

1. What is said above about addition applies to matrices.

2. What is said above about addition applies to matrix multiplication
because matrix multiplication involves addition.

Top