DEV Community

Caleb Zhao
Caleb Zhao

Posted on • Edited on

Incorrect calculations: 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.

previous \,\quad\quad\quad\quad\hspace{7.2cm} next

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read more →

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs