behaviour of some data types
play

Behaviour of some data-types In this laboration you will visualize - PDF document

Linkping institute of technology Department of Computer and Information Science (IDA) Klas Arvidsson 2010-10-13 Behaviour of some data-types In this laboration you will visualize and investigate some peculiarities of different data-types


  1. Linköping institute of technology Department of Computer and Information Science (IDA) Klas Arvidsson 2010-10-13 Behaviour of some data-types In this laboration you will visualize and investigate some peculiarities of different data-types Time required This assignment is expected to require at most 30 hours. You will execute the given expressions, observe the results and make some conclusions by answering the questions. Objective • become aware of floating point inaccuracy and rounding errors • become aware of integer limitations • learn in which order to perform operations to minimize errors • be able to accurately predict integer over- or underflow (exceeding range) • learn how to use integers and how to activate warnings Assignment 1 Investigate floating point behaviour Most of the following code can be pasted directly to Matlab command window. Mark it and then press the middle mouse button to paste it. Set matlab to show all decimals: format long Create variables x and k with initial values: x = 6/7 k = 4 With the command whos you can see x and k are of type (class) double. Double is the default type for all variables and calculations in Matlab. If you do not explicitly specify a different type, double is what you get. whos How many bytes is required to store one double variable? _________________________(1) Consider this expression. z = x + 2^k - 2^k; What result do you expect? _________________________(2) Page 1

  2. 2010-10-13 If you subtract the expected result from the actual we get the magnitude of the error: disp(z - x); What result did you get? _________________________(3) z = x + 2^k - 2^k; The expression above seems to generate a small error. Try to do the calculation in the expression in some different orders. Insert parentheses to modify the order of calculation. Which order of calculation gives a better result, write down the modified expression: ____________________(4) What do you think is the cause of the error? Mark all correct options. (5a) Rounding errors from storing the floating point numbers in binary form with limited bits. (5b) The exponentiation in 2^k. (5c) The addition. (5d) The subtraction. Modify k and investigate how the error is affected: k = 32; z = x + 2^k - 2^k; disp(z - x); What is the result? _________________________(6) Is the error larger or smaller than before? _________________________(7) k = -4; z = x + 2^k - 2^k; disp(z - x); Is the error larger or smaller than with k = 4 ? _________________________(8) There seems to be a relationship between the magnitude of 2^k and the error. Create a table of the error at different values for 2^k . fprintf(’%7s%16s%20s\n’, ’x’, ’2^k’, ’error’); for k = -4:2:32 fprintf(’%7.5f%16.2f%20.16f\n’, x, 2^k, abs(x+2^k-2^k-x) ); end Describe the relationship between 2^k and the error? _________________________(9) Page 2

  3. 2010-10-13 Let us look at some other calculations before making any conclusions. To get an approximation of the error in the following calculations we first do the calculation in single precision ( sp_sum ) and then in double precision ( dp_sum ). Now we can use the difference as a rough error estimation. The tables produced below will show four columns. The first number of an addition, the second number in an addition, the difference in magnitude of the first two numbers, and the estimated error in the sum. for k = pi/6:pi/3:20 sp_sum = single(2^k) + single(2^(20-k)); dp_sum = 2^k + 2^(20-k); fprintf(’%12.2f%14.2f%10.4f%16.12f\n’, 2^k, 2^(20-k), ... abs(log10(2^k) - log10(2^(20-k))), ... abs(dp_sum - sp_sum)); end for k = pi/3:2*pi/3:40 sp_sum = single(2^k) + single(2^(32-k)); dp_sum = 2^k + 2^(32-k); fprintf(’%16.2f%14.2f%10.4f%20.12f\n’, 2^k, 2^(32-k), ... abs(log10(2^k) - log10(2^(32-k))), ... abs(dp_sum - sp_sum)); end Do you observe any relationship between the relative difference of the added numbers (expressed logarithmically) and the magnitude of the error? _________________________(10) When do you, in general, observe the smallest error? (Pick the single most correct answer.) (11a) When one of the numbers are close to 1000. (11b) When one of the numbers are close to 80000. (11c) When the magnitude of the numbers are similar. (11d) When there is a large difference in magnitude. Theoretically, in which order should you add the numbers 7.0, 2.0, 4.0, 2.0 to get the most accurate result? (practically the result will not differ in this case) _________________________(12) Make sure you are still using format long (you see 16 decimals). A student have the assignment of adding 1e16 + 1e5 + 99999 + 99998 + ... + 1 On the next page four different calculations of this sum is shown. They will give slightly different result. Which version is correct? ________________________(13) What is wrong with each of the failing versions? _________________________(14) Page 3

  4. 2010-10-13 % version 1 sum = 0; for i = 1:100000 sum = sum + i; end disp(sum+1e16); % version 2 sum = 0; for i = 100000:-1:1 sum = sum + i; end disp(sum+1e16); % version 3 sum = 1e16; for i = 1:100000 sum = sum + i; end disp(sum); % version 4 sum = 1e16; for i = 100000:-1:1 sum = sum + i; end disp(sum); Initiate x to a large double precision floating point number: x = 1.00e+16 + 10; disp(x); The last digit displayed is the tens digit. Look at it. It should be 1 . Now repeatedly add the value 3 to the variable x. Do it 10 times: for i = 1:10 x = x + 3; end disp(x); Page 4

  5. 2010-10-13 Look at the last visible decimal of x again. It should be: _____(15) But it is: _____(16) Do the addition ten more times: for i = 1:10 x = x + 3; end disp(x); You have now in total added 60 to the initial value of x . The last visible decimal of x should be: _____(17) The last visible decimal of x is: _____(18) When you add 3 to x , how much is in fact added? _____(19) Any idea why the simple addition of the integer 3 does not work correct? Hint: Think of everything you have done up to this point. ____________________________________________________(20) This will conclude the investigation of floating point numbers. You have seen: • That calculations with floating point numbers are not always exact. • That the order of calculation may be important to reduce errors. This behaviour is not specific to Matlab, but occur in any computer and any programming language that store floating point numbers. We have however found a special as of yet unexplained Matlab- specific flaw: The remainder when dividing a small number with a larger should always be the small number unmodified. This can be verified by subtracting the small number again, like so: mod(0.1, 3.14) - 0.1 But Matlab have trouble sometimes, with some denominators: mod(0.1, pi) - 0.1 The above expression should of course be zero, but have a small deviation. An equivalent expression to calculate the remainder, and that gives the correct result also when built-in mod have trouble: function r = mod(x, y) r = x - y*floor(x/y); If you have a theory to explain this behaviour of the built-in mod , speak up. Page 5

  6. 2010-10-13 Assignment 2 Investigate integer behaviour Since integers are not used by default in Matlab you have to specifically tell matlab to store numbers as integers. Once an integer number is used in an expression all calculations done with that number will be integer and the result will be a new integer. Matlab have two special constants that are already integer. They are: intmin The value of intmin is: _________________________(21) intmax The value of intmax is: _________________________(22) They represent the maximum and minimum integers possible to use. Any integer calculations inside this range is always exact, with no calculation error and no rounding errors. The only risk of error is if some calculation have (perhaps intermediate) results outside the range. In matlab it is possible to receive warnings if this occur. Turn those warnings on now: intwarning(’on’) To create integers you must use the function int32: int32(7) % The integer 7 int32(7.9) % The integer 8 (rounded), observe the warning int32(7) / 2 int32(7) / 3 int32(10) / 2.5 int32(10) / int32(2.5) % observe the warning The operations above are various divisions involving integers in Matlab. A common approach in programming languages to perform division with integers is to remove any decimals if the result is floating point. How does Matlab handle this? (23a) Remove decimals, result is closest lower integer (23b) Round up always, result is closest higher integer (23c) Round, result is closest higher or lower integer The denominator in the divisions are in three cases floating point. How does it work in Matlab? (24a) Division is done floating point and the result is converted to integer. (24b) Operands are first converted to integer and then the division is done. Page 6

Recommend


More recommend