s_tan.c revision 151969
12116Sjkh/* @(#)s_tan.c 5.1 93/09/24 */ 22116Sjkh/* 32116Sjkh * ==================================================== 42116Sjkh * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 52116Sjkh * 62116Sjkh * Developed at SunPro, a Sun Microsystems, Inc. business. 72116Sjkh * Permission to use, copy, modify, and distribute this 88870Srgrimes * software is freely granted, provided that this notice 92116Sjkh * is preserved. 102116Sjkh * ==================================================== 112116Sjkh */ 122116Sjkh 132116Sjkh#ifndef lint 1450476Speterstatic char rcsid[] = "$FreeBSD: head/lib/msun/src/s_tan.c 151969 2005-11-02 14:01:45Z bde $"; 152116Sjkh#endif 162116Sjkh 172116Sjkh/* tan(x) 182116Sjkh * Return tangent function of x. 192116Sjkh * 202116Sjkh * kernel function: 212116Sjkh * __kernel_tan ... tangent function on [-pi/4,pi/4] 222116Sjkh * __ieee754_rem_pio2 ... argument reduction routine 232116Sjkh * 242116Sjkh * Method. 258870Srgrimes * Let S,C and T denote the sin, cos and tan respectively on 268870Srgrimes * [-PI/4, +PI/4]. Reduce the argument x to y1+y2 = x-k*pi/2 272116Sjkh * in [-pi/4 , +pi/4], and let n = k mod 4. 282116Sjkh * We have 292116Sjkh * 302116Sjkh * n sin(x) cos(x) tan(x) 312116Sjkh * ---------------------------------------------------------- 322116Sjkh * 0 S C T 332116Sjkh * 1 C -S -1/T 342116Sjkh * 2 -S -C T 352116Sjkh * 3 -C S -1/T 362116Sjkh * ---------------------------------------------------------- 372116Sjkh * 382116Sjkh * Special cases: 392116Sjkh * Let trig be any of sin, cos, or tan. 402116Sjkh * trig(+-INF) is NaN, with signals; 412116Sjkh * trig(NaN) is that NaN; 422116Sjkh * 432116Sjkh * Accuracy: 448870Srgrimes * TRIG(x) returns trig(x) nearly rounded 452116Sjkh */ 462116Sjkh 472116Sjkh#include "math.h" 482116Sjkh#include "math_private.h" 492116Sjkh 5097413Salfreddouble 51117912Spetertan(double x) 522116Sjkh{ 532116Sjkh double y[2],z=0.0; 542116Sjkh int32_t n, ix; 552116Sjkh 562116Sjkh /* High word of x. */ 572116Sjkh GET_HIGH_WORD(ix,x); 582116Sjkh 592116Sjkh /* |x| ~< pi/4 */ 602116Sjkh ix &= 0x7fffffff; 61151969Sbde if(ix <= 0x3fe921fb) { 62151969Sbde if(ix<0x3e300000) /* x < 2**-28 */ 63151969Sbde if((int)x==0) return x; /* generate inexact */ 64151969Sbde return __kernel_tan(x,z,1); 65151969Sbde } 662116Sjkh 672116Sjkh /* tan(Inf or NaN) is NaN */ 682116Sjkh else if (ix>=0x7ff00000) return x-x; /* NaN */ 692116Sjkh 702116Sjkh /* argument reduction needed */ 712116Sjkh else { 722116Sjkh n = __ieee754_rem_pio2(x,y); 732116Sjkh return __kernel_tan(y[0],y[1],1-((n&1)<<1)); /* 1 -- n even 742116Sjkh -1 -- n odd */ 752116Sjkh } 762116Sjkh} 77