s_tan.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. * ====================================================
  3. * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  4. *
  5. * Developed at SunPro, a Sun Microsystems, Inc. business.
  6. * Permission to use, copy, modify, and distribute this
  7. * software is freely granted, provided that this notice
  8. * is preserved.
  9. * ====================================================
  10. */
  11. /* tan(x)
  12. * Return tangent function of x.
  13. *
  14. * kernel function:
  15. * __kernel_tan ... tangent function on [-pi/4,pi/4]
  16. * __ieee754_rem_pio2 ... argument reduction routine
  17. *
  18. * Method.
  19. * Let S,C and T denote the sin, cos and tan respectively on
  20. * [-PI/4, +PI/4]. Reduce the argument x to y1+y2 = x-k*pi/2
  21. * in [-pi/4 , +pi/4], and let n = k mod 4.
  22. * We have
  23. *
  24. * n sin(x) cos(x) tan(x)
  25. * ----------------------------------------------------------
  26. * 0 S C T
  27. * 1 C -S -1/T
  28. * 2 -S -C T
  29. * 3 -C S -1/T
  30. * ----------------------------------------------------------
  31. *
  32. * Special cases:
  33. * Let trig be any of sin, cos, or tan.
  34. * trig(+-INF) is NaN, with signals;
  35. * trig(NaN) is that NaN;
  36. *
  37. * Accuracy:
  38. * TRIG(x) returns trig(x) nearly rounded
  39. */
  40. #include "math.h"
  41. #include "math_private.h"
  42. double tan(double x)
  43. {
  44. double y[2],z=0.0;
  45. int32_t n, ix;
  46. /* High word of x. */
  47. GET_HIGH_WORD(ix,x);
  48. /* |x| ~< pi/4 */
  49. ix &= 0x7fffffff;
  50. if(ix <= 0x3fe921fb) return __kernel_tan(x,z,1);
  51. /* tan(Inf or NaN) is NaN */
  52. else if (ix>=0x7ff00000) return x-x; /* NaN */
  53. /* argument reduction needed */
  54. else {
  55. n = __ieee754_rem_pio2(x,y);
  56. return __kernel_tan(y[0],y[1],1-((n&1)<<1)); /* 1 -- n even
  57. -1 -- n odd */
  58. }
  59. }
  60. libm_hidden_def(tan)