Download presentation
Presentation is loading. Please wait.
1
Floating Point Math & Representation
Intro to Computer Org. Floating Point Math & Representation
2
Motivation Integers are a very limited subset of the real numbers, even if we could represent all integers. Much of the time, we wish to use fractional numbers, be they rational or irrational.
3
A Human Perspective Consider a standard fractional number such as The decimal indicates the fractional component. We could try storing this one as an integer and mark the decimal place…
4
A Human Perspective But consider a number such as 1.058712 * 1046.
This one can’t be easily represented by integers due to its magnitude. What about * 10-46? All of these are fairly easy to represent in our writing system.
5
A Human Perspective Note that the last two numbers presented were written in scientific notation. Commonly used shorthand that preserves the most significant details of any number.
6
Binary Scientific Notation
Just as we have scientific notation in the decimal number system, we can have it in a binary system. Ex: * 23 = = = =
7
Decimal -> Binary We already learned how to convert integers to binary. Divide the number by two. Take the remainder as the next digit, working right-to-left. If quotient = 0, stop. Otherwise, take the quotient as the new number, return to step 1.
8
Decimal -> Binary To convert the fractional part from decimal to binary: Multiply the number by two. Remove the integer part and save it as the next digit, working left-to-right. If remainder of number = 0, stop. Otherwise, take the remainder as the new number, return to step 1.
9
Decimal -> Binary Note that the process is almost a direct inversion of the process for whole numbers.
10
Decimal -> Binary 0.625 * 2 = 1.250 => .1___
0.625 * 2 = => .1___ 0.250 * 2 = => .10__ 0.500 * 2 = => .101_ 0 is left, so we’re done!
11
Decimal -> Binary Because we’re using base 2, some fractions become repeating. 0.2 * 2 = 0.4 => .0______ 0.4 * 2 = 0.80 => .00_____ 0.8 * 2 = 1.60 => .001____ 0.6 * 2 = 1.20 => .0011___ 0.2 * 2…
12
Decimal -> Binary Because we’re using base 2, some fractions become repeating. 0.2 * 2 = 0.4 => .0______ 0.4 * 2 = 0.80 => .00_____ 0.8 * 2 = 1.60 => .001____ 0.6 * 2 = 1.20 => .0011___ 0.2 * 2…
13
Decimal -> Binary For conversions in this class, it’s highly advised to handle each side of a decimal point separately.
14
Decimal -> Binary Let’s now consider 14.625.
14 => 0.625 => So, => Final step – normalization = * 23. Scientific notation allows only one leading digit.
15
Binary Scientific Notation
All the basic mathematical operations operate in a similar fashion to their decimal scientific notation counterparts. Read starting in page 195 for more details on the operations. Part of section 3.6, the introduction to floating point.
16
Binary Scientific Notation
Notable exception – sometimes division is performed by taking the reciprocal of the divisor, then performing multiplication.
17
Floating Point The floating point specification has three pieces.
A sign bit The fractional part of the number An exponent field The entire number fits within 32 bits if single-precision 64 bits if double-precision
18
Floating Point The floating point specification has three pieces.
A sign bit The fractional part of the number An exponent field
19
Floating Point – Sign Bit
Floating point represents sign in a manner identical to that of the sign-magnitude representation of integers. Leading bit = 1 if negative, 0 if positive. Denotable as S in formulas, where (-1)S gives the sign of the floating-point number.
20
Floating Point The floating point specification has three pieces.
A sign bit The fractional part of the number Also called significand. An exponent field
21
Floating Point - Significand
With one exception, all binary numbers in scientific notation share the same leading digit – 1. Definition of the notation says leading digit must be non-zero. As a result, the leading 1 is not stored – only the parts after the “binary point” are.
22
Floating Point - Significand
Thus, to obtain the part of the number that represents the significand, (1. + Fraction) signifies the need to prefix the bits appropriately.
23
Floating Point The floating point specification has three pieces.
A sign bit The fractional part of the number An exponent field
24
Floating Point – Exponents
Floating point numbers store their exponents in yet another representation – one that is biased. Take desired exponent and add the bias to it. Single-precision: Add 127 (27-1). Double-precision: Add 1023 (210-1).
25
Floating Point – Exponents
Represent the result as an unsigned integer. Why might one wish to do this?
26
Floating Point – Exponents
Special cases After biasing the exponent, two values indicate special conditions. The minimum possible value (zero) The maximum possible value (all ones) 255 in single-precision 2047 in double-precision The corresponding unbiased exponents are thus out-of-range.
27
Floating Point – Exponents
Before covering the special cases, let’s take a look at standard floating-point numbers. We’ll use single-precision for our examples.
28
Single-Precision Floats
Sign Exponent Fraction/Significand 1 bit 8 bits 23 bits Let’s take a look at = * 23. Sign = positive, so we’d represent it with a zero.
29
Single-Precision Floats
Sign Exponent Fraction/Significand 8 bits 23 bits Let’s take a look at = * 23. Exponent (unbiased) = 3. Exponent (biased) = = 130. 130 = Sidenote – 2(8-1) – 1 = 127. Pattern holds for double-precision.
30
Single-Precision Floats
Sign Exponent Fraction/Significand 23 bits Let’s take a look at = * 23. We ignore the leading 1. The part to the right of the binary point is , so we store this.
31
Single-Precision Floats
Sign Exponent Fraction/Significand Let’s take a look at = * 23. We ignore the leading 1. The part to the right of the binary point is , so we store this and append zeros to the end to fill out the remaining bits. 31
32
Single-Precision Floats
How about ( …) * 2-13? Let’s work this one on the board.
33
Floating Point - Zero One of the special cases of floating point is when all bits of the float/double are zero. A very convenient representation of zero!
34
Imprecision of Floating Point
All floating-point numbers are stored in a scientific notation format with limited bits. This gives a tradeoff between range of numbers representable and the precision on those numbers. Single-precision gives 22 bits for the significant, giving accuracy to a little over seven decimal digits.
35
Imprecision of Floating Point
What is the smallest possible difference between any two numbers with the same (binary) magnitude? That is, the same power of two in binary scientific notation. It depends on the power of two!
36
Imprecision of Floating Point
Sign Exponent Fraction/Significand 3 (unbiased) 22 bits Let’s take a look at numbers between 8 and 16. (16 not included.) Unbiased exponent =3. What would be the smallest possible difference, looking at the bit patterns?
37
Imprecision of Floating Point
Sign Exponent Fraction/Significand 3 (unbiased) The answer, in floating point format, is all zeros with a one at the end. Since the exponent is the same for both numbers, the difference is only in the significand. The above difference is the smallest non-zero difference possible. Warning – the above is before normalization – there is no leading 1! 37
38
Imprecision of Floating Point
Sign Exponent Fraction/Significand 3 (unbiased) What would this translate to in decimal? (-1)0 * ( ) * 23 = = 2-19 Note that we don’t add the 1 to the fraction because we’re dealing with a difference between two floating point numbers here before normalization. 2-19 = , or * 10-6. 38
39
Imprecision of Floating Point
Note that this number is not precise to seven actual decimal places when the number is not in scientific notation! Numerical Analysis studies the consequences of discrete representations of real numbers, equations, and the like.
40
Other Special Cases Special case – biased exponent is all ones.
Is the significand all zero bits? If so, the bit pattern represents ±∞. If not, it represents NaN. (Not a number) Useful for when illegal operations are performed, such as dividing by zero.
41
Other Special Cases Last special case – biased exponent is zero, but the significand is non-zero. This represents a denormalized number. Allows a gradual loss of precision at the limits of the exponent range. For more details, read section 3.6 of the text. We won’t worry about it much here.
42
More examples (On board)
6,706,993,152 – approximate population of the world. Answer in single-precision + decimal * Answer in single-precision Answer in decimal notation
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.