s_tan.c 1.9 KB

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