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
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.

请先登录,再进行评论。

采纳的回答

James Tursa
James Tursa 2018-7-2

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Function Creation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by