Download presentation
Presentation is loading. Please wait.
1
Floating Point Numbers
Binary 5 Floating Point Numbers
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… x 109 In Java we use either double or float
3
Floating Point Numbers
A floating point number is represented in three 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)
4
Floating Point Numbers
For example, let’s consider what happens when we write in the format M x BE Scientific notation uses one significant digit before the decimal point, multiplied by the base to the appropriate power eg: x 10-2 Normalised notation has no significant digits before the decimal point, multiplied by the base to the appropriate power eg: x 10-1
5
Floating Point Numbers
Floating point numbers are stored in normalised form as this is the most efficient way. Examples of normalisation (decimal): x 10-4 x 10-6 x 104 164 x 10-2 x 101 794 x 10-5 x 10-2
6
Floating Point Numbers
Examples of normalisation (binary): 1101 x 2100 x 21000 x x 2-011 Note: This examples have used unsigned binary, with a minus symbol where the exponent is negative
7
To “normalise” a number…
Move the decimal point to put a 0 to its left, and a non-zero digit to the right. Count how many places the decimal point moved. If the decimal point moved… left, increase the exponent right, decrease the exponent … by the same amount.
8
Floating Point Numbers
Practice normalising the following decimal numbers: 45.1 x = 0.063 x = x = x = x =
9
Floating Point Numbers
Practice normalising the following decimal numbers: 45.1 x = x 103(moved 2x to the left, so add 2) 0.063 x = 0.63 x 102(moved 1x to the right, so minus 1) x = x 10-5 x = 0.76 x 102 x = x 10-8
10
Floating Point Numbers
Practice normalising the following binary numbers (for now, just put in a minus for negatives): e.g x 2010 = x 2110 101.1 x = x = x = x = = Note: If no exponent is provided, then you start at 0
11
Floating Point Numbers
Practice normalising the following binary numbers 101.1 x = x 2101 x = x 2-110 x = x x = 0.1 x 2-111 = x 2111 Note: If no exponent is provided, then you start at 0
12
Floating Point Numbers
To store a floating number we need to allocate space for the mantissa, exponent and sign There needs to be a balance between: Precision Range Total memory required for storage Type 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 14-15 sig figs
13
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 decimal == == x 2 = 1 Sign Bit of mantissa (1 bit) Exponent in 2’s complement (5 bits) Normalised mantissa (10 bits) 00001
14
Floating Point Numbers
Lets look at more examples: x 20 To convert that to decimal == == x 1 = Sign Bit of mantissa (1 bit) Exponent in 2’s complement (5 bits) Normalised mantissa (10 bits) 1 00000
15
Floating Point Numbers
Lets look at more examples: As a normalised binary number, this is... x As a regular decimal… mantissa = = exponent = = -2 decimal number = x 2-2 = Sign Bit of mantissa (1 bit) Exponent in 2’s complement (5 bits) Normalised mantissa (10 bits) 1 11110
16
Floating Point Numbers
Lets look at more examples: As a normalised binary number, this is… x As a regular decimal… mantissa = = exponent = -13 decimal number = x 2-13 = … Sign Bit of mantissa (1 bit) Exponent in 2’s complement (5 bits) Normalised mantissa (10 bits) 1 10011
17
Floating Point Numbers
Lets look at more examples: As a normalised binary number, this is… ?x 2? As a regular decimal… mantissa = exponent = decimal number = = Sign Bit of mantissa (1 bit) Exponent in 2’s complement (5 bits) Normalised mantissa (10 bits) 01111
18
Floating Point Numbers
Lets look at more examples: As a normalised binary number, this is… x As a regular decimal… mantissa = exponent = 15 decimal number = x 215 = … Sign Bit of mantissa (1 bit) Exponent in 2’s complement (5 bits) Normalised mantissa (10 bits) 01111
19
Problems with floating point
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 When carrying out calculations, the size of the mantissa changes Two examples using a 4-digit mantissa: x x x x x x 103 = 0.1 x x 105 (Should be so it loses precision)
20
Helpful Hint Always convert a decimal to binary BEFORE attempting to normalise
21
Floating Point Numbers
Represent the following binary numbers using a 16-bit representation, with… 1 bit for the exponent sign 1 bit for the mantissa sign 6 bits for the exponent 8 bits for the mantissa 0.011 x 2100 111000 10.11 x 2-111
22
Precision When using an 8-bit two’s complement representation, which of the following decimals can be represented precisely? (that is, without any rounding off) 128.5 0.1
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.