How to Increase the accuracy of trigonometric functions?
9 views (last 30 days)
Show older comments
Liheng Shi
on 22 Nov 2019
Commented: Walter Roberson
on 22 Nov 2019
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
0 Comments
Accepted Answer
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
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.
Walter Roberson
on 22 Nov 2019
For example in ieee 754 binary double precision
1/49*49-1
is not 0.
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!