Numeric Testing
To test numeric functions we should use cases like so:
| Fcalc(x) - Ftrue(x) | < tolerance
We can calculate Ftrue
using an arbitrary precision maths package.
This is as there are inherent losses with floating point numbers so our calculated value may be off (within reason).
Tolerance
We can calculate the tolerance with the following function:
\[\text{Tolerance} \leq \text{Cn}(x) \times \text{ULP}\]Where:
- $\text{ULP}$ - Unit of least precision which is dependant on the data type used.
- $\text{Cn}(x)$ - Function of the condition number where the greater the condition number, the greater the effect of loss in precision.
Types of Function Test
Golden Value
These are particular values which are proven mathematically such as:
- $\sin(\pi) = 0$
- $\log(1) = 0$
We can test these using the tolerance calculation above.
Identities
This is a case where we use multiple functions in conjunction to produce an output:
\[\sin(x)^2+\cos(x)^2=1\]Using identities may hide compensated error (where the functions are equally wrong in the opposite direction).
Inverse Function Tests
These are similar to identities:
\[x = (\sqrt x)^2\]Unlike identities, they test for accuracy as in the previous example - $\sin(x)=1$ and $\cos(x)=0$, while still passing the test.
Binary Number Represenatations
8-bit Float
Consider that we are storing a decimal number in an 8-bit float:
sign | exponent | significand | |||||
---|---|---|---|---|---|---|---|
0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
We can calculate the error when storing 0.1
:
0 1111 001
This is stored perfectly.
Or calculate the error when storing 0.25
:
0 1111 011
We cannot store 25 in the 3 bit significand so the closes digit is 3. This gives an error of 0.05.
Fixed Point Binary
We can also use a fixed point representation like so:
$2^2$ | $2^1$ | $2^0$ | $2^{-1}$ | $2^{-2}$ | $2^{-3}$ | $2^{-4}$ | $2^{-5}$ |
---|---|---|---|---|---|---|---|
1 | 0 | 1 | 0 | 0 | 1 | 1 | 0 |
This can also be written as:
\[1101\ .\ 0011_2\]We can calculate the error of storing 0.1 in 8 bits:
\[0\ .\ 0001\ 1010_2 = 0.1015625\]We have to round to fit this in 8 bits.
\[\vert0.1 - 0.1015625\vert = 1.5625{\times}10^{-3}\]Rounding in Binary
- If the next digit is 0 then round down (store as truncated).
- If the next digit is 1 then round up.