Home > Backend Development > C++ > body text

Why are C/C \'s `sin()` and `cos()` functions producing unexpected results for known angles like 180 degrees?

Linda Hamilton
Release: 2024-11-24 07:00:13
Original
680 people have browsed it

Why are C/C  's `sin()` and `cos()` functions producing unexpected results for known angles like 180 degrees?

Sin and Cos Functions Yielding Unexpected Results for Known Angles

Issue:

When utilizing C/C 's cos() and sin() functions with an angle of 180 degrees, the obtained results deviate significantly from the anticipated values, such as sin(0.0547) and cos(0.99). Instead, these functions return sin(3.5897934739308216e-009) and cos(-1.00000).

Code in Question:

double radians = DegreesToRadians(angle);
double cosValue = cos(radians);
double sinValue = sin(radians);
Copy after login

Where DegreesToRadians() converts degrees to radians using the formula:

double DegreesToRadians(double degrees)
{
    return degrees * PI / 180;
}
Copy after login

Explanation:

C/C 's trigonometric functions (sin(), cos(), tan(), etc.) require their input in radians rather than degrees. The DegreesToRadians() function provided in the code offers an approximate conversion of degrees to radians, utilizing PI as a constant. However, approximations introduce rounding errors, and the use of M_PI may not align precisely with the mathematical value of π.

Moreover, passing the converted radians directly to the sin() and cos() functions can further amplify these errors, leading to incorrect results for certain angles.

Solution:

To address this issue, angle reduction in degrees can be performed before calling the trigonometric functions. This technique reduces the angle to a range between -45° and 45°, addressing rounding errors. For example, consider the sind() function below:

double sind(double x) {
  if (!isfinite(x)) {
    return sin(x);
  }
  if (x < 0.0) {
    return -sind(-x);
  }
  int quo;
  double x90 = remquo(fabs(x), 90.0, &amp;quo);
  switch (quo % 4) {
    case 0:
      // Use * 1.0 to avoid -0.0
      return sin(d2r(x90)* 1.0);
    case 1:
      return cos(d2r(x90));
    case 2:
      return sin(d2r(-x90) * 1.0);
    case 3:
      return -cos(d2r(x90));
  }
  return 0.0;
}
Copy after login

This function encapsulates the angle reduction logic and ensures more accurate results for various angles, including 180 degrees.

The above is the detailed content of Why are C/C \'s `sin()` and `cos()` functions producing unexpected results for known angles like 180 degrees?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template