Home >Backend Development >C++ >Why Do C/C \'s cos() and sin() Functions Produce Unexpected Results for Common Angles Like 180 Degrees?

Why Do C/C \'s cos() and sin() Functions Produce Unexpected Results for Common Angles Like 180 Degrees?

Patricia Arquette
Patricia ArquetteOriginal
2024-11-27 12:00:16732browse

Why Do C/C  's cos() and sin() Functions Produce Unexpected Results for Common Angles Like 180 Degrees?

Unexpected Results from C/C 's cos() and sin() for Familiar Angles

When applying an angle of 180 degrees to C/C 's cos() and sin() functions, an incorrect outcome may arise. This could be surprising since we anticipate the results to be:

  • sin of 0.0547
  • cos of 0.99

However, the actual results often differ:

  • sin of 3.5897934739308216e-009
  • cos of -1.00000

This discrepancy stems from the fact that cos(a), sin(a), and tan(a) functions in C/C expect radians as an argument, not degrees. The conversion from 180 degrees to radians can sometimes introduce an approximation.

Even with the correct radian calculation, the results may still deviate due to the finite precision of double and a potential misrepresentation of PI.

To address this, it's best to reduce the argument in degrees before invoking the trig function. By limiting the angle to between -45° and 45°, we ensure the accuracy of the outcome.

For instance, consider the following code, which calculates sin() in degrees using argument reduction:

#include <math.h>
#include <stdio.h>

static double d2r(double d) {
  return (d / 180.0) * ((double) M_PI);
}

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, &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;
}

int main(void) {
  int i;
  for (i = -360; i <= 360; i += 15) {
    printf("sin()  of %.1f degrees is  % .*e\n", 1.0 * i, DBL_DECIMAL_DIG - 1,
        sin(d2r(i)));
    printf("sind() of %.1f degrees is  % .*e\n", 1.0 * i, DBL_DECIMAL_DIG - 1,
        sind(i));
  }
  return 0;
}

This code generates a more accurate result than the original approach, especially for values at multiple angles, because it performs argument reduction before calling the trig function.

The above is the detailed content of Why Do C/C \'s cos() and sin() Functions Produce Unexpected Results for Common Angles Like 180 Degrees?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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