DEV Community

Caleb Zhao
Caleb Zhao

Posted on

Incorrect calculations by the computer: tan(x) and cot(x) near x=k*π

Exploring the computational errors of the tangent function tan(x) and cotangent function cot(x) in MATLAB near numbers around kπ(k0). k\,\pi (k\neq0).

Example 1. Suppose x=65.97344621π. x=65.973446\approx21\,\pi. Calculate tan(x) \tan(x) and cot(x) \cot(x) in MATLAB.

Let's just post the figure directly.

MATLAB outputFrom the above figure, it can be seen that the two outputs from MATLAB are
2.746143375848801e07 2.746143\red{375848801}e-07 and 3.641470466526212e+06. 3.6414704\red{66526212}e+06.
However, the correct values with 16 significant digits are 0.2746143419922914e-6 and 0.3641470408082585e7, respectively (as provided by ISRealsoft).
Thus, the outputs of MATLAB contain 9 and 8 incorrect digits, with error rates of 9/16 = 56.25% and 8/16 = 50% respectively for the significant figures.

Top comments (0)