Wrong result in elipse equation

1 view (last 30 days)
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 Comment
Adam
Adam on 2 Jul 2018
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.

Sign in to comment.

Accepted Answer

James Tursa
James Tursa on 2 Jul 2018

More Answers (0)

Community Treasure Hunt

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

Start Hunting!