function result = add_then_subtract(input_val, num_iter) // Repeatedly multiply and divide the given value by three, // in such a way that the end result should be identical // to the input value. // // Return the result. // result = input_val; one_third = 1.0/3.0; for icount = 1 : num_iter result = result + one_third; end for icount = 1 : num_iter result = result - one_third; end endfunction

function result = add_subtract_together(input_val, num_iter) // Repeatedly add and subtract a constant value // from the 'input_val', // in such a way that the end result should be identical // to the input value. // // Return the result. // result = input_val; one_third = 1.0/3.0; for icount = 1 : num_iter result = result + one_third; result = result - one_third; end endfunction

They look pretty similar, right? And, in theory, they ought to provide exactly the same result. But watch what happens when we run them with a large number of iterations: 100,000, to be exact.

First, we must type into the Scilab command window
the command **format ("v", 25)** in order to see
values displayed to their full internal precision.

>> format ("v", 25)

Now, we can run each function and see the results displayed to MATLAB's full precision.

>> add_subtract_together(9, 100000) ans = 9. >> add_then_subtract(9,100000) ans = 9.0000000000019202417434 >>

Not exactly the same, are they?

Copyright © Michael Richmond. This work is licensed under a Creative Commons License.