My code does does not go through the the if section
2 次查看(过去 30 天)
显示 更早的评论
Hi all,
I am pretty sure that in column 1 of Coefficient matrix (Coefficient(1:26,1)) there is value equal to Alpha(my input parameter). I wondering why my cod does not go through the if section. Can anyone tell me what is going wrong here?
clear
close all
clc
load("Coefficient.mat")
Alpha=0.8;
Mu=9;
Alpha_vector=Coefficient(:,2)+Coefficient(:,3)*Mu+Coefficient(:,4)*(Mu^2)+Coefficient(:,5)*(Mu^3)+Coefficient(:,6)*(Mu^4)...
+Coefficient(:,7)*(Mu^5)+Coefficient(:,8)*(Mu^6);
[wasfound, idx] = ismember(Alpha, Coefficient(1:26,1));
if wasfound
landa = Alpha_vector(idx);
else
do other stuff
end
0 个评论
采纳的回答
John D'Errico
2023-1-18
Rule # 1: NEVER test for exact equality of floating point numbers. (At least unless you fully, completely absolutely understand floating point representations.)
Rule # 2: There ain't no rule 2.
Welcome to the wonderful wacky world of floating point arithmetic, where what looks like a number that you know and recognize is not always the case.
Effectively in your code, you are looking for an EXACT match to the number 0.8. You do recognize of course that 0.8 is not reepresentable exactly as a double precision number? This is because MATLAB (as well as almost all other languages) use an IEE floating point representation for doubles, that encodes the numbers in a BINARY form.
For example, what is the EXACT value of 2/3, when represented as a decimal, in a finite number of decimal digits? I am sure you would accept that 0.6666 is not the value, nor is 0.6667. And as many digits as you wish to take, you will ner get an exact match. This is because 2/3 lacks a finite, terminating expansion in a decimal form.
So do you assume that 0.8 has such an expansion in binary? (NO, it does not.) Instead, MATLAB uses the closest approximation it can find using 52 binary bits of precision. It will be close, but not exactly the case. So in fact, 0.8, in binary form is an infinitely repeating binary number of this form:
0.11001100110011001100110011001100110011001100110011001100...
MATLAB actually stores it as effectively this value:
0.11001100110011001100110011001100110011001100110011010
Do you see that MATLAB has been forced to approximate the infintely repeating binary form as a finite form?
What is worse of course is then every computation you do with these approximate values as stored as doubles then can accumulate into larger errors in the least significant bits. (Luckily the law of large numbers tends to work in your favor to cancel any errors out.)
So what can you do? USE A TOLERANCE. ALWAYS. That is, whenever you would be testing for exact equality of two floating point numbers, DON'T. See rule 1.
4 个评论
Torsten
2023-1-19
Be careful.
Change the remaining "landa_min" to "lambda_min" and "Lambda" to "lambda" (MATLAB is case sensitive !).
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Identification 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!