Slide 1

Slide 1 text

FLOATING 101 POINT

Slide 2

Slide 2 text

FLOATING 100.999998 POINT

Slide 3

Slide 3 text

engineers we are

Slide 4

Slide 4 text

researchers we are

Slide 5

Slide 5 text

3.14159265358979 3238462643383279 5028841971693993 7510582097494459 2307816406286208 NUMBERS WE PLAY WITH ALL DAY LONG

Slide 6

Slide 6 text

well, sometimes even at night. (yawn).

Slide 7

Slide 7 text

So, what is a floating point?

Slide 8

Slide 8 text

A floating point is ± D 1 .D 2 D 3 ···D n x Be

Slide 9

Slide 9 text

A floating point is sign ± D 1 .D 2 D 3 ···D n x Be

Slide 10

Slide 10 text

A floating point is significand ± D 1 .D 2 D 3 ···D n x Be

Slide 11

Slide 11 text

A floating point is base ± D 1 .D 2 D 3 ···D n x Be

Slide 12

Slide 12 text

A floating point is exponent ± D 1 .D 2 D 3 ···D n x Be

Slide 13

Slide 13 text

A floating point represents ± (D 1 + D 2 * B-1 + D 3 * B-2 + … + D n * B(n-1)) * Be

Slide 14

Slide 14 text

For example + 3.14 x 100 = (3 + 1*0.1 + 4*0.01)*1 = 3.14

Slide 15

Slide 15 text

The point can float ! + 3.14 x 10-1 = 0.314

Slide 16

Slide 16 text

The point can float ! + 3.14 x 10+1 = 31.4

Slide 17

Slide 17 text

What if B = 2 ? + 1.00 x 2+2 = 4.0

Slide 18

Slide 18 text

Like machines do. http://grouper.ieee.org/groups/754/

Slide 19

Slide 19 text

Normalization of floating point

Slide 20

Slide 20 text

Multiple representations + 0.01 x 22 = 1.0 + 0.10 x 21 = 1.0 + 1.00 x 20 = 1.0

Slide 21

Slide 21 text

Normalized representation + 0.01 x 22 = 1.0 + 0.10 x 21 = 1.0 + 1.00 x 20 = 1.0

Slide 22

Slide 22 text

Normalized representation + (1.)000 x 20 1 is omitted

Slide 23

Slide 23 text

Normalized representation + (1.)000 x 20 there's room for an extra digit!

Slide 24

Slide 24 text

Excess-127 representation -127 → 0 -126 → +1 … -1 → +126 0 → +127

Slide 25

Slide 25 text

#include FLT_MIN, FLT_MAX, ... #include M_PI, M_E, NAN, INFINITY, ...

Slide 26

Slide 26 text

Why no exact representation for 0.1?

Slide 27

Slide 27 text

FLOATING POINT REAL NUMBERS is used to represent

Slide 28

Slide 28 text

FLOATING POINT RATIONAL NUMBERS denotes a (finite) subset of

Slide 29

Slide 29 text

0.1 cannot be expressed as a power of 2 + ??? x 2??

Slide 30

Slide 30 text

+ 00 x 20 1 It's also a matter of precision

Slide 31

Slide 31 text

+ 01 x 20 1 1.25 It's also a matter of precision

Slide 32

Slide 32 text

+ 10 x 20 1 1.25 1.5 It's also a matter of precision

Slide 33

Slide 33 text

+ 11 x 20 1 1.25 1.5 1.75 It's also a matter of precision

Slide 34

Slide 34 text

+ 11 x 20 π/2 It's also a matter of precision

Slide 35

Slide 35 text

+ 11 x 20 π/2 It's also a matter of precision

Slide 36

Slide 36 text

+ 00 x 21 1 1.25 1.5 1.75 2.0 Not just a matter of precision or basis...

Slide 37

Slide 37 text

+ 01 x 21 1 1.25 1.5 1.75 2.0 2.5 Not just a matter of precision or basis...

Slide 38

Slide 38 text

+ 10 x 21 1 1.25 1.5 1.75 2.0 2.5 3.0 Not just a matter of precision or basis...

Slide 39

Slide 39 text

Like death and taxes rounding errors are a fact of life. http://wiki.octave.org/FAQ

Slide 40

Slide 40 text

+ 110 x 21 Operands that differ greatly + 100 x 2-2

Slide 41

Slide 41 text

+ 110000 x 21 Operands that differ greatly + 000101 x 21

Slide 42

Slide 42 text

+ 110000 x 21 Operands that differ greatly + 000101 x 21 = 110

Slide 43

Slide 43 text

No content

Slide 44

Slide 44 text

Operands that are really close + 111 x 21 - 110 x 21 = 001 x 21

Slide 45

Slide 45 text

Operands that are really close + 111 x 21 - 110 x 21 = 100 x 2-2

Slide 46

Slide 46 text

No content

Slide 47

Slide 47 text

Fixed point representation + 100.001010 = 22 + 2-3+ 2-5 = 4.15625

Slide 48

Slide 48 text

POINT WHAT'S THE WITH FLOATING

Slide 49

Slide 49 text

FP ARITHMETIC IS FAST Embedded in HW.

Slide 50

Slide 50 text

Single precision up to ~10+38. FP REPRESENTS A WIDE RANGE

Slide 51

Slide 51 text

HE APPROVES FP

Slide 52

Slide 52 text

Anyway, errors still there.

Slide 53

Slide 53 text

Okay, what about increasing the number of digits use decimal representations estimating errors think before you type

Slide 54

Slide 54 text

More digits, please! double (52 significant bits) long double (112 significant bits) arbitrary precision * * language support needed

Slide 55

Slide 55 text

Use decimal representations! decimal (C# only) BigDecimal (Java only) std::decimal (C++, coming soon)* * after IEEE-754 2008

Slide 56

Slide 56 text

Estimate the error of your algo rel_err = fabs(f – fp) / f

Slide 57

Slide 57 text

Use float to represent time float time; while (true) time += 0.20;

Slide 58

Slide 58 text

Use float to represent time float time; while (true) time += 0.20; This is BAD. And you should feel BAD.

Slide 59

Slide 59 text

Compare float numbers (a == b)

Slide 60

Slide 60 text

Compare float numbers (a == b) fabs(a -b) <= FLT_EPSILON

Slide 61

Slide 61 text

Compare float numbers (a == b) fabs(a -b) <= FLT_EPSILON fabs(a - b) <= max(fabs(a),fabs(b)) * pc

Slide 62

Slide 62 text

There is no silver bullet.

Slide 63

Slide 63 text

Use libraries (when available).

Slide 64

Slide 64 text

Vector addition (naive) float t[SIZE]; float result; for (i = 0; i < SIZE; ++i) result += t[i];

Slide 65

Slide 65 text

RESCUE GNU GSL TO THE

Slide 66

Slide 66 text

No content

Slide 67

Slide 67 text

that's all folks! @lorisfichera – https://kid-a.github.com References and source code available at https://github.com/kid-a/floating-point-seminar Credits Font: Yanone Kaffeesatz (http://www.yanone.de/typedesign/kaffeesatz/)