You are on page 1of 13

COMPUTER PROGRAMMING

(CS F111)

LECTURE 07–FLOATING POINT REPRESENTATION


Fractions: Fixed-Point
 How can we represent fractions?
 Use a “binary point” to separate positive from
negative powers of two -- just like “decimal point.”

2-1 = 0.5
2-2 = 0.25
Example: 2-3 = 0.125
11111110.110

January 24, 2018 Biju K Raveendran@BITS Pilani. 2


Conversion from Binary to Decimal(Fractions)
Convert to decimal: 00101000.101

Step1: Take integer part and convert to decimal


23 + 25 = 40

Step2: Take fraction part and convert to decimal


2-1+2-3 = .625

Step3: Add result of Step1 and Step2


40.625

January 24, 2018 Biju K Raveendran@BITS Pilani. 3


2-1 = 0.5
2-2 = 0.25
Example: 2-3 = 0.125
11111110.110

Note: 11111110 = -2 (decimal)


.110 = 0.75
So 11111110.110 gives -2 + 0.75 = -1.25

January 24, 2018 Biju K Raveendran@BITS Pilani. 4


Arithmetic on Fractions
2’s comp addition and subtraction still work.
 If binary points are aligned 2-1 = 0.5
Example: 2-2 = 0.25
2-3 = 0.125

00101000.101 (40.625)
+ 11111110.110 (-1.25)
00100111.011 (39.375)

January 24, 2018 Biju K Raveendran@BITS Pilani. 5


Precision and Range
 Precision: Difference between successive values
for a given data type
 Precision of the number, is determined by the number of
fractional bits.
 For example, a fixed-point representation with three bits
to the right of the binary point has a precision of 2-3
 i.e. value of least significant bit
 Range
 The range of numbers
 For example, for 8 bit 2’s compliment data type the
range is -128 to +127.

January 24, 2018 Biju K Raveendran@BITS Pilani. 6


Representations of very Big and very
Small Numbers
How many Bits required to represent 6.023 x 223 ?

How many Bits required to represent 6.626 x 2-34 ?

Can we represent above numbers by using 32 Bits?


 If YES, HOW?
 By Floating Point Representation

January 24, 2018 Biju K Raveendran@BITS Pilani. 7


Floating-Point Representation
 Use equivalent of “scientific notation”: F x 2E

 Need to represent F (fraction), E (exponent), and


sign.
 IEEE 754 Floating-Point Standard (32-bits):
1b 8b 23b

S exponent fraction

N  ( 1)S  1.fraction  2exponent 127, 1  exponent  254


N  ( 1)S  0.fraction  2126, exponent  0

January 24, 2018 Biju K Raveendran@BITS Pilani. 8


Floating Point Example
 Single-precision IEEE floating point number
1 01111110 10000000000000000000000
sign exponent fraction

 Sign is 1 number is negative.


 Exponent field is 01111110 = 126 (decimal).
 Fraction is 0.100000000000… = 0.5 (decimal).

Value = -1.1 x 2(126-127) = -1.1x 2-1 = -0.11


Decimal Equivalent: -0.75.
January 24, 2018 Biju K Raveendran@BITS Pilani. 9
Floating Point Example
Represent 1/8 (0.125) in IEEE 754 format?
Binary equivalent of 0.125 is 0.001 or 1.0 x 2-3
(Normalized)
N = (-1)s X 1.fraction X 2exponent-127

Sign bit = 0 (Number is positive)


exponent - 127 = -3 i.e. exponent = 124
Binary equivalent of 124 = 01111100
Fraction = 00000000000000000000000

Final representation of 1/8 in IEEE 754 format is


00111110000000000000000000000000

January 24, 2018 Biju K Raveendran@BITS Pilani. 10


Floating Point Example
Represent 2-131 in IEEE 754 format?
Binary equivalent of 0.00001x 2-126
If exponent is 0 then
N = (-1)s X 0.fraction X 2-126

Sign bit = 0 (Number is positive)


exponent = 0
Fraction = 00001000000000000000000

Final representation of 2-131 in IEEE 754 format is


00000000000001000000000000000000

January 24, 2018 Biju K Raveendran@BITS Pilani. 11


Floating point Representation

 What is the largest positive number we can


represent by using a floating point
representation?
 0 11111110 11111111111111111111111 ~ 2128
 What is the smallest positive number we can
represent by using a floating point
representation?
 0 00000000 00000000000000000000001 2-149

January 24, 2018 Biju K Raveendran@BITS Pilani. 12


Exercises

 0.0101 x 267
 110010.1010 [Assume this number is
represented in 2’s complement form]
 0

January 24, 2018 Biju K Raveendran@BITS Pilani. 13

You might also like