Docsity

IEEE Floating Point Standard: Representation, Operations, and Puzzles - Prof. Jian-Guo Liu, Study notes of Mathematics

Toggle sidebar
bg1
Page 1
Floating Point
Sept 5, 2002
Topics
Topics
IEEE Floating Point Standard
Rounding
Floating Point Operations
Mathematical properties
class04.ppt
15-213
“The course that gives CMU its Zip!”
– 2 – 15-213, F’02
Floating Point Puzzles
Floating Point Puzzles
For each of the following C expressions, either:
Argue that it is true for all argument values
Explain why not true
•x == (int)(float) x
•x == (int)(double) x
•f == (float)(double) f
•d == (float) d
•f == -(-f);
•2/3 == 2/3.0
•d < 0.0
((d*2) < 0.0)
•d > f
-f > -d
•d * d >= 0.0
•(d+f)-d == f
int x = …;
float f = …;
double d = …;
Assume neither
dnor fis NaN
– 3 – 15-213, F’02
IEEE Floating Point
IEEE Floating Point
IEEE Standard 754
IEEE Standard 754
Established in 1985 as uniform standard for floating point
arithmetic
Before that, many idiosyncratic formats
Supported by all major CPUs
Driven by Numerical Concerns
Driven by Numerical Concerns
Nice standards for rounding, overflow, underflow
Hard to make go fast
Numerical analysts predominated over hardware types in
defining standard
– 4 – 15-213, F’02
Fractional Binary Numbers
Fractional Binary Numbers
Representation
Representation
Bits to right of “binary point” represent fractional powers of 2
Represents rational number:
bibi–1 b2b1b0b–1 b–2 b–3 b–j
• • •• • • .
1
2
4
2i–1
2i
• • •
• • •
1/2
1/4
1/8
2–j
bk⋅2k
k=− j
i
pf3
pf4
pf5
pf8

Partial preview of the text

Download IEEE Floating Point Standard: Representation, Operations, and Puzzles - Prof. Jian-Guo Liu and more Study notes Mathematics in PDF only on Docsity!

Floating Point

Sept 5, 2002

TopicsTopics

 IEEE Floating Point Standard  Rounding  Floating Point Operations  Mathematical properties

class04.ppt

“The course that gives CMU its Zip!”

  • 2 – 15-213, F’

Floating Point Puzzles Floating Point Puzzles

 For each of the following C expressions, either:  Argue that it is true for all argument values  Explain why not true

  • x == (int)(float) x
  • x == (int)(double) x
  • f == (float)(double) f
  • d == (float) d
  • f == -(-f);
  • 2/3 == 2/3.
  • d < 0.0  ((d2) < 0.0)*
  • d > f  -f > -d
  • d * d >= 0.
  • (d+f)-d == f

int x = …; float f = …; double d = …;

Assume neither d nor f is NaN

  • 3 – 15-213, F’

IEEE Floating Point IEEE Floating Point

IEEE Standard 754IEEE Standard 754

 Established in 1985 as uniform standard for floating point arithmetic  Before that, many idiosyncratic formats  Supported by all major CPUs

Driven by Numerical ConcernsDriven by Numerical Concerns

 Nice standards for rounding, overflow, underflow  Hard to make go fast  Numerical analysts predominated over hardware types in defining standard

  • 4 – 15-213, F’

Fractional Binary Numbers Fractional Binary Numbers

Representation Representation

 Bits to right of “binary point” represent fractional powers of 2  Represents rational number:

bi bi –1 • • • b 2 b 1 b 0. b –1 b –2 b –3 • • • b – j

2 i –

2 i

2 – j

bk ⋅ 2 k k =− j

i

  • 5 – 15-213, F’

Frac. Binary Number Examples Frac. Binary Number Examples

ValueValue RepresentationRepresentation

ObservationsObservations

 Divide by 2 by shifting right  Multiply by 2 by shifting left  Numbers of form 0.111111… 2 just below 1. 1/2 + 1/4 + 1/8 + … + 1/2 i^ + … →→→→ 1. Use notation 1.0 – εεεε

  • 6 – 15-213, F’

Representable NumbersRepresentable Numbers

Limitation Limitation

 Can only exactly represent numbers of the form x /2 k  Other numbers have repeating bit representations

Value Value RepresentationRepresentation

1/3 0.0101010101[01]… 2

1/5 0.001100110011[0011]… 2

1/10 0.0001100110011[0011]… 2

  • 7 – 15-213, F’

Numerical FormNumerical Form

 – 1 s^ M 2 E Sign bit s determines whether number is negative or positive Significand M normally a fractional value in range [1.0,2.0). Exponent E weights value by power of two

