Presentation is loading. Please wait.

Presentation is loading. Please wait.

Floating Point Numbers

Similar presentations


Presentation on theme: "Floating Point Numbers"— Presentation transcript:

1 Floating Point Numbers
Prof. Giancarlo Succi, Ph.D., P.Eng. Prof. G. Succi, Ph.D., P.Eng.

2 Content Introduction Scientific Notation IEEE 754 Scientific Notation
Converting numbers Special numbers Prof. G. Succi, Ph.D., P.Eng.

3 Numbers since now What can we represent in N bits? Unsigned integers:
0 to 2N – 1 Signed Integers (One’s Complement) -2(N-1) + 1 to 2(N-1) - 1 Signed Integers (Two’s Complement) -2(N-1) to 2(N-1) - 1 Prof. G. Succi, Ph.D., P.Eng.

4 Numbers since now What can we represent in 32 bits? Unsigned integers:
0 to 4,294,967,295 Signed Integers (One’s Complement) - 2,147,483,647 to 2,147,483,647 Signed Integers (Two’s Complement) - 2,147,483,648 to 2,147,483,647 Prof. G. Succi, Ph.D., P.Eng.

5 Regions not covered since now
Other numbers Regions covered: Unsigned integers Signed integers -1 1 2 -2,147,483,648 2,147,483,647 4,294,967,295 Regions not covered since now Prof. G. Succi, Ph.D., P.Eng.

6 Other numbers What about other numbers?
Very large numbers? (seconds/century) 3,155,760,00010 ( x 109) Very small numbers? (atomic diameter) (1.010 x 10-8) Rationals (repeating pattern) 2/3 ( ) Irrationals /2 ( ) Transcendentals e ( ),  ( ) All represented in scientific notation Prof. G. Succi, Ph.D., P.Eng.

7 Content Introduction Scientific Notation IEEE 754 Scientific Notation
Converting numbers Special numbers Prof. G. Succi, Ph.D., P.Eng.

