/* CS 341 Spring 2002 */ /* Note segment 7 */ /* 19-March-2002 */ /* Taken by Apostolos Paul Pantazis */ --> Floating Point numbers * Look up fixed point representation & scaled integers. * FPn = Floating Point number('s). --> What is an FPn? a hack to do fixed point with a variable scaling factor. Consists of: (this is for fixed point numbers). 1. actuall digits - int - 2. scale factor - R(reall num) known at "compile time". FPn --> both digits and scale factor are not known until runtime. With FPn's there is always the chance of turning te answer to garbage Here are some DONT's for FPn's: well rounding will cause some precision loss... Consider the following example: we have 2 FPn: x -> x.xxxx * 10^(n) y -> y.yyyy * 10^(n) | | --> Feed them in an some arithmetic operation: | | --> you get a number z.zzzz * 10^(p) we would like to think of x&y and an arithmetic operation so that teh result is not accurate to 5 digits. Ex: The 2 identical twins but height: Twin_1 == 1.9732 Twin_2 == 1.9721 == 1.1 * 10^(-3) this could be written as: 1.1000 * 10^(-3. The 3 0's are made up so that from a 5 digit precision we have gone down to 2 digits precision, that is 1.1. ** THE ERROR IN PRECISION WAS CAUSED BY SUBTRACTING 2 NUMBERS THAT WERE NEARLY IDENTICAL ** ** YOU SHOULD NOT TAKE A BIG NUMBER AND ADD A SMALL NUMBER TO IT ** " Computers do things in 2 ways: 1. either totaly correct 2. OR totaly wrong...(really wrong !) in which case we spent most our time debugging to fix the flaw." .Barak P Tuesday March 19th 2001 .- Seymore Cray --> speed is what matters not precision.. IEEE FP standard --> Never loose precision when you dont have to.. FP on a decimal (base 10) computer: 0...99,999,999 signed 10's complement: -50,000,000 ... 49,999,999 m4 m3 m2 m1 m0 l2 l1 l0 /* in memory */ m4 to m0 are the digit of the number l2 to l0 is the exponent The M's are refered to as the Mantissa. m4*m3*m2*m1*m0 * 10^(l2*l1*l0) 4 SUM m1* 10^(i) * 10^( *1 ). i = 0 *1 --> 2 SUM e*j*10^(j) j =0 Adding 2 floating Point numbers: if(exp == same) { add Mantissas and + exp back on... } Consider the following example: 4.1231 * 10^(2) 8.1111 * 10^(2) ------ 122.2342 * 10^(2) not in format to put back in a word so we say 12.2342 * 10^(2) is equivelant to 1.22342 the 10^(2) became a 10^(3) to cover for moving the decimal point to 1. So actuall number in the computer are: 1.2234 (Mantissa) 003 (Exponent). Normalization: Chewing a FPn by moving a decimal point to were we need it to be so it will fit our representation. Adding 2 numbers without same exponent.. Pretend that we are normalizing until the exp match. 2.0041 * 10^(3) 1.0012 * 10^(0) --------------- 0.1001 * 10^(1) 0.0100 * 10^(2) 0.0010 * 10^(3) --------------- (+) 2.0051 * 10^(3) Multiplication: (x * 10^(n)) * (y * 10^(n)) = x*y*10^(n + m) Division == do not de-normalize before doing division. Negative numbers: (sign_bit) Mantissa(unsigned) exponent.