How to Increase the accuracy of trigonometric functions?

9 views (last 30 days)
The typical function "sin", "cos" or "tan" seems not accurate enough for me. How to improve it?
And the function "digits" didn't work in this case.
Example codes (The answers should be 0):
>> alpha = 1;
>> cos(alpha) * tan(alpha) - sin(alpha)
ans =
1.1102e-16
>> tan(alpha) - sin(alpha) / cos(alpha)
ans =
2.2204e-16
>> digits(256);
>> cos(alpha) * tan(alpha) - sin(alpha)
ans =
1.1102e-16
>> tan(alpha) - sin(alpha) / cos(alpha)
ans =
2.2204e-16

Accepted Answer

Walter Roberson
Walter Roberson on 22 Nov 2019
alpha = sym(1);
>> simplify(cos(alpha) * tan(alpha) - sin(alpha))
ans =
0
>> simplify(tan(alpha) - sin(alpha) / cos(alpha))
ans =
0
There is no realistic prospect of getting an exact 0 for these calculations when using finite binary floating point calculations. The problem is not the accuracy of the trig functions: the problem occurs for all finite representations http://matlab.wikia.com/wiki/FAQ#Why_is_0.3_-_0.2_-_0.1_.28or_similar.29_not_equal_to_zero.3F
  2 Comments
Steven Lord
Steven Lord on 22 Nov 2019
Adding to what Walter said, the digits function has no effect on computations using just numeric variables. It affects symbolic calculations only.
Depending on what you're trying to do the sinpi family of functions (introduced in release R2018b) or the sind family of functions may be of use to you. See the list of functions on this documentation page to know what's available.

Sign in to comment.

More Answers (0)

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!