What is tan (90) in c?

the value it gives is 557135813.94455. will the value stay the same every time? why doesn't it show infinity ??

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

#define PI 3.14159265

int main ()
{
  double param, result;
  param = 90.0;
  result = tan ( param * PI / 180.0 );
  printf ("The tangent of %f degrees is %f.\n", param, result );
  return 0;
}

      

+3


source to share


3 answers


You are not passing a Pi / 2 value, you are passing 90.0 * 3.14159265 / 180.0, an approximation.



+6


source


Floating point arithmetic is not exact arithmetic. You cannot compare two floating point numbers using ==

; for example 0.6 / 0.2 - 3 == 0

should be true, but on most systems it will be false. Be careful when doing floating point calculations and expect accurate results; it is doomed to fail. Consider each floating point calculation just to return an approximation; although very good, sometimes even accurate, just don't rely on it to be accurate.



+1


source


The code does not require a 90 ยฐ tangent, but a radian tangent close to 90 ยฐ. The conversion to radians is not exact as ฯ€ / 2 radians cannot be represented exactly as double

.

The solution is to decrease the range of degrees first and then call tan(d2r(x))

.

#include <math.h>

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

double tand(double x /* degrees */) {
  if (!isfinite(x)) {
    return tan(x);
  } else if (x < 0.0) {
    return -tand(-x);
  }
  int quo;
  double x45 = remquo(fabs(x), 90.0, &quo);
  //printf("%d %f ", quo & 3, x45);
  switch (quo % 4) {
    case 0:
      return tan(d2r(x45));
    case 1:
      return 1.0 / tan(d2r(- x45));
    case 2:
      return -tan(d2r(-x45));
    case 3:
      return -1.0 / tan(d2r(x45));
  }
  return 0.0;
}

#define PI 3.14159265

int main(void) {
  double param, result;
  param = 90.0;
  result = tan(param * PI / 180.0);
  printf("Angle  %.*e radian\n", DBL_DECIMAL_DIG - 1, param * PI / 180.0);
  printf("Pi/2 = 1.5707963267948966192313216916398...\n");
  printf("The tangent of %f degrees is %f.\n", param, result);
  int i;
  for (i = -360; i <= 360; i += 30) {
    printf("The tangent method 1 of %.1f degrees is  %.*e\n", 
        1.0*i, DBL_DECIMAL_DIG - 1, tan(d2r(-i)));
    printf("The tangent method 2 of %.1f degrees is  %.*e\n", 
        1.0*i, DBL_DECIMAL_DIG - 1, tand(-i));
  }
  return 0;
}

      

OP output

Angle  1.5707963250000001e+00 radian
Pi/2 = 1.5707963267948966192313216916398...
The tangent of 90.000000 degrees is 557135183.943528.

      

top scores

The tangent method 1 of -360.0 degrees is  -2.4492935982947064e-16
The tangent method 2 of -360.0 degrees is  0.0000000000000000e+00
The tangent method 1 of -330.0 degrees is  -5.7735026918962640e-01
The tangent method 2 of -330.0 degrees is  -5.7735026918962573e-01
The tangent method 1 of -300.0 degrees is  -1.7320508075688770e+00
The tangent method 2 of -300.0 degrees is  -1.7320508075688774e+00
The tangent method 1 of -270.0 degrees is  5.4437464510651230e+15
The tangent method 2 of -270.0 degrees is  -inf
The tangent method 1 of -240.0 degrees is  1.7320508075688752e+00
The tangent method 2 of -240.0 degrees is  1.7320508075688774e+00
The tangent method 1 of -210.0 degrees is  5.7735026918962540e-01
The tangent method 2 of -210.0 degrees is  5.7735026918962573e-01
The tangent method 1 of -180.0 degrees is  -1.2246467991473532e-16
The tangent method 2 of -180.0 degrees is  0.0000000000000000e+00
...

      

+1


source







All Articles