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 13176385Sbde#include <sys/cdefs.h> 14176385Sbde__FBSDID("$FreeBSD: releng/10.3/lib/msun/src/s_tan.c 218509 2011-02-10 07:37:50Z das $"); 152116Sjkh 162116Sjkh/* tan(x) 172116Sjkh * Return tangent function of x. 182116Sjkh * 192116Sjkh * kernel function: 202116Sjkh * __kernel_tan ... tangent function on [-pi/4,pi/4] 212116Sjkh * __ieee754_rem_pio2 ... argument reduction routine 222116Sjkh * 232116Sjkh * Method. 248870Srgrimes * Let S,C and T denote the sin, cos and tan respectively on 258870Srgrimes * [-PI/4, +PI/4]. Reduce the argument x to y1+y2 = x-k*pi/2 262116Sjkh * in [-pi/4 , +pi/4], and let n = k mod 4. 272116Sjkh * We have 282116Sjkh * 292116Sjkh * n sin(x) cos(x) tan(x) 302116Sjkh * ---------------------------------------------------------- 312116Sjkh * 0 S C T 322116Sjkh * 1 C -S -1/T 332116Sjkh * 2 -S -C T 342116Sjkh * 3 -C S -1/T 352116Sjkh * ---------------------------------------------------------- 362116Sjkh * 372116Sjkh * Special cases: 382116Sjkh * Let trig be any of sin, cos, or tan. 392116Sjkh * trig(+-INF) is NaN, with signals; 402116Sjkh * trig(NaN) is that NaN; 412116Sjkh * 422116Sjkh * Accuracy: 438870Srgrimes * TRIG(x) returns trig(x) nearly rounded 442116Sjkh */ 452116Sjkh 46176360Sdas#include <float.h> 47176360Sdas 482116Sjkh#include "math.h" 49176385Sbde#define INLINE_REM_PIO2 502116Sjkh#include "math_private.h" 51176385Sbde#include "e_rem_pio2.c" 522116Sjkh 5397413Salfreddouble 54117912Spetertan(double x) 552116Sjkh{ 562116Sjkh double y[2],z=0.0; 572116Sjkh int32_t n, ix; 582116Sjkh 592116Sjkh /* High word of x. */ 602116Sjkh GET_HIGH_WORD(ix,x); 612116Sjkh 622116Sjkh /* |x| ~< pi/4 */ 632116Sjkh ix &= 0x7fffffff; 64151969Sbde if(ix <= 0x3fe921fb) { 65218509Sdas if(ix<0x3e400000) /* x < 2**-27 */ 66151969Sbde if((int)x==0) return x; /* generate inexact */ 67151969Sbde return __kernel_tan(x,z,1); 68151969Sbde } 692116Sjkh 702116Sjkh /* tan(Inf or NaN) is NaN */ 712116Sjkh else if (ix>=0x7ff00000) return x-x; /* NaN */ 722116Sjkh 732116Sjkh /* argument reduction needed */ 742116Sjkh else { 752116Sjkh n = __ieee754_rem_pio2(x,y); 762116Sjkh return __kernel_tan(y[0],y[1],1-((n&1)<<1)); /* 1 -- n even 772116Sjkh -1 -- n odd */ 782116Sjkh } 792116Sjkh} 80176360Sdas 81176360Sdas#if (LDBL_MANT_DIG == 53) 82176360Sdas__weak_reference(tan, tanl); 83176360Sdas#endif 84