




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





IEEE Floating Point Standard Rounding Floating Point Operations Mathematical properties
class04.ppt
For each of the following C expressions, either: Argue that it is true for all argument values Explain why not true
int x = …; float f = …; double d = …;
Assume neither d nor f is NaN
Established in 1985 as uniform standard for floating point arithmetic Before that, many idiosyncratic formats Supported by all major CPUs
Nice standards for rounding, overflow, underflow Hard to make go fast Numerical analysts predominated over hardware types in defining standard
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
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 – εεεε
Can only exactly represent numbers of the form x /2 k Other numbers have repeating bit representations
– 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
MSB is sign bit exp field encodes E frac field encodes M
s exp frac
MSB is sign bit exp field encodes E frac field encodes M
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
s exp frac
NaN^ NaN
-Normalized -Denorm +Denorm +Normalized
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
normalized, denormalized representation of 0, NaN, infinity
s exp^ frac
76 32 0
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)
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
e = 3 exponent bits f = 2 fraction bits Bias is 3
Denormalized Normalized Infinity
e = 3 exponent bits f = 2 fraction bits Bias is 3
Denormalized Normalized Infinity
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
All bits = 0
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
(–1) s1^ M1 2 E (–1) s2^ M2 2 E Assume E1 > E
(–1) s^ M 2 E Sign s , significand M : Result of signed align & add Exponent E : E
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
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
a b a + c b + c? ALMOST Except for infinities & NaNs
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
a b & c 0 a * c b * c? ALMOST Except for infinities & NaNs
float single precision double double precision
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
int x = …; float f = …; double d = …;
Assume neither d nor f is NAN
Exploded 37 seconds after liftoff Cargo worth $500 million
Computed horizontal velocity as floating point number Converted to 16-bit integer Worked OK for Ariane 4 Overflowed for Ariane 5 Used same software
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