Different results when passing the same value in a function.

9 views (last 30 days)
Hi guys,
I have never had this problem in MATLAB before since I assumed variable definition is not necessarily needed unlike C, C++, Fortran, etc. I am running the following code:
------------------------------------------------------------------------------------------------
clear all
format long
start_delta = 0.015;
end_delta = 0.05;
delta_step = 0.0001;
i_MAX = ceil((end_delta - start_delta)/delta_step);
axmine = double(zeros(i_MAX, 64));
aymine = double(zeros(i_MAX, 64));
azmine = double(zeros(i_MAX, 64));
delta_array = double(zeros(i_MAX, 1));
delta_array(1) = start_delta;
[axmine(1,:), aymine(1,:), azmine(1,:)] = lorenzmine(0.015);
[axmineother, aymineother, azmineother] = lorenzmine(delta_array(1));
delta_array(2) = start_delta + delta_step;
[axmine(2,:), aymine(2,:), azmine(2,:)] = lorenzmine(0.0151);
[axmineother2, aymineother2, azmineother2] = lorenzmine(delta_array(2));
------------------------------------------------------------------------------------------------
I am passing the value 0.015, 0.0151, 0.0152, etc as a parameter using the array delta_array and also as a value in the function lorenzmine(). The output from the function lorenzmine() axmine(1,:), aymine(1,:) and azmine(1,:) matches the array axmineother, aymineother and azmineother whether I pass delta_array(1) or 0.015. I would think that would be the case with 0.0151 as well, but to my surprise the array axmine(2,:) DOESNT match axmineother2, aymine(2,:) DOESNT MATCH aymineother2 and so on. It seems like passing 0.0151 as a value and as a parameter gives different results. Any input into this would be appreciated. Thanks.

Accepted Answer

Roger Stafford
Roger Stafford on 29 Jun 2014
You have apparently assumed in your code that 0.015 + 0.0001 gives precisely the same result as 0.0151 . As it turns out, they differ by 1 in their least bit. (Use format hex and try it out.) This is because these are decimal fractions and your computer is using binary floating point numbers which can only approximate each of the three numbers. In this case the sum of the two approximations does not quite equal the approximation of their sum which can easily happen and does in this case.
Therefore your function 'lorenzmine' is receiving slightly different inputs in the case in question. I have no idea what the function lorenzmine does but your assertion of "passing the same value" is incorrect and presumably that explains your different results.
  2 Comments
Roger Stafford
Roger Stafford on 29 Jun 2014
Just because "format longEng" displays two quantities with the same character string does not ensure that the quantities are exactly equal. The "format hex" is a sure-fire method of detecting inequality because it reflects the actual values of the binary bits that are present floating point numbers. Also the test "==" is another valid way of detecting inequality.
Roger Stafford
Roger Stafford on 30 Jun 2014
Let's see if I understand you correctly. You're saying that with format hex you obtained the same values for the two 'double' quantities, delta_array(2) and 0.0151, but yet you obtained different answers for lorenzmine(delta_array(2)) and lorenzmine(0.0151)? In that case you need to explain how the function lorenzmine works. Does it possess any reference to random generators such as 'rand'?
Also in your previous code you did:
start_delta = 0.015;
delta_step = 0.0001;
delta_array(2) = start_delta + 1*delta_step;
That will always give you a different result than starting with the string 0.0151 . That is, how were you "able to get all of them to agree"? What different processing were you doing?

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!