Download presentation
Presentation is loading. Please wait.
Published byDana Martin Modified over 9 years ago
1
Page 1 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Floating-Point (real) numbers Consider the Number:4,321.42 The number could be rewritten as: 4,321.42 = 432.142 * 10 = 432.142 * 10 1 = 432.142E+1 = 43.2142 * 100 = 43.2142 * 10 2 = 43.2142E+2 = 4.32142 * 1000 = 4.32142 * 10 3 = 4.32142E+3 =.432142 * 10000 =.432142 * 10 4 =.432142E+4 What Difference does it make??
2
Page 2 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types If we consider the last notation:.432142E+4 Notice that there are three components: The Sign: In this case plus (implied) The Value (or Mantissa) The Exponent (or Characteristic of the exponent): The power to which we must raise our base (in this case, base 10) How are these components Stored?? When storing real (floating-point) numbers, we MUST keep track of each of the components
3
Page 3 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types The sign is easy: As with integers, we need only 1 bit. If we use 2-bytes (16-bits), as we did with integers, we have have 15-bits left to distribute among the remaining two components (The Mantissa and the Characteristic of the Exponent) How How to distribute the remaining bits is more complex. THREE There are THREE other considerations: The Mantissa can only take on POSITIVE Values (The Sign-bit will indicate if the value is positive or negative) BOTHThe Characteristic of the Exponent (or Characteristic) can take on BOTH positive and negative values) For every bit we allocate to the Mantissa, there is 1 (one) less bit available for the Characteristic
4
Page 4 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types What’s the trade-off between allocating bits to the Mantissa or the Characteristic (of the exponent) ??? Consider the following Number:.8072 E+11 Where: MantissaCharacteristic The actual Number is: 80,720,000,000 First, Let’s consider the Mantissa (.8072): To represent the number 8072, we need 13-bits Why ??? Because: 2 12 = 4096 (Meaning the numbers 0 … 4095) 2 13 = 8192 (Meaning the numbers 0 … 8191) 2 14 = 16384 (Meaning the numbers 0 … 16383)
5
Page 5 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types BUT, The Mantissa is a decimal (i.e.,.8072) ???? TRUE.But what if the number were:.80716 Which is SMALLER BUT Requires MORE Digits to represent Could We Represent the decimal with 13-bits ??? NO. To Represent the decimal.80716 we need: n = log(I) log(2) = 4.907 0.301 = 16.302= 17 bits Mantissa The more the number of bits allocated to the Mantissa, The greater the Level of Precision
6
Page 6 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types IF we allocate 13-bits to the Mantissa: we have 3 decimals of precision IF we allocate 17-bits to the Mantissa: we have 5 decimals of precision That can’t be true! With 13-bits we can represent numbers to 8191. There are 4-digits in 8191. With 17- bits we can represent numbers to 2 17 - 1 = 131071. There are 6-digits in the number 131071. YES - That’s True. …. BUT …. …. BUT …. Given 13-bits, can we represent the number 9235 ?? (also a 4-digit Number) Given 17-bits, can we represent the number 131072 ?? (also a 6-digit Number)
7
Page 7 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types How do we interpret the Characteristic ??? Characteristic The more the number of bits allocated to the Characteristic, The greater the Magnitude Assume that we have the choice of allocating EITHER 4 or 5 bits to the Characteristic. Since the characteristic can be either positive or negative, the ranges are: 4-bits Allocated: -2 3 to +2 3 - 1 OR -8 to +7 5-bits Allocated: -2 4 to +2 4 - 1 OR -16 to +15 So ???
8
Page 8 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Which is the larger Number ??.124E+7 = 1240000 (The largest characteristic available with 4-bits) OR.124E+15 = 124000000000000 (The largest characteristic available with 5-bits) REMEMBER: DOUBLE Each time we add 1 additional bit, we DOUBLE the size of the number as can represent
9
Page 9 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types Which is the SMALLER Number ??.124E-8 = 0.00000000124 (The largest negative characteristic available with 4-bits) OR.124E-16 = 0.0000000000000000124 (The largest negative characteristic available with 5-bits)..... SIMILARLY..... Large OR Small Magnitude refers to how Large OR How Small the numbers which can represent are.
10
Page 10 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types What’s More Important: Precision OR Magnitude ?? Depends on Individual Needs: When Dealing with Measures such as PI ( ), Precision. When Dealing with (e.g) Number of atoms in a jar: Magnitude How ARE the bits allocated ??? FIRST, we have to determine how many bits we should use Let’s assume that 16-bits (the same number we used for integers) is sufficient
11
Page 11 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types ALL Let’s look at ALL possible combinations: Mantissa Bits 1 Mantissa Range 0 … 2 1 -1 => 0.. 1 Characteristic Bits 14 Characteristic Range -2 13 -2 13 - 1 = -8,192 to 8,191 2 0 … 2 2 -1 => 0.. 3 13-2 12 -2 12 - 1 = -4,096 to 4,095 3 0 … 2 3 -1 => 0.. 7 12-2 11 -2 11 - 1 = -2,048 to 2,047 4 0 … 2 4 -1 => 0.. 15 11-2 10 -2 10 - 1 = -1,024 to 1,023 5 0 … 2 5 -1 => 0.. 31 10-2 9 -2 9 - 1 = -512 to 511 6 0 … 2 6 -1 => 0.. 63 9-2 8 -2 8 - 1 = -256 to 255 7 0 … 2 7 -1 => 0.. 127 8-2 7 -2 7 - 1 = -128 to 127 8 0 … 2 8 -1 => 0.. 255 7-2 6 -2 6 - 1 = -64 to 63 9 0 … 2 9 -1 => 0.. 511 6-2 5 -2 5 - 1 = -32 to 31 10 0 … 2 10 -1 => 0.. 1,023 5-2 4 -2 4 - 1 = -16 to 15 11 0 … 2 11 -1 => 0.. 2,047 4-2 3 -2 3 - 1 = -8 to 7 12 0 … 2 12 -1 => 0.. 4,095 3-2 2 -2 2 - 1 = -4 to 3 13 0 … 2 13 -1 => 0.. 8,191 2-2 1 -2 1 - 1 = -2 to 1 14 0 … 2 14 -1 => 0.. 16,387 1-2 0 -2 0 - 1 = -1 to 0 What’s the Solution ???
12
Page 12 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types ALL ALL of the combinations are Inadequate: We Need More bits How Many More ??? It would make sense to see what would happen if we added on another 16-bits (2-bytes) How will that change things ??? What can we do ??? We still need 1 (one) bit for the Sign, but we will now have the remaining 31-bits (4-bytes - 1-bit = 32-bits - 1 = 31-bits) to distribute between the Mantissa and the Characteristic.
13
Page 13 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types SOME Let’s look at SOME combinations if 31-bits are allocated: Mantissa Bits 15 Precision (decimals) 32,767 (4 decimalss) Characteristic Bits 16 Magnitude (E-Vals) -2 15 -2 15 - 1 = -32,786 to 32,785 16 65,535 (4 decimals) 15-2 14 -2 14 - 1 = -16,384 to 16,383 17 131,071 (5 decimals) 14-2 13 -2 13 - 1 = -8.192 to 8,191 18 262,143 (5 decimals) 13-2 12 -2 12 - 1 = -4,096 to 4,095 19 524,288 (5 decimals) 12-2 11 -2 11 - 1 = -2,048 to 2,047 20 1,048,576 (6 decimals) 11-2 10 -2 10 - 1 = -1,024 to 1,023 21 2,097,152 (6 decimals) 10-2 9 -2 9 - 1 = -512 to 511 22 4,194,304 (6 decimals) 9-2 8 -2 8 - 1 = -256 to 255 23 8,388,608 (6 decimals) 8-2 7 -2 7 - 1 = -128 to 127 24 16,777,216 (7 decimals) 7-2 6 -2 6 - 1 = -64 to 63 25 33,554,432 (7 decimals) 6-2 5 -2 5 - 1 = -32 to 31 26 67,108,864 (7 decimals) 5-2 4 -2 4 - 1 = -16 to 15 27 134,217,728 (8 decimals) 4-2 3 -2 3 - 1 = -8 to 7 28 268,435,456 (8 decimals) 3-2 2 -2 2 - 1 = -4 to 3
14
Page 14 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types What’s the best Combination ??? There is no clear-cut answer. One common allocation scheme is: 1 - bit7 - bits24 - bits Sign Characteristic of the Exponent Mantissa What Range of Numbers can we represent ??? Real Numbers are characterized by their LEVEL OF PRECISION, PRECISION, Not range This scheme allows for 7 decimals of precision
15
Page 15 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Basic Data Types OK - But what is the LARGEST (positive) number we can represent ???.(2 24 - 1) E (26 (26 -1) =.16777216 E 63 = 167,772,160,000,000,000,000,000,000,000,000,000,000,000, 000,000,000,000,000,000,000 What is the SMALLEST number we can represent ??? -.1 E -(2 6 ) =.1 E -64 = -.00000000000000000000000000000000000000000000000000 00000000000000001 And In Between ??? ALL values: B BB BUT only to 7 (or 8) decimals of precision.
16
Page 16 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Repeat Slides for this SectionRepeat Slides for this Section Go To Next Set of Slides For this ChapterGo To Next Set of Slides For this Chapter Go To Slide Index For Chapter 2Go To Slide Index For Chapter 2 Go To Slide Index For Chapter 3Go To Slide Index For Chapter 3 Go To Slide Index For TextbookGo To Slide Index For Textbook Go To Home PageGo To Home Page This Concludes The Slides for this Section Choose an Option:
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.