# Significant Digits
The significant digits of a number are those that we know with confidence. Numbers on a computer are not different.


Floating point numbers are represented in the the form
\begin{equation}
m \times b^e
\end{equation}
where $m$ is called the mantissa and represents the digits of the number, $b$ is the base (10 for decial, 2 for binary, 8 for octal...), and $e$ is the exponent.

These numbers are stored in a computer as a series of digits to represent the sign of the number, the mantissa, and the exponent, such that

This representation divides the memory into bits or registers and each digit occupies one of those bits. The digits in a number will occupy the manitssa up to however many places are allowed by the computer. For example, if a computer has a 7 bit representation where 1 bit is reserved for the sign of the number, 2 bits are reserved for the signed exponent, and 4 bits are reserved for the mantissa, then, the number 0.5468113287 is represented as:
++00547

Note how the computer had to round (or chop) the last digit. This chopping or rounding leads to round of errors (discussed later), but keep that in mind for the moment. The implications of this are very important.

## Example 1

When two floating-point numbers are added, the mantissa of the number with the smaller exponent is modified so that the exponents are the same. This will align the decimal points to make addition straightforward. Suppose we wanted to add 

$0.1557\times 10^1 + 0.4381 \times 10^{-1}$ 

on a computer that with a four digit mantissa and a one digit exponent. First the computer will align the number with the smallest exponent to the number with the larger one, such that:

$0.4381 \times 10^{-1} \to 0.004381 \times 10^1$

But this result can't fit on the mantissa, so the number either gets chopped ($0.004381 \to 0.0043$ or rounded $0.004381 \to 0.0044$). Now the addition returns $0.1600\times 10^1$.

In practice, numbers in a computer are represented using a binary system following the same idea for the exponent and mantissa discussed above.

Modern computers can deal with a huge mantissas and can represent very large and very small numbers. For a 32bit representation (default float in Python and Matlab), the mantissa is a whopping 24 bits! But still, round off errors are present!

## Example 2
Let's add a large number to a small number


```python
x = 1e18
x - 1
```




    1e+18



now try to evaluate $ x - x + 1$ and compare that to $x - (x-1)$


```python
x - x + 1
```




    1.0




```python
x - (x-1)
```




    0.0



The impact of the binary representation also has other effects. Fractions for example cannot be fully represented in binary digits and have to be roudned on chopped!
For example, the true value of 

$1/10 = 0.1 = 0.000 11 00 11 00 11 00 11 00 11 00 \ldots $

but in practice, on a 32-bit representation, this number gets chopped or rounded. Here's an example of how this could induce error.

## Example 3
In numerical methods, we often have to iterate and repeat calculations hundreds of thousands of times. This leads to accumulation of round off errors. Consider for example evaluating the sum

$\sum_1^{10^5} 10^{-5} = 10^{-5} + 10^{-5} + \ldots + 10^{-5} = 100,000 \times 10^{-5} = 1$

Perform this summation using half (np.float16), single (np.float32), double (np.float64), and triple precision (np.float128)


```python
import numpy as np # we need numpy to define the different floats
x = 0.1
s16 = s32 = s64 = s128 = 0.0
ntotal = 100000
exactval = x*ntotal
for i in range(0,ntotal):
    s16 = s16 + np.float16(x)
    s32 = s32 + np.float32(x)
    s64 = s64 + np.float64(x)    
    s128 = s128 + np.float128(x)        
print(s16)
print(s32)
print(s64)
print(s128)
```

    9997.55859375
    10000.000149011612
    10000.000000018848
    9999.999999999998721



```python
print((s16 - exactval))
print((s32 - exactval))
print((s64 - exactval))
print((s128 - exactval))
```

    -2.44140625
    0.00014901161193847656
    1.8848368199542165e-08
    -1.2789769243681803346e-12



```python
print(np.float16(1.0/3.0))
```

    0.3333



```python

```

e58a224a04bc1e5213749c431e68a82d4792c890
ipynb
Jupyter Notebook
misc/01. Numbers and Computers.ipynb
jomorodi/NumericalMethods
e040693001941079b2e0acc12e0c3ee5c917671c
MIT
2019-03-27T05:22:34.000Z
2021-01-27T10:49:13.000Z
misc/01. Numbers and Computers.ipynb
jomorodi/NumericalMethods
e040693001941079b2e0acc12e0c3ee5c917671c
MIT
misc/01. Numbers and Computers.ipynb
jomorodi/NumericalMethods
e040693001941079b2e0acc12e0c3ee5c917671c
MIT
2019-12-29T23:31:56.000Z
2021-12-28T19:04:10.000Z
Qwen/Qwen-72B
1. YES
2. YES