DEV Community

Caleb Zhao
Caleb Zhao

Posted on

Incorrect calculations: sind(x) and cosd(x)

In this blog post, we explore incorrect calculations of the sine function sind(x) and cosine function cosd(x) of which the independent variable is in degrees in MATLAB.

Example 1. Given that x1 = 12345678.9 and x2=12345678.9e20, calculate sind(x1), cosd(x1), sind(x2), and cosd(x2).

Using MATLAB, I computed the values of sind(x) and cosd(x) at x1 and x2. The results are shown in the following screenshot.

MATLAB outputFrom the above screenshot, it can be seen that the outputs from MATLAB are 0.323917418204301,0.946085358825439,0.927183854566787, -0.323917418\red{204301}, -0.94608535882\red{5439}, 0.\red{927183854566787}, and 0.374606593415912 -0.\red{374606593415912} respectively.

However, the correct values for the first two, rounded to 16 significant digits, are -0.3239174181981494 and -0.9460853588275453, respectively; the accurate values for the last two are 0 and 1, respectively (as provided by ISRealsoft).

Therefore, the error rates for the significant digits of the first two are 7/16 = 43.75% and 5/16 = 31.25%, respectively; while the last two have completely incorrect digits, resulting in an error rate of 100% for each.

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

Image of Datadog

Create and maintain end-to-end frontend tests

Learn best practices on creating frontend tests, testing on-premise apps, integrating tests into your CI/CD pipeline, and using Datadog’s testing tunnel.

Download The Guide

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more