Is this Matlab bug or error? anyone who can help?

1 次查看(过去 30 天)
I'm using Matlab to solve some problems.
and I realized sth went wrong
that is like this
stepsize =0.05;
theta = (0:-stepsize:-180+stepsize)*pi/180;
%I'm looking for -90 degree
find(theta == -90*pi/180)
the answer is
ans =
1×0 empty double row vector
What's wrong with me??
  2 个评论
Stephen23
Stephen23 2018-2-15
编辑:Stephen23 2018-2-15
The reason is because those numbers are stored on your computer as finite binary numbers called floating point numbers, and they cannot represent exactly those values:
  • in just the same way that you cannot write 1/3 exactly using a finite decimal fraction,
  • and pi cannot be written with a finite number of digits in any integer base...
Your code tests for equivalence of floating point numbers, which is always unreliable. You need to change your algorithm to take into account floating point error:
etc, etc, etc

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2018-2-15

更多回答(0 个)

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by