- 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...
Is this Matlab bug or error? anyone who can help?
2 views (last 30 days)
Show older comments
Jeong Hyun Seo
on 15 Feb 2018
Commented: Jeong Hyun Seo
on 15 Feb 2018
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
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:
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
Accepted Answer
More Answers (0)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!