CHAPTER 3 Floating Point. Representation of Fractions (1/2) With base 10, we have a decimal point to...

43
CHAPTER 3 Floating Point

Transcript of CHAPTER 3 Floating Point. Representation of Fractions (1/2) With base 10, we have a decimal point to...

Page 1: CHAPTER 3 Floating Point. Representation of Fractions (1/2) With base 10, we have a decimal point to separate integer and fraction parts to a number.

CHAPTER 3

Floating Point

Page 2: CHAPTER 3 Floating Point. Representation of Fractions (1/2) With base 10, we have a decimal point to separate integer and fraction parts to a number.

Representation of Fractions (1/2)

• With base 10, we have a decimal point to separate integer and fraction parts to a number.

20.4005 = 2x101 + 4x10-1 + 5x10-4

xx.yyyy101

100 10-1 10-2 10-3 10-4

Page 3: CHAPTER 3 Floating Point. Representation of Fractions (1/2) With base 10, we have a decimal point to separate integer and fraction parts to a number.

Representation of Fractions (2/2)“Binary Point” like decimal point signifies boundary between integer and fractional parts:xx.yyyy

2120 2-1 2-2 2-3 2-4

Example 6-bit representation:

10.10102 = 1x21 + 1x2-1 + 1x2-3 = 2.62510

If we assume “fixed binary point”, range of 6-bit representations with this format:

0 to 3.9375 (almost 4)

Page 4: CHAPTER 3 Floating Point. Representation of Fractions (1/2) With base 10, we have a decimal point to separate integer and fraction parts to a number.

Fractional Powers of 2

0 1.0 11 0.5 1/22 0.25 1/43 0.125 1/84 0.0625 1/165 0.03125 1/326 0.0156257 0.00781258 0.003906259 0.00195312510 0.000976562511 0.0004882812512 0.00024414062513 0.000122070312514 0.0000610351562515 0.000030517578125

i 2-i

Page 5: CHAPTER 3 Floating Point. Representation of Fractions (1/2) With base 10, we have a decimal point to separate integer and fraction parts to a number.

Representation of Fractions with Fixed Pt.

What about addition and multiplication?

Addition is straightforward:

01.100 1.510

00.100 0.510

10.000 2.010

Multiplication a bit more complex:

01.100 1.510

00.100 0.510

00 000 000 00 0110 0 00000 000000000110000

Whole Fraction

Where’s the answer, 0.11? (need to remember where point is)

Page 6: CHAPTER 3 Floating Point. Representation of Fractions (1/2) With base 10, we have a decimal point to separate integer and fraction parts to a number.

Representation of FractionsSo far, in our examples we used a “fixed” binary point what we really want is to “float” the binary point. Why?Floating binary point most effective use of our limited bits

(and thus more accuracy in our number representation):

… 000000.001010100000…

Any other solution would lose accuracy!

example: put 0.1640625 into binary. Represent with 5-bits, choosing where to put the binary point.

Store these bits and keep track of the binary point 2 places to the left of the MSB

With floating point rep., each number carries an exponent field recording the location of its binary point.

The binary point is not stored, so very large and very small numbers can be represented.

Page 7: CHAPTER 3 Floating Point. Representation of Fractions (1/2) With base 10, we have a decimal point to separate integer and fraction parts to a number.

Scientific Notation (in Decimal)

6.0210 x 1023

radix (base)decimal point

significand exponent

• Normalized form: no leadings 0s (exactly one non-zero digit to left of decimal point)

• Alternatives to representing 1/1,000,000,000

–Normalized: 1.0 x 10-9

–Not normalized: 0.1 x 10-8,10.0 x 10-10

Page 8: CHAPTER 3 Floating Point. Representation of Fractions (1/2) With base 10, we have a decimal point to separate integer and fraction parts to a number.

Scientific Notation (in Binary)

1.02 x 2-1

radix (base)“binary point”

exponent

• Computer arithmetic that supports binary scientific notation is called floating point.

• Corresponds to C++ data types

– float, double

significand

Page 9: CHAPTER 3 Floating Point. Representation of Fractions (1/2) With base 10, we have a decimal point to separate integer and fraction parts to a number.

Floating Point Representation (1/2)

• Normalized: s1.xxxx…xxx2*2yyy…y2

• Word Sizes (32 or 64 bits)

031S Exponent30 23 22

Significand

1 bit 8 bits 23 bits

• S represents sign

• binary Exponent yyyy

• binary significand/fraction -- xxxx

