Class 15.1 Binary Fractions. Fixed Point. Floating Point.pptx

Download Report

Transcript Class 15.1 Binary Fractions. Fixed Point. Floating Point.pptx

Floating Point. Binary Fractions.
• Fixed point representation
• Scientific Notation. Floating point
• Single precision, Double precision.
Textbook Ch.4.8 (p.275-301)
Central Connecticut State University, MIPS Tutorial. Chapters 29-32.
Fixed Point representation
This is base 10 positional notation. The decimal point is
put between the 100 position and the 10-1 position
3
5
7
.
3×102+ 5×101+ 7×100+ .
2
8
4
2×10-1+ 8×10-2+ 4×10-3
If the base is two, the point '.' is called a binary point.
1
0
0
.
1
1×22+
0×21+
0×20+
.
1×2-1+ 0×2-2+ 1×2-3
0
1
Integer and Fractional parts
1
0
0
.
1
0
1
1×22 +
0×21 +
0×20 +
.
1×2-1 +
0×2-2 +
1×2-3
1×4 +
0×2 +
0×1+
.
1×0.5 +
0×0.25 + 1×0.125
4 +
0 +
0 +
.
0.5 +
0 +
4
.
625
0.125
How to calculate
Power of 2
3
2
1
0
.
-1
-2
-3
-4
Decimal
8
4
2
1
.
0.5
0.25
0.125
0.0625
Include?
0
1
1
0
.
1
0
0
1
0110 . 1001 = 4 + 2 + 0.5 +0.0625 = 6.5625
Adding fixed point Expressions
fixed point
as decimal
01101001
6.5625
00010100
1.2500
01111101
7.8125
Power of 2
3
2
1
0
.
-1
-2
-3
-4
Decimal
8
4
2
1
.
0.5
0.25
0.125
0.062
5
Include?
0
1
1
1
.
1
1
0
1
0111. 1101 = 4 + 2 + 1 + 0.5 + 0.25 + 0.0625 = 7.8125
Binary Addition Algorithm Works!
Different Ranges
 Sometimes one program needs to deal with several
different ranges of numbers.
 Consider a program that must deal with both the
measurements that describe the dimensions on a silicon
chip (say 0.000000010 to 0.000010000 meters)
 and also the speed of electrical signals, 100000000.0 to
300000000.0 meters/second.
 It is hard to find a place to fix the decimal point so that
all these values can be represented.
Point Floats
 Notice that in writing those numbers (0.000000010,
0.000010000, 100000000.0, 300000000.0) I was able
to put the decimal point where it was needed in each
number.
 When the decimal point moves to where it is needed
in an expression, the decimal point is said to Float.
Scientific Notation
1.38502 × 1003
---+--|
|
+-- exponent
|
+--- mantissa
1.38502 × 1000 = 1385.02
 The idea of floating point comes from scientific
notation for numbers.
 The mantissa always has a decimal point after the first
digit.
 The decimal point "floats" to where it belongs.
 The mantissa gives the digits and the exponent says
where the decimal point should go.
Negative Exponents
 The exponent shows by what power of 10 to multiply
the mantissa by,
 or (the same thing) how far to float the decimal point.
10+n means move the decimal point n places right.
10-n means move the decimal point n places left.
1.82371 ×
101
=
1.82371 ×
10
=
18.2371
(move the point 1 place right)
1.82371 ×
10-1
=
1.82371 ×
.1
=
.182371
(move the point 1 place left)
1.82371 ×
102
=
1.82371 ×
100 =
182.371
(move the point 2 places right)
1.82371 ×
10-2
=
1.82371 ×
.01 =
.0182371
(move the point 2 places left)
The reverse process is called mantissa normalization
Negative Values
The notation for a negative number has a negative sign in front of
the first digit:
-1.38502 × 1003 = -(1.38502 × 1003) = -1385.02
The notation for a small negative number has two negative signs:
one to negate the entire number and one (in the exponent) to shift
the decimal point left:
-1.38502 × 10-3 = -(1.38502 × 10-3) = -0.00138502
e - close to idea of floating point
 Writing the cross (×) and the superscript exponent is
tedious,The
andnumber
not suitable
forinprograms.
of digits
the
called theisprecision
 A slightlymantissa
alteredisnotation
used in computer
of and
the floating
programs
in inputpoint
and number.
output text:
1.38502
1.38502
-1.38502
-1.38502
×
×
×
×
10+03
10-03
10+03
10-03
= 1.38502e+03
= 1.38502e-03
= -1.38502e+03
= -1.38502e-03
 This is getting close to the idea of floating point.
 Each expression has eight digits.
 Two of the digits are used to "float" the decimal
point.
 The remaining six show the digits of the number.
