s_tan.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. libm_hidden_proto(tan)
  47. #ifdef __STDC__
  48. double tan(double x)
  49. #else
  50. double tan(x)
  51. double x;
  52. #endif
  53. {
  54. double y[2],z=0.0;
  55. int32_t n, ix;
  56. /* High word of x. */
  57. GET_HIGH_WORD(ix,x);
  58. /* |x| ~< pi/4 */
  59. ix &= 0x7fffffff;
  60. if(ix <= 0x3fe921fb) return __kernel_tan(x,z,1);
  61. /* tan(Inf or NaN) is NaN */
  62. else if (ix>=0x7ff00000) return x-x; /* NaN */
  63. /* argument reduction needed */
  64. else {
  65. n = __ieee754_rem_pio2(x,y);
  66. return __kernel_tan(y[0],y[1],1-((n&1)<<1)); /* 1 -- n even
  67. -1 -- n odd */
  68. }
  69. }
  70. libm_hidden_def(tan)