• Represent numbers as small as 2.0 x 10-38 to as large as 2.0 x 1038

Page 10: CHAPTER 3 Floating Point. Representation of Fractions (1/2) With base 10, we have a decimal point to separate integer and fraction parts to a number.

Floating Point Representation (2/2)• What if result too large?

(> 2.0x1038 , < -2.0x1038 )– Overflow! Positive exponent larger than

represented in 8-bit exponent field

• What if result too small? (>0 & < 2.0x10-38 , <0 & > - 2.0x10-38 )– Underflow! Negative exponent larger than

represented in 8-bit exponent field

• What would help reduce chances of overflow and/or underflow? (more bits)

0 2x10-38 2x10381-1 -2x10-38-2x1038

underflow overflowoverflow

Page 11: CHAPTER 3 Floating Point. Representation of Fractions (1/2) With base 10, we have a decimal point to separate integer and fraction parts to a number.

Double Precision Representation• Next Multiple of Word Size (64 bits)

• Double Precision (vs. Single Precision)– C++ variable declared as double– Represent numbers almost as small as

2.0 x 10-308 to almost as large as 2.0 x 10308 – But primary advantage is greater accuracy

(#digits) due to larger significand

031S Exponent

30 20 19Significand

1 bit 11 bits 20 bitsSignificand (cont’d)

32 bits

Page 12: CHAPTER 3 Floating Point. Representation of Fractions (1/2) With base 10, we have a decimal point to separate integer and fraction parts to a number.

IEEE 754 Floating Point Standard (1/2)Single Precision (DP similar):

• Sign bit: 1 means negative, 0 means positive

• Significand:– To pack more bits, leading 1 implied for normalized

numbers– 1 + 23 bits single, 1 + 52 bits double

– always true when normalized: 0 < significand < 1

• Note: zero has no leading 1, so reserve exponent value 0 just for number ZERO

031S Exponent30 23 22

Significand

1 bit 8 bits 23 bits

Page 13: CHAPTER 3 Floating Point. Representation of Fractions (1/2) With base 10, we have a decimal point to separate integer and fraction parts to a number.

IEEE 754 Floating Point Standard (2/2)• Biased Exponent Notation: bias is subtracted to

get real exponent value

– Uses bias of 127 for single prec.

– Subtract 127 from Exponent field to get actual value

– Uses 1023 bias for double precision

– Bias = 2(#expbits – 1) – 1

031S Exponent30 23 22

Significand

1 bit 8 bits 23 bits• Value = (-1)S x (1 + Significand) x 2(Exponent-127)

Page 14: CHAPTER 3 Floating Point. Representation of Fractions (1/2) With base 10, we have a decimal point to separate integer and fraction parts to a number.

Example: Converting Binary FP to Decimal

• Sign: 0 => positive

• Exponent: – 0110 1000two = 104ten

– Bias adjustment: 104 - 127 = -23

• Significand:1 + 1x2-1+ 0x2-2 + 1x2-3 + 0x2-4 + 1x2-5 +...

=1+2-1+2-3 +2-5 +2-7 +2-9 +2-14 +2-15 +2-17 +2-22

= 1.0 + 0.666115

0 0110 1000 101 0101 0100 0011 0100 0010

• Represents: 1.66611510*2-23 ~ 1.986*10-7

(about 2/10,000,000)

Page 15: CHAPTER 3 Floating Point. Representation of Fractions (1/2) With base 10, we have a decimal point to separate integer and fraction parts to a number.

Converting Decimal Fraction to FP (1/5)

• Simple Case: If denominator of fraction is a power of 2 (2, 4, 8, 16, etc.), then it’s easy.

• Show MIPS representation of -0.75– -0.75 = -3/4

– -112/1002 = -0.11two

– Normalized to -1.12 x 2-1

– (-1)S x (1 + significand) x 2(Exponent-127)

– (-1)1 x (1 + .100 0000 ... 0000) x 2(-1)

1 0111 1110 100 0000 0000 0000 0000 0000

Page 16: CHAPTER 3 Floating Point. Representation of Fractions (1/2) With base 10, we have a decimal point to separate integer and fraction parts to a number.

Converting Decimal to FP (2/5)• Given decimal fraction F• Algorithm to convert F to binary fraction.

for (k=1 to #bitsneeded)

{

dk = int ( 2 x F);

F = fraction (2 x F)

}

F = 0.845 0.11012

k=3: F = 0.380 2 x F = 0.760 d3 = 0 F = 0.760

k=1: F = 0.845 2 x F = 1.690 d1 = 1 F = 0.690

k=2: F = 0.690 2 x F = 1.380 d2 = 1 F = 0.380

k=4: F = 0.760 2 x F = 1.520 d4 = 1 F = 0.520

Page 17: CHAPTER 3 Floating Point. Representation of Fractions (1/2) With base 10, we have a decimal point to separate integer and fraction parts to a number.

Converting Decimal to FP (3/5)

• The number of binary digits may exceed the #bits in the significand– The binary fraction is truncated loss of accuracy

• Now, to normalize:

• Now, determine exponent:

• Finally, represent the number: 0.11012 1.10102 x 2-1

bias = 127expon = 127 + -1 = 126= 011111102

0 0111 1110 101 0000 0000 0000 0000 0000

Page 18: CHAPTER 3 Floating Point. Representation of Fractions (1/2) With base 10, we have a decimal point to separate integer and fraction parts to a number.

Converting Decimal to FP (4/5)

• Fact: All rational numbers have a repeating pattern when written out in decimal.

• Fact: This still applies in binary.

• To finish conversion:– Write out binary number with repeating pattern.– Cut it off after correct number of bits (different for

single v. double precision).– Derive Sign, Exponent and Significand fields.

Page 19: CHAPTER 3 Floating Point. Representation of Fractions (1/2) With base 10, we have a decimal point to separate integer and fraction parts to a number.

Converting Decimal to FP (5/5)

1. Denormalize: -23.40625

2. Convert integer part:23 = 16 + ( 7 = 4 + ( 3 = 2 + ( 1 ) ) ) = 101112

3. Convert fractional part:.40625 = .25 + ( .15625 = .125 + ( .03125 ) ) = .011012

4. Put parts together and normalize:10111.01101 = 1.011101101 x 24

5. Convert exponent: 127 + 4 = 100000112

1 1000 0011 011 1011 0100 0000 0000 0000

-2.340625 x 101

Page 20: CHAPTER 3 Floating Point. Representation of Fractions (1/2) With base 10, we have a decimal point to separate integer and fraction parts to a number.

Understanding the Significand (1/2)

• Method 1 (Fractions):– In decimal: 0.34010 34010/100010

3410/10010

– In binary: 0.1102 1102/10002 = 610/810

112/1002 = 310/410

– Advantage: less purely numerical, more thought oriented; this method usually helps people understand the meaning of the significand better

Page 21: CHAPTER 3 Floating Point. Representation of Fractions (1/2) With base 10, we have a decimal point to separate integer and fraction parts to a number.

Understanding the Significand (2/2)

• Method 2 (Place Values):– Convert from scientific notation

– In decimal: 1.6732 = (1x100) + (6x10-1) + (7x10-2) + (3x10-3) + (2x10-4)

– In binary: 1.1001 = (1x20) + (1x2-1) + (0x2-2) + (0x2-3) + (1x2-4)

– Interpretation of value in each position extends beyond the decimal/binary point

– Advantage: good for quickly calculating significand value; use this method for translating FP numbers

Page 22: CHAPTER 3 Floating Point. Representation of Fractions (1/2) With base 10, we have a decimal point to separate integer and fraction parts to a number.

Peer to Peer Exercise

What is the decimal equivalent of:

1 1000 0001 111 0000 0000 0000 0000 0000S Exponent Significand

(-1)S x (1 + Significand) x 2(Exponent-127)

(-1)1 x (1 + .111) x 2(129-127)

-1 x (1.111) x 2(2)

-111.12

-7.510

Page 23: CHAPTER 3 Floating Point. Representation of Fractions (1/2) With base 10, we have a decimal point to separate integer and fraction parts to a number.

Precision and Accuracy

Precision is the number of bits in a computer word used to represent a value.

Accuracy is a measure of the difference in value between the actual (decimal) value and its computer representation.

Don’t confuse these two terms!

High precision permits high accuracy but doesn’t guarantee

it. It is possible to have high precision but low accuracy.

Example: float pi = 3.14;pi will be represented using all 24 bits of the significant (highly precise), but is only an approximation (not accurate).

pi is actually some infinitely non-repeating number (transcendental), not 3.14

Page 24: CHAPTER 3 Floating Point. Representation of Fractions (1/2) With base 10, we have a decimal point to separate integer and fraction parts to a number.

Representation for 0• Represent 0?

– exponent all zeroes– significand all zeroes too– What about sign?–+0: 0 00000000 00000000000000000000000

–-0: 1 00000000 00000000000000000000000

• Why two zeroes?–Helps in some limit comparisons–Ask math majors

Page 25: CHAPTER 3 Floating Point. Representation of Fractions (1/2) With base 10, we have a decimal point to separate integer and fraction parts to a number.

Representation for ± ∞

• In FP, divide by 0 should produce ± ∞, not overflow.

• Why?– OK to do further computations with ∞

e.g., X/0 > Y may be a valid comparison– Ask math majors

• IEEE 754 represents ± ∞–Most positive exponent reserved for ∞–Significand all zeroes

Page 26: CHAPTER 3 Floating Point. Representation of Fractions (1/2) With base 10, we have a decimal point to separate integer and fraction parts to a number.

Special Numbers• What have we defined so far?

(Single Precision)

Exponent Significand Object

0 0 0

0 nonzero ???

1-254 anything +/- fl. pt. #

255 0 +/- ∞

255 nonzero ???

Page 27: CHAPTER 3 Floating Point. Representation of Fractions (1/2) With base 10, we have a decimal point to separate integer and fraction parts to a number.

Representation for Not a Number

• What is sqrt(-4.0)or 0/0?–If ∞ not an error, these shouldn’t be either.–Called Not a Number (NaN)–Exponent = 255, Significand nonzero

• Why is this useful?–Symbol for typical invalid operations–Hope NaNs help with debugging?–Programmers may postpone tests or decisions to a later time–They contaminate: op(NaN, X) = NaN

Page 28: CHAPTER 3 Floating Point. Representation of Fractions (1/2) With base 10, we have a decimal point to separate integer and fraction parts to a number.

Representation for Denorms (1/2)• Problem: There’s a gap among representable

FP numbers around 0–Smallest representable pos num:

a = 1.0… 2 * 2-126 = 2-126

–Second smallest representable pos num:

b = 1.000……1 2 * 2-126 = 2-126 + 2-149

a - 0 = 2-126

b - a = 2-149

b

a0+-

Gaps!Normalization and implicit 1are to blame!

Page 29: CHAPTER 3 Floating Point. Representation of Fractions (1/2) With base 10, we have a decimal point to separate integer and fraction parts to a number.

Representation for Denorms (2/2)

• Solution:–We still haven’t used Exponent = 0, Significand nonzero

–Denormalized number: no leading 1, implicit exponent = -126.

–Smallest representable pos num:a = 2-149

–Second smallest representable pos num:b = 2-148

0+-

Page 30: CHAPTER 3 Floating Point. Representation of Fractions (1/2) With base 10, we have a decimal point to separate integer and fraction parts to a number.

Summary of Special Numbers •Reserve exponents, significands:

Exponent Significand Object0 0 00 nonzero Denorm1-254 anything +/- fl. pt. #255 0 +/- ∞255 nonzero NaN

Page 31: CHAPTER 3 Floating Point. Representation of Fractions (1/2) With base 10, we have a decimal point to separate integer and fraction parts to a number.

Rounding

• Math on real numbers we worry about rounding to fit result in the significant field.

• FP hardware carries 2 extra bits of precision, and rounds for proper value

• Rounding occurs when…–converting double to single precision–converting floating point # to an integer–Intermediate step if necessary

Page 32: CHAPTER 3 Floating Point. Representation of Fractions (1/2) With base 10, we have a decimal point to separate integer and fraction parts to a number.

IEEE Four Rounding Modes• Round towards + ∞

–ALWAYS round “up”: 2.1 3, -2.1 -2

• Round towards - ∞–ALWAYS round “down”: 1.9 1, -1.9 -2

• Round towards 0 (i.e., truncate)–Just drop the last bits

• Round to (nearest) even (default)–Normal rounding, almost: 2.5 2, 3.5 4–Insures fairness on calculation–Half the time we round up, other half down–Also called unbiased

Page 33: CHAPTER 3 Floating Point. Representation of Fractions (1/2) With base 10, we have a decimal point to separate integer and fraction parts to a number.

• Algorithm:1. Shift smaller number right to match exponents

2. Add significands

3. Normalize result, checking for over/underflow

4. Round result significand

5. Normalize result again, checking for over/underflow

• Subtraction essentially the same

Floating Point Addition & Subtraction

Page 34: CHAPTER 3 Floating Point. Representation of Fractions (1/2) With base 10, we have a decimal point to separate integer and fraction parts to a number.

Floating Point Addition & Subtraction

• Example (4 decimal digit significand):

9.234 10-1 + 7.656 10-2

= 9.234 10-1 + 0.7656 10-1 = 9.9996 10-1 (Extra position to round

accurately)

= 10.00 10-1

= 1.000 100

Page 35: CHAPTER 3 Floating Point. Representation of Fractions (1/2) With base 10, we have a decimal point to separate integer and fraction parts to a number.

Floating Point Multiplication

• Algorithm:1. Multiply significands (unsigned integer mult &

fix sign)

2. Add exponents (signed biased integer add)

3. Normalize result, checking for over/underflow

4. Round result significand

5. Normalize result again, checking for over/underflow

Page 36: CHAPTER 3 Floating Point. Representation of Fractions (1/2) With base 10, we have a decimal point to separate integer and fraction parts to a number.

Floating Point Multiplication

• Example (4 decimal digit significand):

3.501 10-3 -4.002 106

= -14.016002 103

= -1.4016002 104

= -1.402 104

Page 37: CHAPTER 3 Floating Point. Representation of Fractions (1/2) With base 10, we have a decimal point to separate integer and fraction parts to a number.

Floating Point Division

• Algorithm:1. Divide significands (unsigned integer div & fix sign)

2. Subtract exponents (signed biased integer sub)

3. Normalize result, checking for over/underflow

4. Round result significand

5. Normalize result again, checking for over/underflow

Page 38: CHAPTER 3 Floating Point. Representation of Fractions (1/2) With base 10, we have a decimal point to separate integer and fraction parts to a number.

Floating Point Division

• Example (4 decimal digit significand):

4.010 10-3 / -4.012 106

= 4.010/-4.012 * 10-9

= -0.999501 10-9

= -9.99501 10-10

= -10.00 10-10

= -1.000 10-9

Page 39: CHAPTER 3 Floating Point. Representation of Fractions (1/2) With base 10, we have a decimal point to separate integer and fraction parts to a number.

MIPS Floating Point Architecture (1/4)• Separate floating point instructions:

–Single Precision:add.s, sub.s, mul.s, div.s

–Double Precision:add.d, sub.d, mul.d, div.d

• These are far more complicated than their integer counterparts so they can take much longer to execute

Page 40: CHAPTER 3 Floating Point. Representation of Fractions (1/2) With base 10, we have a decimal point to separate integer and fraction parts to a number.

MIPS Floating Point Architecture (2/4)

• Problems:–Inefficient to have different instructions take vastly

differing amounts of time.

–Generally, a particular piece of data will not change from float to int, or vice versa, within a program.

• Only one type of instruction will be used on it.

–Some programs do no floating point calculations

–It takes lots of hardware relative to integers to do floating point fast

Page 41: CHAPTER 3 Floating Point. Representation of Fractions (1/2) With base 10, we have a decimal point to separate integer and fraction parts to a number.

MIPS Floating Point Architecture (3/4)

• 1990 Solution: Make a completely separate chip that handles only FP.

• Coprocessor 1: FP chip–contains 32 32-bit registers: $f0, $f1, …–most of the registers specified in .s and .d instruction

refer to this set–separate load and store: lwc1 and swc1

(“load word coprocessor 1”, “store …”)–Double Precision: by convention, even/odd pair

contain one DP FP number: $f0/$f1, $f2/$f3, … , $f30/$f31

• Even register is the name

Page 42: CHAPTER 3 Floating Point. Representation of Fractions (1/2) With base 10, we have a decimal point to separate integer and fraction parts to a number.

MIPS Floating Point Architecture (4/4)• 1990 Computer actually contains multiple

separate chips:–Processor: handles all the normal stuff–Coprocessor 1: handles FP and only FP; –more coprocessors?… Yes, later–Today, FP coprocessor integrated with CPU, or cheap

chips may leave out FP HW

• Instructions to move data between main processor and coprocessors:

–mfc0, mtc0, mfc1, mtc1, etc.

• Appendix A contains many more FP ops

Page 43: CHAPTER 3 Floating Point. Representation of Fractions (1/2) With base 10, we have a decimal point to separate integer and fraction parts to a number.

Things to Remember

• Floating Point numbers approximate values that we want to use.

• IEEE 754 Floating Point Standard is most widely accepted standard for FP numbers

• New MIPS registers($f0-$f31), instructions:– Single Precision (32 bits, 2x10-38… 2x1038):

add.s, sub.s, mul.s, div.s–Double Precision (64 bits , 2x10-308…2x10308): add.d, sub.d, mul.d, div.d

• Type is not associated with data, bits have no meaning unless given in context (instruction)