Docsity

Floating Points, Lecture Slide - Computer Science, Slides of Introduction to Computers

Partial preview of the text

Download Floating Points, Lecture Slide - Computer Science and more Slides Introduction to Computers in PDF only on Docsity!

Floating Point Arithmetic

Sept. 24, 1998

Topics

  • IEEE Floating Point Standard
  • Rounding
  • Floating Point Operations
  • Mathematical properties
  • Alpha floating point

class10.ppt

“The course that gives CMU its Zip!”

Floating Point Puzzles

  • For each of the following C expressions, either:
    • Argue that 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

Fractional Binary Numbers

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

Fractional Binary Number Examples

Value Representation

Observation

  • Divide by 2 by shifting right
  • Numbers of form 0.111111… 2 just below 1.
    • Use notation 1.0 – ε

Limitation

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

Value Representation

1/3 0.0101010101[01]… 2

1/5 0.001100110011[0011]… 2

1/10 0.0001100110011[0011]… 2

“Normalized” Numeric Values

Condition

  • exp ≠ 000 … 0 and exp ≠ 111 … 1

Exponent coded as biased value

E = Exp – Bias

  • Exp : unsigned value denoted by exp
  • Bias : Bias value » Single precision: 127 » Double precision: 1023

Mantissa coded with implied leading 1

m = 1.xxx…x 2

  • xxx…x : bits of significand
  • Minimum when 000 … 0 ( m = 1.0)
  • Maximum when 111 … 1 ( m = 2.0 – ε)
  • Get extra leading bit for “free”

Normalized Encoding Example

Value

Float F = 15213.0;

  • 1521310 = 11101101101101 2 = 1.1101101101101 2 X 2^13

Significand

m = 1.1101101101101 2 sig = (^110110110110100000000002)

Exponent

E = 13

Bias = 127 Exp = 140 = (^100011002) Floating Point Representation (Class 02): Hex: 4 6 6 D B 4 0 0 Binary: 0100 0110 0110 1101 1011 0100 0000 0000 140: 100 0110 0 15213: 1 110 1101 1011 01

Interesting Numbers

Description Exp Significand Numeric Value

Zero 00…00 00…00 0.

Smallest Pos. Denorm. 00…00 00…01 2 –^ {23,52}^ X 2–^ {126,1022}

  • Single ≈ 1.4 X 10–
  • Double ≈ 4.9 X 10–

Largest Denormalized 00…00 11…11 (1.0 – ε) X 2–^ {126,1022}

  • Single ≈ 1.18 X 10–
  • Double ≈ 2.2 X 10–

Smallest Pos. Normalized 00…01 00…00 1.0 X 2–^ {126,1022}

  • Just larger than largest denormalized

One 01…11 00…00 1.

Largest Normalized 11…10 11…11 (2.0 – ε) X 2{127,1023}

  • Single ≈ 3.4 X 10^38
  • Double ≈ 1.8 X 10^308

Memory Referencing Bug Example

main () { long int a[2]; double d = 3.14; a[2] = 1073741824; / Out of bounds reference / printf("d = %.15g\n", d); exit(0); }

main () { long int a[2]; double d = 3.14; a[2] = 1073741824; / Out of bounds reference / printf("d = %.15g\n", d); exit(0); }

Alpha MIPS Sun -g 5.30498947741318e-315 3.1399998664856 3. -O 3.14 3.14 3.

From Class 01

Referencing Bug on MIPS

MIPS -g

  • Overwrites lower 4 bytes with value 0x
  • Original value 3.14 represented as 0x40091eb851eb851f
  • Modified value represented as 0x40091eb
  • Exp = 1024 E = 1024–1023 = 1
  • Mantissa difference: .0000011eb851f 16
  • Integer value: 11eb851f 16 = 300,647,711 10
  • Difference = 21 X 2–52^ X 300,647,711 ≈ 1.34 X 10–
  • Compare to 3.140000000 – 3.139999866 = 0.

long int a[2]; double d = 3.14; a[1] a[2] = 1073741824; a[0]

d

MIPS Stack Frame (-g)

Special Values

Condition

  • exp = 111 … 1

Cases

  • exp = 111 … 1 , significand = 000 … 0

– Represents value ∞(infinity)

  • Operation that overflows
  • Both positive and negative

– E.g., 1.0/0.0 = −1.0/−0.0 = +∞, 1.0/−0.0 = −∞

  • exp = 111 … 1 , significand ≠ 000 … 0
    • Not-a-Number (NaN)
    • Represents case when no numeric value can be determined
    • E.g., sqrt(–1), ∞ − ∞
    • No fixed meaning assigned to significand bits

Floating Point Operations

Conceptual View

  • First compute exact result
  • Make it fit into desired precision
    • Possibly overflow if exponent too large
    • Possibly round to fit into significand

Rounding Modes (illustrate with $ rounding)

  • Zero $1.00 $2.00 $1.00 $2.00 –$1.
  • Nearest Even (default) $1.00 $2.00 $2.00 $2.00 –$2.

A Closer Look at Round-To-Even

Default Rounding Mode

  • Hard to get any other kind without dropping into assembly
  • All others are statistically biased
    • Sum of set of positive numbers will consistently be over- or under- estimated

Applying to Other Decimal Places

  • When exactly halfway between two possible values
    • Round so that least signficant digit is even
  • E.g., round to nearest hundredth 1.2349999 1.23 (Less than half way) 1.2350001 1.24 (Greater than half way) 1.2350000 1.24 (Half way—round up) 1.2450000 1.24 (Half way—round down)

FP Multiplication

Operands

(–1) s1^ m1 2 E (–1) s2^ m2 2 E

Exact Result

(–1) s^ m 2 E

  • Sign s : s1 ^ s
  • Mantissa m : m1 * m
  • Exponent E : E1 + E

Fixing

  • Overflow if E out of range
  • Round m to fit significand precision

Implementation

  • Biggest chore is multiplying mantissas

FP Addition

Operands

(–1) s1^ m1 2 E (–1) s2^ m2 2 E

  • Assume E1 > E

Exact Result

(–1) s^ m 2 E

  • Sign s , mantissa m :
    • Result of signed align & add
  • Exponent E : E1 – E

Fixing

  • Shift m right, increment E if m ≥ 2
  • Shift m left k positions, decrement E by k if m < 1
  • Overflow if E out of range
  • Round m to fit significand precision

(–1) s1^ m

(–1) s2^ m

E1 – E

(–1) s^ m