Wrong result in elipse equation

조회 수: 1 (최근 30일)
Andrea Stevanato
Andrea Stevanato 2018년 7월 2일
답변: James Tursa 2018년 7월 2일
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