Presentation is loading. Please wait.

Presentation is loading. Please wait.

Floating Point Numbers.  Floating point numbers are real numbers.  In Java, this just means any numbers that aren’t integers (whole numbers)  For example…

Similar presentations


Presentation on theme: "Floating Point Numbers.  Floating point numbers are real numbers.  In Java, this just means any numbers that aren’t integers (whole numbers)  For example…"— Presentation transcript:

1 Floating Point Numbers

2  Floating point numbers are real numbers.  In Java, this just means any numbers that aren’t integers (whole numbers)  For example… 2.86-0.54.000 -0.033.14159267.2 x 10 9  In Java we use either double or float

3  A floating point number is represented in 3 parts: M x B E M  mantissa, represented as a number that is >=0 and <1 B  base (2 is binary, 10 is decimal) E  exponent (the power of the base as a positive or negative)

4  M x B E  eg: 0.06779  Scientific notation uses one significant digit before the decimal point, multiplied by the base to the appropriate power  eg: 6.779 x 10 -2  Normalised notation has no significant digits before the decimal point, multiplied by the base to the appropriate power  eg: 0.6779 x 10 -1

5  Floating point numbers are stored in normalised form as this is the most efficient way.  Examples of normalisation (decimal) : 0.00156 x 10 -4  0.156 x 10 -6 1732.1  0.17321 x 10 4 164 x 10 -2  0.164 x 10 1 794 x 10 -5  0.794 x 10 -2

6  Examples of normalisation (binary) : 1101 x 2 100  0.1101 x 2 1000 1101.001 x 2 -111  0.1101001 x 2 -011 Note: This examples have used unsigned binary, with a minus symbol where the exponent is negative

7  Normalising a number: Move the decimal point to the left or right to ensure that there is a 0 to the left of the decimal point and a number 1-9 on the right of the decimal point Count how many places the decimal point moved If the decimal point moved to the left (decreasing the size of the mantissa) you increase the exponent (add to the powers) If the decimal point moved to the right (increasing the size of the mantissa) you decrease the exponent (subtract the powers)

8  Practice normalising the following decimal numbers: Left + / Right - 45.1 x 10 1 = 0.063 x 10 3 = -178.135 x 10 -8 = 0.0076 x 10 4 = 1200.21 x 10 -12 =

9  Practice normalising the following decimal numbers: Left + / Right - 45.1 x 10 1 = 0.451 x 10 3 (moved 2x to the left, so add 2) 0.063 x 10 3 = 0.45163 x 10 2 (moved 1x to the right, so minus 1) -178.135 x 10 -8 = -0.178135 x 10 -5 0.0076 x 10 4 = 0.76 x 10 2 1200.21 x 10 -12 = 0.120021 x 10 -8

10  Practice normalising the following binary numbers (just put in a minus for negatives) : Left + / Right - 101.1 x 2 10 = -0.01101 x 2 -101 = 1000.01 x 2 1100 = 0.0001 x 2 -100 = -1101001 = Note: If no exponent is provided, then you start at 0

11  Practice normalising the following binary numbers (just put in a minus for negatives) : Left + / Right - 101.1 x 2 10 = 0.1011 x 2 101 -0.01101 x 2 -101 = -0.1101x 2 -110 1000.01 x 2 1100 = 0.100001 x 2 10000 0.0001 x 2 -100 = 0.1 x 2 -111 -1101001 = -0.1101001 x 2 111 Note: If no exponent is provided, then you start at 0

12  Floating Point Numbers require the computer to store the mantissa, exponent and sign  There needs to be a balance between: Precision Range Total memory required for storage TypeSizeLargest ValueSmallest ValuePrecision float32 bits  3.4  10 38  1.4  10 -45 6-7 sig figs double64 bits  1.8  10 308  4.9  10 -324 14-15 sig figs

13  Lets look at some examples of floating point representation using a 16-bit word  Example 1: 16-bit word with 5-bit 2’s complement exponent and normalised mantissa 0.1 x 2 01 To convert that to decimal 0.1 == 0.5 2 01 == 2 0.5 x 2 = 1 Sign Bit of mantissa (1 bit) Exponent in 2’s complement (5 bits) Normalised mantissa (10 bits) 00000110 0000 0000

14  Lets look at more examples: -0.1101 x 2 0 To convert that to decimal -0.1101 == -0.8125 2 0 == 1 -0.8125 x 1 = -0.8125 Sign Bit of mantissa (1 bit) Exponent in 2’s complement (5 bits) Normalised mantissa (10 bits) 10000011 0100 0000

15  Lets look at more examples: -0.101 x 2 11110 To convert that to decimal -0.101 == -0.625 11110 == -2 2 -2 == 0.25 -0.625 x 0.25 = -0.15625 Sign Bit of mantissa (1 bit) Exponent in 2’s complement (5 bits) Normalised mantissa (10 bits) 11111010 1000 0000

16  Problems with floating point numbers Floating point numbers are often an inexact representation Many decimals ie. 0.1, 0.2 & 0.4 are recurring binary fractions and cannot be represented exactly Also when carrying out calculations, the size of the mantissa changes Two examples using a 4 digit mantissa: 0.6152 x 10 3 0.6152 x 10 5 - 0.6151 x 10 3 x 0.6151 x 10 5 ----------------------------- ----------------------------- 0.0001 x 10 3 = 0.1 x 10 0 0.3875 x 10 5 (Should be 0.387471..so it loses precision)

17  Complete info booklet questions: Page 13  Helpful Hint: Always convert a decimal to binary BEFORE attempting to normalise


Download ppt "Floating Point Numbers.  Floating point numbers are real numbers.  In Java, this just means any numbers that aren’t integers (whole numbers)  For example…"

Similar presentations


Ads by Google