confused Assume another programmer has given you a MATLAB function called doCool
ID: 3553471 • Letter: C
Question
confused
Assume another programmer has given you a MATLAB function called doCoolCaclulation which takes a matrix as input and returns a scalar as an output. Your boss asks you to write some code to handle the results of this function, the most important case being when the results are zero. Your boss has done some MATLAB before, and he gives you some code that he thinks will get you started. Does you bosses code handle the very special case appropriately? If not, explain wny and modify the code to yield a better solution.(1.5 Marks)Explanation / Answer
Floating point inequalities are somewhat inaccurate. For instance, you may expect that 1.12345 = 1.12345 should be true, but on system such an inequality may surprisingly give you false result.
Why? Well, try another example. You know that 0.9999999999 is not equal to 1.00, but for floating point inequalities, you may get the numbers to be equal due to rounding off errors.
Solution:
Change the code inside the if condition to this:
if res>(-0.0001) & res<(0.0001)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.