Single Precision Floating Point
 Single-precision floating point value is 32 bits long.
 The bits are divided into fixed-sized fields as follows:
• The mantissa and exponent fields work like the similar
parts in scientific notation.
• The sign bit gives the sign of the entire expression: a 0 bit
means positive and a 1 bit means negative.
Mantissa
The mantissa represents a number in 24-bit base two positional
notation that looks like this:
1.xxx xxxx xxxx xxxx xxxx xxxx
(x = 0 or 1; there are 23 x's)
• The mantissa represents the 23-bit binary fraction
• part of a 24-bit number that starts with a "1".
• The 20 place (the one's place) is presumed to contain a 1
and is not present in the mantissa.
• This trick gives us 24 bits of precision with only 23 bits.
For example, this
binary number
1.11110000101101101010001
is represented as
11110000101101101010001
Mantissa Explained
Represent 00011.00101101 as a float

Drop the leading zeros: 11.00101101




Now there is a leading one.
Normalize the mantissa - Move the binary point so that the
leading one is in the one's place (the exponent shows the move):
1.100101101 × 21
Drop the leading one:
100101101 × 21
Add zero bits on the right so that there are 23 bits in total:
10010110100000000000000 × 21
 The mantissa is:
10010110100000000000000
The Exponent
Biased 127 Exponent
actual
expon
ent
 The exponent is expressed
using a biased integer.
 This is an unsigned binary
integer that has +127 added
to it.
 The sign bit is zero when the
number is zero.
 A floating point zero consists
of 32 zero bits.
biased
eight bit
expon
exponent
ent
-127
0
0000 0000
-126
1
0000 0001
-10
117
0111 0101
0
127
0111 1111
1
128
1000 0000
10
137
1000 1001
127
254
1111 1110
128
255
1111 1111
Notes
Not repre
sented
Special
Usage
Formula and Example
 s is the sign bit (0 or 1),
 M is the mantissa (.000...000 to .1111...111)
 E is the exponent (-126 to +127).
value = (-1)s × 1.M × 2E-127
1.0




What is the sign bit of 1.0?
What is the mantissa of 1.0?
What is the actual exponent of 1.0?
What is the biased exponent of 1.0?
0 (for positive)
000 0000 0000 0000 0000 0000
0
0+12710 = 0111 11112
0 x 3F 80 00 00
Example on SPIM
## Program to represent 1.0
.text
.globl main
main:
.data
val:
.float
1.0
## End of file
- 0.110





imprecise representation
The sign bit of -0.1 is 1 (for negative).
The binary fraction for 0.1 (from the chapter 29
MIPS tutorial)
0.0001100110011001100110011001100...
The mantissa of 0.1 is:

Shift the leading bit into the one's place:
1.100110011001100110011001100...

The shift was 4 places left, for an exponent
of -4

Drop bit in the one's place:
.100110011001100110011001100...

Retain 23 bits: 100 1100 1100 1100 1100 1100
The actual exponent is -4
The biased exponent is 127-4 = 123 = 0111 1011
Decimal
Binary so far
Start
0.1
0.
×2
0.2
0.0
×2
0.4
0.00
×2
0.8
0.000
×2
1.6
0.0001
.6
0.0001
1.2
0.00011
0.2
0.00011
×2
0.4
0.000110
×2
0.8
0.0001100
×2
1.6
0.00011001
.6
0.00011001
1.2
0.000110011
0.2
0.000110011
×2
0.4
0.0001100110
×2
0.8
0.00011001100
×2
×2
Result
0.00011001100...
- 0.110
+-
imprecise representation
exponent
Mantissa
1 0 1 1 1 1 0 1 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 1
B
D
C
C
C
C
C
D
On SPIM, a constant of -0.1 is represented as 0xBDCCCCCD
because the value is rounded up one bit to form the "D" at
the end.



The sign bit of -0.1 is 1 (for negative).
Retain 23 bits: 100 1100 1100 1100 1100 1100
The biased exponent is 127-4 = 123 = 0111 1011
Floating Point Double and more Precisions
31
S
30
20
19
0
Mantissa – 20 bits
Exponent - 11 bits
31
0
Mantissa continued – 32 bits
• Single precision floats have only 24 bits of precision.
• This is the equivalent of 7 to 8 decimal digits. The 7 or 8 decimal
digits of precision is much worse than most electronic calculators.
• Double precision has 53 bits and 15 or 16 decimal places of
precision.
Name
Common name
binary16
Half precision
binary32
Single precision
binary64
Double precision
Quadruple
binary128
precision
Ba
Digits
se
2
10+1
E min
−14
Decimal Decimal
digits
E max
+15
3.31
4.51
E max
2
23+1
−126
+127
7.22
38.23
2
52+1
−1022
+1023
15.95
307.95
2
112+1
−16382 +16383
34.02
4931.77