230 likes | 486 Views
מבנה מחשב. ייצוג מספרים רציונליים. Fixed Point vs. Floating Point. We’ve already seen two ways to represent a positive integer in computer hardware: signed and unsigned. Both ways were with a fixed point representation - the location of the binary point was fixed:. Exponent. Mantissa.
E N D
מבנה מחשב ייצוג מספרים רציונליים
Fixed Point vs. Floating Point • We’ve already seen two ways to represent a positive integer in computer hardware: signed and unsigned. • Both ways were with a fixed point representation - the location of the binary point was fixed: תמר שרוט, נועם חזון
Exponent Mantissa Base Floating Point • Going back to decimal… • Sometimes it is more comfortable to represent a value using a floating point. • For instance: • Generally, we use the form תמר שרוט, נועם חזון
Floating Point • If we look at 123: • The same goes for (fixed point) 123.456: תמר שרוט, נועם חזון
Floating Point • Using the form: תמר שרוט, נועם חזון
Binary Rationals • Converting from any base to decimal is done as before. • So, for instance, the binary number 100.101: תמר שרוט, נועם חזון
Binary Rationals • For converting from decimal to binary, we’ll use a polynomial of base 2: • So, for instance 20.75 to binary: • What about converting to floating point? תמר שרוט, נועם חזון
Binary Rationals • Here, of course • In order to transform the result to floating point, we’ll continue from here: תמר שרוט, נועם חזון
Binary Rationals • Problem: how can we convert simple fractions to binary? Binary representation might require infinite number of digits. • For example: • We have an algorithm. תמר שרוט, נועם חזון
Algorithm for Simple Fractions • write “0.” • while (true) do: • If • Break • else • If • write “1” • else write “0” תמר שרוט, נועם חזון
Algorithm for Simple Fractions • For instance: • 0. • 0.0 • 0.01 • 0.010 • And so on… • 0.01010101… תמר שרוט, נועם חזון
Algorithm for Simple Fractions • From here, converting to floating point is easy: תמר שרוט, נועם חזון
Binary Rationals • Problems: • This algorithms can run to infinity. • Furthermore, we do not have an endless supply of digits. • Solution: • Run the main loop the number of times as the number of digits you have for the fraction part. תמר שרוט, נועם חזון
Fixed Algorithm for Simple Fractions • write “0.” • for each available digit to fraction part do: • If • Break • else • If • write “1” • else write “0” תמר שרוט, נועם חזון
Mixed Part Numbers • For mixed whole and simple fraction parts numbers, like : • Convert the integer part to binary as we learned on integers. • Convert the fraction part as learned now. • Add the results. • Only now, if desired, convert to floating point. תמר שרוט, נועם חזון
Float vs. Double • So how many digits do we really have? • Depends on the representation. We have two possible representations for floating point values: 4-byte float and 8-byte double. • It all depends on the amount of accuracy we need. תמר שרוט, נועם חזון
Hidden Bit • As mentioned before, we use the form: • Where • In decimal, every digit would have values in the range 0..9 besides which have values in range 1..9. • Likewise, in binary, could only have the value of 1. • So why should we save it? • Since we won’t save it, we’ll refer to it as the “hidden bit” תמר שרוט, נועם חזון
Float 31 30 23 22 0 • 32-bit (4-byte) representation. • 1 bit for sign: 1 for negative, 0 for positive. • 23 bits for mantissa. • 8 bits for the exponent.Important: The truevalue of a exponent isunsigned exponent representation - 127. 8 bits 23 bits 1 bit תמר שרוט, נועם חזון
Float Limitations 31 30 23 22 0 • 0 is represented with mantissa=0 and “computer” exponent=0. • Max absolute value (all 1’s in mantissa and 11111110 exponent): • Min absolute value (0 in mantissa and 1 as “computer” exponent): • “Computer” exponent=0 and mantissa different from 0 represent sub-normal numbers • “Computer” exponent=255 represent and Nan. 8 bits 23 bits 1 bit תמר שרוט, נועם חזון
Double 63 62 52 51 0 • 64-bit (8-byte) representation. • 1 bit for sign: 1 for negative, 0 for positive. • 52 bits for mantissa. • 11 bits for the exponent.Important: The truevalue of a exponent isunsigned exponent representation - 1023. 11 bits 52 bits 1 bit תמר שרוט, נועם חזון
Double Limitations 63 62 52 51 0 • 0 is represented with mantissa=0 and “computer” exponent=0. • Max absolute value (all 1’s in mantissa and 11111111110 exponent): • Min absolute value (0 in mantissa and 1 as “computer” exponent): • “Computer” exponent=0 and mantissa different from 0 represent sub-normal numbers • “Computer” exponent=2047 represent and Nan. 11 bits 52 bits 1 bit תמר שרוט, נועם חזון
Examples (1) 31 30 23 22 0 • Convert the following float to decimal: • 0xc3528000 = 1100 0011 0101 0010 1000 0000 0000 0000 • As float parts: 1 10000110 10100101000000000000000 • With hidden bit: (1.)10100101 • As decimal: - 134 • Real exp.: - 7 • Sum up: 8 bits 23 bits 1 bit תמר שרוט, נועם חזון
Examples (2) 31 30 23 22 0 • Convert 12.125 to float: • As polynomial: • Factor out: • As parts: + 3 (1.)100001 • Represented as: 0 130 1000010…0 • Binary exp.: 0 10000010 1000010…0 8 bits 23 bits 1 bit תמר שרוט, נועם חזון