Lecture 1: Floating-point arithmetic, vector norms

Syllabus

Today:

Tomorrow: Matrix norms and unitary matrices

Representation of numbers

Fixed point

Floating point

The numbers in computer memory are typically represented as floating point numbers

A floating point number is represented as

$$\textrm{number} = \textrm{significand} \times \textrm{base}^{\textrm{exponent}},$$

where significand is integer, base is positive integer and exponent is integer (can be negative), i.e.

$$ 1.2 = 12 \cdot 10^{-1}.$$

Floating point: formula

$$f = (-1)^s 2^{(p-b)} \left( 1 + \frac{d_1}{2} + \frac{d_2}{2^2} + \ldots + \frac{d_m}{2^m}\right),$$

where $s \in \{0, 1\}$ is the sign bit, $d_i \in \{0, 1\}$ is the $m$-bit mantissa, $p \in \mathbb{Z}; 0 \leq p \leq 2^e$, $e$ is the $e$-bit exponent, commonly defined as $2^e - 1$

Can be thought as a uniform $m$-bit grid between two sequential powers of $2$.

Fixed vs Floating

Q: What are the advantages/disadvantages of the fixed and floating points?

A: In most cases, they work just fine.

IEEE 754

In modern computers, the floating point representation is controlled by IEEE 754 standard which was published in 1985 and before that point different computers behaved differently with floating point numbers.

IEEE 754 has:

Possible values are defined with

and have the following restrictions

The two most common format, single & double

The two most common formats, called binary32 and binary64 (called also single and double formats). Recently, the format binary16 plays important role in learning deep neural networks.

Name Common Name Base Digits Emin Emax
binary16 half precision 2 11 -14 + 15
binary32 single precision 2 24 -126 + 127
binary64 double precision 2 53 -1022 +1023

Examples

Q: what about -infinity and NaN ?

Accuracy and memory

The relative accuracy of single precision is $10^{-7}-10^{-8}$, while for double precision is $10^{-14}-10^{-16}$.

Crucial note 1: A float16 takes 2 bytes, float32 takes 4 bytes, float64, or double precision, takes 8 bytes.

Crucial note 2: These are the only two floating point-types supported in hardware (float32 and float64) + GPU/TPU different float types are supported.

Crucial note 3: You should use double precision in computational science and engineering and float on GPU/Data Science.

Also, half precision can be useful in training deep neural network, see this paper.

How does number representation format affect training of neural networks (NN)?

Plots are taken from this paper

bfloat16 (Brain Floating Point)

Tensor Float from Nvidia (blog post about this format)

Mixed precision (docs from Nvidia)

Alternative to the IEEE 754 standard

Issues in IEEE 754:

Concept of posits can replace floating point numbers, see this paper

Division accuracy demo

Square root accuracy demo

Exponent accuracy demo

Summary of demos

Loss of significance

Summation algorithm

However, the rounding errors can depend on the algorithm.

$$S = \sum_{i=1}^n x_i = x_1 + \ldots + x_n.$$

Naïve algorithm

Naïve algorithm adds numbers one-by-one:

$$y_1 = x_1, \quad y_2 = y_1 + x_2, \quad y_3 = y_2 + x_3, \ldots.$$