Fixed Point vs Floating Point n n Weve

  • Slides: 23
Download presentation

Fixed Point vs. Floating Point n n We’ve already seen two ways to represent

Fixed Point vs. Floating Point n n 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: 0 1 1 1 0 1 נועם חזון , תמר שרוט

Floating Point n n Going back to decimal… Sometimes it is more comfortable to

Floating Point n n Going back to decimal… Sometimes it is more comfortable to represent a value using a floating point. ¨ For instance: Exponent Mantissa n Generally, we use the form נועם חזון , תמר שרוט Base

Floating Point n If we look at 123: ¨ n The same goes for

Floating Point n If we look at 123: ¨ n The same goes for (fixed point) 123. 456: ¨ נועם חזון , תמר שרוט

Floating Point n Using the form: ¨ ¨ נועם חזון , תמר שרוט

Floating Point n Using the form: ¨ ¨ נועם חזון , תמר שרוט

Binary Rationals n Converting from any base to decimal is done as before. ¨

Binary Rationals n Converting from any base to decimal is done as before. ¨ So, for instance, the binary number 100. 101: נועם חזון , תמר שרוט

Binary Rationals n For converting from decimal to binary, we’ll use a polynomial of

Binary Rationals n For converting from decimal to binary, we’ll use a polynomial of base 2: ¨ So, n for instance 20. 75 to binary: What about converting to floating point? נועם חזון , תמר שרוט

Binary Rationals n n Here, of course In order to transform the result to

Binary Rationals n n Here, of course In order to transform the result to floating point, we’ll continue from here: נועם חזון , תמר שרוט

Binary Rationals n Problem: how can we convert simple fractions to binary? Binary representation

Binary Rationals n 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 n n write “ 0. ” while (true) do: ¨

Algorithm for Simple Fractions n n write “ 0. ” while (true) do: ¨ If n Break ¨ else n n If ¨ write “ 1” ¨ n else write “ 0” נועם חזון , תמר שרוט

Algorithm for Simple Fractions n For instance: ¨ 0. 010 n And so on…

Algorithm for Simple Fractions n For instance: ¨ 0. 010 n And so on… ¨ 0. 0101… נועם חזון , תמר שרוט

Algorithm for Simple Fractions n From here, converting to floating point is easy: נועם

Algorithm for Simple Fractions n From here, converting to floating point is easy: נועם חזון , תמר שרוט

Binary Rationals n Problems: ¨ This algorithms can run to infinity. ¨ Furthermore, we

Binary Rationals n Problems: ¨ This algorithms can run to infinity. ¨ Furthermore, we do not have an endless supply of digits. n 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 n n write “ 0. ” for each available

Fixed Algorithm for Simple Fractions n n write “ 0. ” for each available digit to fraction part do: ¨ If n Break ¨ else n n If ¨ write “ 1” ¨ n else write “ 0” נועם חזון , תמר שרוט

Mixed Part Numbers n For mixed whole and simple fraction parts numbers, like :

Mixed Part Numbers n 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 n n n So how many digits do we really have?

Float vs. Double n n n 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 n As mentioned before, we use the form: n Where In decimal,

Hidden Bit n As mentioned before, we use the form: n 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” n n נועם חזון , תמר שרוט

Float 31 30 23 22 sign exponent+127 1 bit n n 0 mantissa 8

Float 31 30 23 22 sign exponent+127 1 bit n n 0 mantissa 8 bits 23 bits 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 - 127. נועם חזון , תמר שרוט

Float Limitations 31 30 23 22 sign exponent+127 1 bit n n 0 mantissa

Float Limitations 31 30 23 22 sign exponent+127 1 bit n n 0 mantissa 8 bits 23 bits 0 is represented with mantissa=0 and “computer” exponent=0. Max absolute value (all 1’s in mantissa and 11111110 exponent): n Min absolute value (0 in mantissa and 1 as “computer” exponent): n “Computer” exponent=0 and mantissa different from 0 represent sub-normal numbers “Computer” exponent=255 represent and Nan. n נועם חזון , תמר שרוט

Double 63 62 52 51 sign exponent+1023 1 bit n n 0 mantissa 11

Double 63 62 52 51 sign exponent+1023 1 bit n n 0 mantissa 11 bits 52 bits 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 - 1023. נועם חזון , תמר שרוט

Double Limitations 63 62 52 51 sign exponent+1023 1 bit n n n 0

Double Limitations 63 62 52 51 sign exponent+1023 1 bit n n n 0 mantissa 11 bits 52 bits 0 is represented with mantissa=0 and “computer” exponent=0. Max absolute value (all 1’s in mantissa and 111110 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. נועם חזון , תמר שרוט

Examples (1) 31 1 30 10000110 1 bit n 23 22 0 101000000000 8

Examples (1) 31 1 30 10000110 1 bit n 23 22 0 101000000000 8 bits 23 bits Convert the following float to decimal: ¨ ¨ ¨ 0 xc 3528000 = 1100 0011 0101 0010 1000 0000 As float parts: 1 10000110 101000000000 With hidden bit: (1. )10100101 As decimal: 134 Real exp. : 7 Sum up: נועם חזון , תמר שרוט

Examples (2) 31 0 30 0 10000010 1 bit n 23 22 100000000000 8

Examples (2) 31 0 30 0 10000010 1 bit n 23 22 100000000000 8 bits 23 bits Convert 12. 125 to float: ¨ ¨ ¨ As polynomial: Factor out: As parts: Represented as: Binary exp. : + 3 0 130 0 10000010 נועם חזון , תמר שרוט (1. )1000010… 0