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

2 views (last 30 days)
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 Comments
Stephen23
Stephen23 on 15 Feb 2018
Edited: Stephen23 on 15 Feb 2018
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

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 15 Feb 2018

More Answers (0)

Community Treasure Hunt

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

Start Hunting!