Floating Point Numbers. Floating point numbers are real numbers. In Java, this just means any...

17
Binary 5 Floating Point Numbers

Transcript of Floating Point Numbers. Floating point numbers are real numbers. In Java, this just means any...

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

Binary 5Floating Point Numbers

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

Floating Point Numbers Floating point numbers are real

numbers. In Java, this just means any numbers

that aren’t integers (whole numbers) For example…

• 2.86 -0.5 4.000• -0.03 3.1415926 7.2 x 109

In Java we use either double or float

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

Floating Point Numbers A floating point number is

represented in 3 parts:M x BE

• 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)

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

Floating Point Numbers M x BE

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

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

Floating Point Numbers 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 104

• 164 x 10-2 0.164 x 101

• 794 x 10-5 0.794 x 10-2

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

Floating Point Numbers Examples of normalisation (binary):

• 1101 x 2100 0.1101 x 21000

• 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

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

Floating Point Numbers 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)

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

Floating Point Numbers Practice normalising the following

decimal numbers: Left + / Right -

• 45.1 x 101 = • 0.063 x 103 =• -178.135 x 10-8 = • 0.0076 x 104 = • 1200.21 x 10-12 =

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

Floating Point Numbers Practice normalising the following

decimal numbers: Left + / Right -

• 45.1 x 101 = 0.451 x 103(moved 2x to the left, so add 2)

• 0.063 x 103 = 0.45163 x 102(moved 1x to the

right, so minus 1)

• -178.135 x 10-8 = -0.178135 x 10-5 • 0.0076 x 104 = 0.76 x 102 • 1200.21 x 10-12 = 0.120021 x 10-8

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

Floating Point Numbers Practice normalising the following

binary numbers (just put in a minus for

negatives): Left + / Right -

• 101.1 x 210 = • -0.01101 x 2-101 =• 1000.01 x 21100 = • 0.0001 x 2-100 = • -1101001 =

Note: If no exponent is provided, then you start at 0

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

Floating Point Numbers Practice normalising the following

binary numbers (just put in a minus for

negatives): Left + / Right -

• 101.1 x 210 = 0.1011 x 2101 • -0.01101 x 2-101 = -0.1101x 2-110

• 1000.01 x 21100 = 0.100001 x 210000

• 0.0001 x 2-100 = 0.1 x 2-111

• -1101001 = -0.1101001 x 2111

Note: If no exponent is provided, then you start at 0

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

Floating Point Numbers 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 storageType Size Largest Value Smallest Value Precision

float 32 bits 3.4 1038 1.4 10-45 6-7 sig figs

double 64 bits 1.8 10308 4.9 10-324 14-15 sig figs

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

Floating Point Numbers 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 201 To convert that to decimal0.1 == 0.5201 == 20.5 x 2 = 1

Sign Bit of mantissa (1 bit)

Exponent in 2’s complement (5 bits)

Normalised mantissa (10 bits)

0 00001 10 0000 0000

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

Floating Point Numbers Lets look at more examples:

-0.1101 x 20 To convert that to decimal

-0.1101 == -0.812520 == 1-0.8125 x 1 = -0.8125

Sign Bit of mantissa (1 bit)

Exponent in 2’s complement (5 bits)

Normalised mantissa (10 bits)

1 00000 11 0100 0000

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

Floating Point Numbers Lets look at more examples:

-0.101 x 211110 To convert that to decimal

-0.101 == -0.62511110 == -22-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)

1 11110 10 1000 0000

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

Floating Point Numbers 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 103 0.6152 x 105

- 0.6151 x 103 x 0.6151 x 105

----------------------------- -----------------------------0.0001 x 103 = 0.1 x 100 0.3875 x 105 (Should be 0.387471..so it

loses precision)

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

Floating Point Numbers Complete info booklet questions:

Page 13

Helpful Hint:Always convert a decimal to binary BEFORE attempting to normalise