Page 1
Numerical Methods
IS089IU
Floating Point Representation and Errors – Chapter 2
Page 2
Scientific Notation
Format Short
Format Long
Format short e
Format long e
Page 3
Floating Point Puzzles
For each of the following expressions, do you understand what they are?
int x = …;
float f = …;
double d = …;
Problem with a "regular number" (fixed-point): A fixed-point system allocates a fixed
number of bits to the integer part and a fixed number to the fractional part.
• To store the small number, like 0.0000000001 mm, you need to allocate many bits for
the fractional part, leaving very few for the integer part.
• To store the large number, like 94,607,304,725,808 km, you need to allocate many
bits for the integer part, leaving no room for the fractional part to represent small
items.
Page 4
IEEE Floating Point
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
Nice standards for rounding, overflow, underflow
Hard to make go fast
Numerical analysts predominated over hardware types in defining standard
Page 5
Binary Number
Base 2 number
Using Matlab/Octave
Decimal to Binary
10(base 10)=1010 (base 2) =
1*23+0*22+1*21+0*20=8+0+2+0=10(base 10)
10/2=5R0, 5/2=2R1,2/2=1R0, 1/2=0R1, Reverse the
remainders: 1010
Binary to Decimal
1001(base 2)=1*23+0*22+0*21+1*20=8+0+0+1=9(base 10)
Decimal points number
10.5(base 10)=1010.1(base 2)
10.25(base 10)=1010.01(base 2)
10.125(base 10)=1010.001(base 2)
Write a matlab
code to convert
decimal to binary
Use dec3bin
Page 6
Fractional Binary Number Examples
Value
5-3/4
2-7/8
63/64
Representation
101.112
10.1112
0.1111112
Observation
Divide by 2 by shifting right
Numbers of form 0.111111…2 just below 1.0
Use notation 1.0 –
Limitation
Can only exactly represent numbers of the form x/2k
Other numbers have repeating bit representations
Value
1/3
1/5
1/10
Representation
0.0101010101[01]…2
0.001100110011[0011]…2
0.0001100110011[0011]…2
Page 7
Floating Point Representation
Numerical Form
–1s M 2E
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
Encoding
s
exp
MSB is sign bit
exp field encodes E
frac field encodes M
Sizes
Single precision: 8 exp bits, 23 frac bits
32 bits total
Double precision: 11 exp bits, 52 frac bits
64 bits total
frac
Page 8
Step-by-Step Example using 32-bit IEEE 754 Format
Let's convert the decimal number -10.625 to a 32-bit IEEE 754 format
Step 1. Convert to Binary:
Integer Part (10): 10 in decimal is 1010 in binary.
Fractional Part (0.625):
0.625 * 2 = 1.25 (write down 1)
0.25 * 2 = 0.5 (write down 0)
0.5 * 2 = 1.0 (write down 1)
The binary fractional part is .101.
Combined, -10.625 in binary is -1010.101.
Write Matlab code to
convert fractional
decimal to binary
(for .5, .25, .125, .75)
2. Convert to Binary Scientific Notation:
Move the binary point to create a normalized number, where there's a "1" to the
left of the point.
-1010.101 becomes -1.010101.
We moved the point 3 places to the left, so the exponent is 3.
The number in scientific notation is -1.010101 x 2³.
Page 9
Step-by-Step Example using 32-bit IEEE 754 Format
3. Determine the Sign Bit:
Since the original number is negative, the sign bit is 1.
4. Calculate the Biased Exponent:
The exponent is 3.
For 32-bit single-precision, the bias is 127.
Biased Exponent = 3 + 127 = 130.
Convert 130 to binary:
130 / 2 = 65 remainder 0
65 / 2 = 32 remainder 1
32 / 2 = 16 remainder 0
16 / 2 = 8 remainder 0
8 / 2 = 4 remainder 0
4 / 2 = 2 remainder 0
2 / 2 = 1 remainder 0
1 / 2 = 0 remainder 1
Reading the remainders in reverse, 130 in binary is 10000010.
Page 10
Step-by-Step Example using 32-bit IEEE 754 Format
5. Determine the Mantissa:
The mantissa is the fractional part of the binary scientific notation: 010101.
Since we have a limited number of bits for the mantissa in the 32-bit format (23
bits), we pad the end with zeros: 01010100000000000000000.
6. The 32-bit IEEE 754 is: 11000001001010100000000000000000.
Check online for the conversion
https://www.h-schmidt.net/FloatConverter/IEEE754.html
Page 11
HW (for advance student only)
Write a Matlab code to convert a Decimal number to
IEEE 754 32-bit floating point number. Test with the
following numbers:
1- 10.12556
2- -125.47523
Page 12
HW week 3:
• Read chapter 1 and 2 of
Matlab book:
• Perform example 1.2
Provide the plot just as
figure 1.4 in Matlab