מבנה מחשב תרגול 2 ייצוג מספרים רציונליים
תמר שרוט, נועם חזון 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:
תמר שרוט, נועם חזון 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 Mantissa Base Exponent
תמר שרוט, נועם חזון Floating Point If we look at 123: The same goes for (fixed point) :
תמר שרוט, נועם חזון Floating Point Using the form:
תמר שרוט, נועם חזון Binary Rationals Converting from any radix to decimal is done as before. So, for instance, the binary number :
תמר שרוט, נועם חזון Binary Rationals For converting from decimal to binary, we’ll use a polynomial of base 2: So, for instance 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 And so on… …
תמר שרוט, נועם חזון 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 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 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 true value of a exponent is unsigned exponent representation bit 8 bits23 bits signexponent+127mantissa
תמר שרוט, נועם חזון Float Limitations 0 is represented with mantissa=0 and “computer” exponent=0. Max absolute value (all 1’s in mantissa and 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. 1 bit 8 bits23 bits signexponent+127mantissa
תמר שרוט, נועם חזון 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 true value of a exponent is unsigned exponent representation Double 1 bit 11 bits52 bits signexponent+1023mantissa
תמר שרוט, נועם חזון 0 is represented with mantissa=0 and “computer” exponent=0. Max absolute value (all 1’s in mantissa and 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. Double Limitations 1 bit 11 bits52 bits signexponent+1023mantissa
תמר שרוט, נועם חזון Examples (1) Convert the following float to decimal: 0xc = As float parts: With hidden bit: (1.) As decimal: Real exp.: - 7 Sum up: 1 bit 8 bits23 bits
תמר שרוט, נועם חזון Examples (2) Convert to float: As polynomial: Factor out: As parts: + 3 (1.) Represented as: …0 Binary exp.: …0 1 bit 8 bits23 bits