s_tan.c revision 176360
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 176360 2008-02-17 07:33:12Z das $"; 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 47176360Sdas#include <float.h> 48176360Sdas 492116Sjkh#include "math.h" 502116Sjkh#include "math_private.h" 512116Sjkh 5297413Salfreddouble 53117912Spetertan(double x) 542116Sjkh{ 552116Sjkh double y[2],z=0.0; 562116Sjkh int32_t n, ix; 572116Sjkh 582116Sjkh /* High word of x. */ 592116Sjkh GET_HIGH_WORD(ix,x); 602116Sjkh 612116Sjkh /* |x| ~< pi/4 */ 622116Sjkh ix &= 0x7fffffff; 63151969Sbde if(ix <= 0x3fe921fb) { 64151969Sbde if(ix<0x3e300000) /* x < 2**-28 */ 65151969Sbde if((int)x==0) return x; /* generate inexact */ 66151969Sbde return __kernel_tan(x,z,1); 67151969Sbde } 682116Sjkh 692116Sjkh /* tan(Inf or NaN) is NaN */ 702116Sjkh else if (ix>=0x7ff00000) return x-x; /* NaN */ 712116Sjkh 722116Sjkh /* argument reduction needed */ 732116Sjkh else { 742116Sjkh n = __ieee754_rem_pio2(x,y); 752116Sjkh return __kernel_tan(y[0],y[1],1-((n&1)<<1)); /* 1 -- n even 762116Sjkh -1 -- n odd */ 772116Sjkh } 782116Sjkh} 79176360Sdas 80176360Sdas#if (LDBL_MANT_DIG == 53) 81176360Sdas__weak_reference(tan, tanl); 82176360Sdas#endif 83