8 Scientific Notation exponent mantissa 6.02 x 1023 radix (base)
decimal point radix (base) Normalized form: no leadings 0s (exactly one 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 Prof. G. Succi, Ph.D., P.Eng.

9 Converting base to 2 exponent mantissa 1.0two x 2-1 radix (base)
binary point radix (base) Computer arithmetic that supports it called floating point, because it represents numbers where binary point is not fixed, as it is for integers Declare such variables in Java as float Prof. G. Succi, Ph.D., P.Eng.

10 Binary Scientific Notation in detail
31 S Exponent 30 23 22 Significand 1 bit 8 bits 23 bits Normal format: +1.xxxxxxxxxxtwo*2yyyytwo S represents Sign Exponent represents y’s Significand represents x’s Prof. G. Succi, Ph.D., P.Eng.

11 Covered Range Regions covered: Largest Exponent: 2128 => 1038
-2.0 x 1038 -2.0 x 10-38 2.0 x 10-38 2.0 x 1038 Underflow Overflow Prof. G. Succi, Ph.D., P.Eng.

12 Extend covered range 31 30 20 22 1 bit 11 bits 20 bits 32 bits
31 S Exponent 30 20 22 Significand 1 bit 11 bits 20 bits Significand 32 bits Double Precision (vs. Single Precision) C variable declared as double Represent numbers almost as small as 2.0 x to almost as large as 2.0 x 10308 But primary advantage is greater accuracy due to larger significand Prof. G. Succi, Ph.D., P.Eng.

13 Content Introduction Scientific Notation IEEE 754 Scientific Notation
Converting numbers Special numbers Prof. G. Succi, Ph.D., P.Eng.

14 IEEE 754 Floating Point Standard
Single Precision, DP similar Sign bit: 1 means negative means positive Significand: To pack more bits, the leading 1 is implicit for normalized numbers bits single, bits double always true: Significand < 1 (for normalized numbers) Note: 0 has no leading 1, so reserve exponent value 0 just for number 0 Prof. G. Succi, Ph.D., P.Eng.

15 IEEE 754: Usability Introduced by Prof. Kahan: to use FP numbers even if no FP hardware is available; e.g., sort records with FP numbers using integer compares It break FP number comparisons into 3 parts: compare signs, then exponents, then significands To improve speed, try to have a single comparison, especially if a number is positive Therefore, … The highest bit is the sign ( negative < positive) Exponent next, so larger exponent  larger # Significand last Prof. G. Succi, Ph.D., P.Eng.

16 IEEE 754: Signed Exponent Using 2’s comp? 1.0 x 2-1 v. 1.0 x2+1 (1/2 v. 2) 1/2 2 This notation using integer compare of 1/2 v. 2 makes 1/2 > 2! Instead, pick notation is smallest negative, and is the largest positive 1.0 x 2-1 v. 1.0 x2+1 (1/2 v. 2) 1/2 2 Prof. G. Succi, Ph.D., P.Eng.

17 IEEE 754: Summary Called Biased Notation, where bias is number subtract to get real number IEEE 754 uses bias of 127 for single prec. Subtract 127 from Exponent field to get actual value for exponent 1023 is bias for double precision (-1)S x (1 + Significand) x 2(Exponent-127) Double precision identical, except with exponent bias of 1023 Prof. G. Succi, Ph.D., P.Eng.

18 Content Introduction Scientific Notation IEEE 754 Scientific Notation
Converting numbers Special numbers Prof. G. Succi, Ph.D., P.Eng.

19 Understanding significand: fractions
In decimal: => 34010/ => 3410/10010 Binary: => 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 Prof. G. Succi, Ph.D., P.Eng.

20 Understanding significand: positional values
Convert from scientific notation In decimal: = (1x100) + (6x10-1) + (7x10-2) + (3x10-3) + (2x10-4) In binary: = (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 Prof. G. Succi, Ph.D., P.Eng.

21 Converting Binary FP to Decimal
Sign: 0 => positive Exponent: two = 104ten Bias adjustment: = -23 Significand: 1 + 1x2-1+ 0x x x x = = Represents: ten*2-23 ~ 1.986*10-7 (about 2/10,000,000) Prof. G. Succi, Ph.D., P.Eng.

22 Attention to non-typed data!!!
0011 0100 What does bit pattern mean: 1.986 *10-7? 878,003,010? “4UCB”? ori $s5, $v0, 17218? Data can be anything; operation of instruction that accesses operand determines its type! Side-effect of stored program concept: instructions stored as numbers Power/danger of unrestricted addresses/ pointers: use ASCII as Fl. Pt., instructions as data, integers as instructions, ... (Leads to security holes in programs) Prof. G. Succi, Ph.D., P.Eng.

23 Converting Decimal to FP
Simple Case: If denominator is an exponent of 2 (2, 4, 8, 16, etc.), then it’s easy. Show MIPS representation of -0.75 -0.75 = -3/4 -11two/100two = -0.11two Normalized to -1.1two x 2-1 (-1)S x (1 + Significand) x 2(Exponent-127) (-1)1 x ( ) x 2( ) 1 Prof. G. Succi, Ph.D., P.Eng.

24 Converting Decimal to FP
If denominator is not an exponent of 2… Then we cannot represent the number precisely, but that’s why we have so many bits in significand: for precision Once we have the significand, normalizing a number to get the exponent is easy So how do we get the significand? Prof. G. Succi, Ph.D., P.Eng.

25 Converting Decimal to FP: Rationale
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. Prof. G. Succi, Ph.D., P.Eng.

26 Conversion: Example How to represent 1/3 in MIPS? 1/3 = 0.33333…10
= … = 1/4 + 1/16 + 1/64 + 1/ / … = … = … 2 * 20 = … 2 * 2-2 Prof. G. Succi, Ph.D., P.Eng.

27 Conversion: Example Sign: 0 Exponent = -2 + 127 = 12510=011111012
Significand = … Prof. G. Succi, Ph.D., P.Eng.

28 Content Introduction Scientific Notation IEEE 754 Scientific Notation
Converting numbers Special numbers Prof. G. Succi, Ph.D., P.Eng.

29 Representation for +/- Infinity
In FP, divide by zero should produce +/- infinity, not overflow. Why? OK to do further computations with infinity e.g., X/0 > Y may be a valid comparison IEEE 754 represents +/- infinity Highest positive exponent reserved for infinity Significand: all zeroes Prof. G. Succi, Ph.D., P.Eng.

30 Representation for 0 Represent 0? Why two zeros? exponent all zeros
significand all zeros too What about sign? +0: -0: Why two zeros? Helps in some comparisons Prof. G. Succi, Ph.D., P.Eng.

31 Special numbers: summary
What have we defined so far? (Single Precision) Exponent Significand Object 0 nonzero ??? anything +/- fl. pt. # /- infinity 255 nonzero ??? Prof. G. Succi, Ph.D., P.Eng.

32 Proposed Example What value is represented by: 1 1000 0001
Prof. G. Succi, Ph.D., P.Eng.

33 Proposed Example: solution
What value is represented by: 1 S Exponent Significand (-1)S x (1 + Significand) x 2(Exponent-127) (-1)1 x ( ) x 2( ) -1 x (1.111) x 2(2) = = -7. 5 Prof. G. Succi, Ph.D., P.Eng.

34 Things to Remember Floating Point numbers approximate the values that we want to use. IEEE 754 Floating Point Standard is the most widely accepted attempt to standardize interpretation of such numbers Every desktop computer sold since ~1997 follows these conventions Type is not associated with data, bits have no meaning unless given in context Prof. G. Succi, Ph.D., P.Eng.


Download ppt "Floating Point Numbers"

Similar presentations


Ads by Google