EncodingEncoding

 MSB is sign bit  exp field encodes E  frac field encodes M

Floating Point Representation Floating Point Representation

s exp frac

  • 8 – 15-213, F’

Encoding Encoding

 MSB is sign bit  exp field encodes E  frac field encodes M

Sizes Sizes

 Single precision: 8 exp bits, 23 frac bits 32 bits total  Double precision: 11 exp bits, 52 frac bits 64 bits total  Extended precision: 15 exp bits, 63 frac bits Only found in Intel-compatible machines Stored in 80 bits » 1 bit wasted

Floating Point Precisions Floating Point Precisions

s exp frac

  • 13 – 15-213, F’

Summary of Floating Point

Real Number Encodings

Summary of Floating Point

Real Number Encodings

NaN^ NaN

-Normalized -Denorm +Denorm +Normalized

  • 14 – 15-213, F’

Tiny Floating Point Example Tiny Floating Point Example

88 - -bit Floating Point Representationbit Floating Point Representation

 the sign bit is in the most significant bit.  the next four bits are the exponent, with a bias of 7.  the last three bits are the frac

  Same General Form as IEEE FormatSame General Form as IEEE Format

 normalized, denormalized  representation of 0, NaN, infinity

s exp^ frac

76 32 0

  • 15 – 15-213, F’

Values Related to the Exponent Values Related to the Exponent

Exp exp E 2 E

0 0000 -6 1/64 (denorms) 1 0001 -6 1/ 2 0010 -5 1/ 3 0011 -4 1/ 4 0100 -3 1/ 5 0101 -2 1/ 6 0110 -1 1/ 7 0111 0 1 8 1000 +1 2 9 1001 +2 4 10 1010 +3 8 11 1011 +4 16 12 1100 +5 32 13 1101 +6 64 14 1110 +7 128 15 1111 n/a (inf, NaN)

  • 16 – 15-213, F’

Dynamic Range Dynamic Range

s exp frac E Value

0 0000 000 -6 0 0 0000 001 -6 1/81/64 = 1/ 0 0000 010 -6 2/81/64 = 2/ … 0 0000 110 -6 6/81/64 = 6/ 0 0000 111 -6 7/81/64 = 7/ 0 0001 000 -6 8/81/64 = 8/ 0 0001 001 -6 9/81/64 = 9/ … 0 0110 110 -1 14/81/2 = 14/ 0 0110 111 -1 15/81/2 = 15/ 0 0111 000 0 8/81 = 1 0 0111 001 0 9/81 = 9/ 0 0111 010 0 10/81 = 10/ … 0 1110 110 7 14/8128 = 224 0 1110 111 7 15/8128 = 240 0 1111 000 n/a inf*

closest to zero

largest denorm smallest norm

closest to 1 below

closest to 1 above

largest norm

Denormalized numbers

Normalized numbers

  • 17 – 15-213, F’

Distribution of Values Distribution of Values

66 - -bit IEEEbit IEEE--like formatlike format

 e = 3 exponent bits  f = 2 fraction bits  Bias is 3

Notice how the distribution gets denser toward zero.Notice how the distribution gets denser toward zero.

Denormalized Normalized Infinity

  • 18 – 15-213, F’

Distribution of Values

(close-up view)

Distribution of Values

(close-up view)

66 - -bit IEEEbit IEEE--like formatlike format

 e = 3 exponent bits  f = 2 fraction bits  Bias is 3

Denormalized Normalized Infinity

  • 19 – 15-213, F’

Interesting Numbers Interesting Numbers

DescriptionDescription expexp fracfrac Numeric ValueNumeric Value

ZeroZero^ 00…0000…00^ 00…0000…00^ 0.00. Smallest Pos.Smallest Pos. DenormDenorm.. 00…0000…00 00…0100…01 22 – –^ {23,52}{23,52}^ X 2X 2––^ {126,1022}{126,1022}  Single ≈≈≈≈ 1.4 X 10–  Double ≈ 4.9 X 10–

LargestLargest DenormalizedDenormalized 00…0000…00 11…1111…11 (1.0(1.0 – – εε) X 2) X 2––^ {126,1022}{126,1022}  Single ≈≈≈≈ 1.18 X 10–  Double ≈ 2.2 X 10–

