s_tan.c revision 2117
1171568Sscottl/* @(#)s_tan.c 5.1 93/09/24 */ 2211095Sdes/* 3171568Sscottl * ==================================================== 4171568Sscottl * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 5171568Sscottl * 6171568Sscottl * Developed at SunPro, a Sun Microsystems, Inc. business. 7171568Sscottl * Permission to use, copy, modify, and distribute this 8171568Sscottl * software is freely granted, provided that this notice 9171568Sscottl * is preserved. 10171568Sscottl * ==================================================== 11171568Sscottl */ 12171568Sscottl 13171568Sscottl#ifndef lint 14171568Sscottlstatic char rcsid[] = "$Id: s_tan.c,v 1.5 1994/08/18 23:10:19 jtc Exp $"; 15171568Sscottl#endif 16171568Sscottl 17171568Sscottl/* tan(x) 18171568Sscottl * Return tangent function of x. 19171568Sscottl * 20171568Sscottl * kernel function: 21171568Sscottl * __kernel_tan ... tangent function on [-pi/4,pi/4] 22171568Sscottl * __ieee754_rem_pio2 ... argument reduction routine 23171568Sscottl * 24171568Sscottl * Method. 25171568Sscottl * Let S,C and T denote the sin, cos and tan respectively on 26171568Sscottl * [-PI/4, +PI/4]. Reduce the argument x to y1+y2 = x-k*pi/2 27171568Sscottl * in [-pi/4 , +pi/4], and let n = k mod 4. 28171568Sscottl * We have 29171568Sscottl * 30171568Sscottl * n sin(x) cos(x) tan(x) 31171568Sscottl * ---------------------------------------------------------- 32171568Sscottl * 0 S C T 33171568Sscottl * 1 C -S -1/T 34171568Sscottl * 2 -S -C T 35171568Sscottl * 3 -C S -1/T 36171568Sscottl * ---------------------------------------------------------- 37171568Sscottl * 38171568Sscottl * Special cases: 39171568Sscottl * Let trig be any of sin, cos, or tan. 40171568Sscottl * trig(+-INF) is NaN, with signals; 41171568Sscottl * trig(NaN) is that NaN; 42171568Sscottl * 43171568Sscottl * Accuracy: 44171568Sscottl * TRIG(x) returns trig(x) nearly rounded 45171568Sscottl */ 46254657Strasz 47171568Sscottl#include "math.h" 48171568Sscottl#include "math_private.h" 49203460Sdelphij 50203460Sdelphij#ifdef __STDC__ 51171568Sscottl double tan(double x) 52171568Sscottl#else 53171568Sscottl double tan(x) 54171568Sscottl double x; 55171568Sscottl#endif 56171568Sscottl{ 57171568Sscottl double y[2],z=0.0; 58171568Sscottl int32_t n, ix; 59171568Sscottl 60171568Sscottl /* High word of x. */ 61171568Sscottl GET_HIGH_WORD(ix,x); 62171568Sscottl 63171568Sscottl /* |x| ~< pi/4 */ 64171568Sscottl ix &= 0x7fffffff; 65171568Sscottl if(ix <= 0x3fe921fb) return __kernel_tan(x,z,1); 66171568Sscottl 67171568Sscottl /* tan(Inf or NaN) is NaN */ 68171568Sscottl else if (ix>=0x7ff00000) return x-x; /* NaN */ 69171568Sscottl 70171568Sscottl /* argument reduction needed */ 71171568Sscottl else { 72171568Sscottl n = __ieee754_rem_pio2(x,y); 73171568Sscottl return __kernel_tan(y[0],y[1],1-((n&1)<<1)); /* 1 -- n even 74171568Sscottl -1 -- n odd */ 75171568Sscottl } 76171568Sscottl} 77171568Sscottl