Wrong result in elipse equation
1 次查看(过去 30 天)
显示 更早的评论
I have defined an anonymous function elipse
>> xCenter = 0.58;
>> yCenter = 590;
>> a = 0.02;
>> b = 200;
>> elipse = @(x,y) ((x-xCenter)^2)/a^2 + ((y -yCenter)^2)/b^2 - 1;
But the result it's wrong in some case like this:
>> x1 = 0.6;
>> y1 = 590;
>> elipse(x1, y1)
ans =
1.77635683940025e-15
This result should be 0, I have to set something else to fix it? One correct example is:
>> x1 = 0.58;
>> y1 = 390;
>> elipse(x1, y1)
ans =
0
1 个评论
Adam
2018-7-2
1.77e-15 pretty much is 0.
Floating point maths on a computer is not 100% accurate. Numbers cannot be represented to complete accuracy (e.g. some numbers would have an infinite number of decimal places, but even much simpler numbers cannot be represented with 1005 accuracy either just because of how they are represented in a floating-point data type).
Any time you do maths involving floats you should expect an accumulation of insignificant errors. Relative to 590 or 0.6 1e-15 seems pretty insiginificant to me.
采纳的回答
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Function Creation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!