Path: blob/master/src/java.base/share/native/libfdlibm/s_atan.c
41149 views
/*1* Copyright (c) 1998, 2001, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/* atan(x)26* Method27* 1. Reduce x to positive by atan(x) = -atan(-x).28* 2. According to the integer k=4t+0.25 chopped, t=x, the argument29* is further reduced to one of the following intervals and the30* arctangent of t is evaluated by the corresponding formula:31*32* [0,7/16] atan(x) = t-t^3*(a1+t^2*(a2+...(a10+t^2*a11)...)33* [7/16,11/16] atan(x) = atan(1/2) + atan( (t-0.5)/(1+t/2) )34* [11/16.19/16] atan(x) = atan( 1 ) + atan( (t-1)/(1+t) )35* [19/16,39/16] atan(x) = atan(3/2) + atan( (t-1.5)/(1+1.5t) )36* [39/16,INF] atan(x) = atan(INF) + atan( -1/t )37*38* Constants:39* The hexadecimal values are the intended ones for the following40* constants. The decimal values may be used, provided that the41* compiler will convert from decimal to binary accurately enough42* to produce the hexadecimal values shown.43*/4445#include "fdlibm.h"4647#ifdef __STDC__48static const double atanhi[] = {49#else50static double atanhi[] = {51#endif524.63647609000806093515e-01, /* atan(0.5)hi 0x3FDDAC67, 0x0561BB4F */537.85398163397448278999e-01, /* atan(1.0)hi 0x3FE921FB, 0x54442D18 */549.82793723247329054082e-01, /* atan(1.5)hi 0x3FEF730B, 0xD281F69B */551.57079632679489655800e+00, /* atan(inf)hi 0x3FF921FB, 0x54442D18 */56};5758#ifdef __STDC__59static const double atanlo[] = {60#else61static double atanlo[] = {62#endif632.26987774529616870924e-17, /* atan(0.5)lo 0x3C7A2B7F, 0x222F65E2 */643.06161699786838301793e-17, /* atan(1.0)lo 0x3C81A626, 0x33145C07 */651.39033110312309984516e-17, /* atan(1.5)lo 0x3C700788, 0x7AF0CBBD */666.12323399573676603587e-17, /* atan(inf)lo 0x3C91A626, 0x33145C07 */67};6869#ifdef __STDC__70static const double aT[] = {71#else72static double aT[] = {73#endif743.33333333333329318027e-01, /* 0x3FD55555, 0x5555550D */75-1.99999999998764832476e-01, /* 0xBFC99999, 0x9998EBC4 */761.42857142725034663711e-01, /* 0x3FC24924, 0x920083FF */77-1.11111104054623557880e-01, /* 0xBFBC71C6, 0xFE231671 */789.09088713343650656196e-02, /* 0x3FB745CD, 0xC54C206E */79-7.69187620504482999495e-02, /* 0xBFB3B0F2, 0xAF749A6D */806.66107313738753120669e-02, /* 0x3FB10D66, 0xA0D03D51 */81-5.83357013379057348645e-02, /* 0xBFADDE2D, 0x52DEFD9A */824.97687799461593236017e-02, /* 0x3FA97B4B, 0x24760DEB */83-3.65315727442169155270e-02, /* 0xBFA2B444, 0x2C6A6C2F */841.62858201153657823623e-02, /* 0x3F90AD3A, 0xE322DA11 */85};8687#ifdef __STDC__88static const double89#else90static double91#endif92one = 1.0,93huge = 1.0e300;9495#ifdef __STDC__96double atan(double x)97#else98double atan(x)99double x;100#endif101{102double w,s1,s2,z;103int ix,hx,id;104105hx = __HI(x);106ix = hx&0x7fffffff;107if(ix>=0x44100000) { /* if |x| >= 2^66 */108if(ix>0x7ff00000||109(ix==0x7ff00000&&(__LO(x)!=0)))110return x+x; /* NaN */111if(hx>0) return atanhi[3]+atanlo[3];112else return -atanhi[3]-atanlo[3];113} if (ix < 0x3fdc0000) { /* |x| < 0.4375 */114if (ix < 0x3e200000) { /* |x| < 2^-29 */115if(huge+x>one) return x; /* raise inexact */116}117id = -1;118} else {119x = fabs(x);120if (ix < 0x3ff30000) { /* |x| < 1.1875 */121if (ix < 0x3fe60000) { /* 7/16 <=|x|<11/16 */122id = 0; x = (2.0*x-one)/(2.0+x);123} else { /* 11/16<=|x|< 19/16 */124id = 1; x = (x-one)/(x+one);125}126} else {127if (ix < 0x40038000) { /* |x| < 2.4375 */128id = 2; x = (x-1.5)/(one+1.5*x);129} else { /* 2.4375 <= |x| < 2^66 */130id = 3; x = -1.0/x;131}132}}133/* end of argument reduction */134z = x*x;135w = z*z;136/* break sum from i=0 to 10 aT[i]z**(i+1) into odd and even poly */137s1 = z*(aT[0]+w*(aT[2]+w*(aT[4]+w*(aT[6]+w*(aT[8]+w*aT[10])))));138s2 = w*(aT[1]+w*(aT[3]+w*(aT[5]+w*(aT[7]+w*aT[9]))));139if (id<0) return x - x*(s1+s2);140else {141z = atanhi[id] - ((x*(s1+s2) - atanlo[id]) - x);142return (hx<0)? -z:z;143}144}145146147