Smallest Pos. NormalizedSmallest Pos. Normalized^ 00…0100…01^ 00…0000…00^ 1.0 X 21.0 X 2––^ {126,1022}{126,1022}  Just larger than largest denormalized OneOne 01…1101…11 00…0000…00 1.01. Largest NormalizedLargest Normalized 11…1011…10 11…1111…11 (2.0(2.0 – – εε) X 2) X 2{127,1023}{127,1023}  Single ≈≈≈≈ 3.4 X 10^38  Double ≈ 1.8 X 10^308

  • 20 – 15-213, F’

Special Properties of Encoding Special Properties of Encoding

FP Zero Same as Integer Zero FP Zero Same as Integer Zero

 All bits = 0

Can (Almost) Use Unsigned Integer Comparison Can (Almost) Use Unsigned Integer Comparison

 Must first compare sign bits  Must consider -0 = 0  NaNs problematic  Will be greater than any other values  What should comparison yield?  Otherwise OK  Denorm vs. normalized  Normalized vs. infinity

  • 25 – 15-213, F’

FP Addition FP Addition

OperandsOperands

(–1) s1^ M1 2 E (–1) s2^ M2 2 E  Assume E1 > E

Exact ResultExact Result

(–1) s^ M 2 E  Sign s , significand M :  Result of signed align & add  Exponent E : E

FixingFixing

 If M  2, shift M right, increment E  if M < 1, shift M left k positions, decrement E by k  Overflow if E out of range  Round M to fit frac precision

(–1) s1^ M

(–1) s2^ M

E1 – E

(–1) s^ M

  • 26 – 15-213, F’

Mathematical Properties of FP Add Mathematical Properties of FP Add

Compare to those of Compare to those of AbelianAbelian GroupGroup

 Closed under addition? YES But may generate infinity or NaN  Commutative? YES  Associative? NO Overflow and inexactness of rounding  0 is additive identity? YES  Every element has additive inverse ALMOST Except for infinities & NaNs

Monotonicity Monotonicity

 a  b  a + c  b + c? ALMOST Except for infinities & NaNs

  • 27 – 15-213, F’

Math. Properties of FP Mult Math. Properties of FP Mult

Compare to Commutative RingCompare to Commutative Ring

 Closed under multiplication? YES But may generate infinity or NaN  Multiplication Commutative? YES  Multiplication is Associative? NO Possibility of overflow, inexactness of rounding  1 is multiplicative identity? YES  Multiplication distributes over addition? NO Possibility of overflow, inexactness of rounding

MonotonicityMonotonicity

 a  b & c  0  a * c  b * c? ALMOST Except for infinities & NaNs

  • 28 – 15-213, F’

Floating Point in C Floating Point in C

C Guarantees Two LevelsC Guarantees Two Levels

float single precision double double precision

ConversionsConversions

 Casting between int , float , and double changes numeric values  Double or float to int  Truncates fractional part  Like rounding toward zero  Not defined when out of range » Generally saturates to TMin or TMax  int to double  Exact conversion, as long as int has  53 bit word size  int to float  Will round according to rounding mode

  • 29 – 15-213, F’

Answers to Floating Point Puzzles Answers to Floating Point Puzzles

  • x == (int)(float) x
  • x == (int)(double) x
  • f == (float)(double) f
  • d == (float) d
  • f == -(-f);
  • 2/3 == 2/3.
  • d < 0.0  ((d2) < 0.0)*
  • d > f  -f > -d
  • d * d >= 0.
  • (d+f)-d == f

int x = …; float f = …; double d = …;

Assume neither d nor f is NAN

  • x == (int)(float) x No: 24 bit significand
  • x == (int)(double) x Yes: 53 bit significand
  • f == (float)(double) f Yes: increases precision
  • d == (float) d No: loses precision
  • f == -(-f); Yes: Just change sign bit
  • 2/3 == 2/3.0 No: 2/3 == 0
  • d < 0.0  ((d2) < 0.0)* Yes!
  • d > f  -f > -d Yes!
  • d * d >= 0.0 Yes!
  • (d+f)-d == f No: Not associative
    • 30 – 15-213, F’

Ariane 5 Ariane 5

 Exploded 37 seconds after liftoff  Cargo worth $500 million

Why Why

 Computed horizontal velocity as floating point number  Converted to 16-bit integer  Worked OK for Ariane 4  Overflowed for Ariane 5  Used same software

  • 31 – 15-213, F’

Summary Summary

IEEE Floating Point Has Clear Mathematical PropertiesIEEE Floating Point Has Clear Mathematical Properties

 Represents numbers of form M X 2 E  Can reason about operations independent of implementation  As if computed with perfect precision and then rounded  Not the same as real arithmetic  Violates associativity/distributivity  Makes life difficult for compilers & serious numerical applications programmers