1/* Copyright JS Foundation and other contributors, http://js.foundation 2 * 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 * 15 * This file is based on work under the following copyright and permission 16 * notice: 17 * 18 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 19 * 20 * Developed at SunSoft, a Sun Microsystems, Inc. business. 21 * Permission to use, copy, modify, and distribute this 22 * software is freely granted, provided that this notice 23 * is preserved. 24 * 25 * @(#)s_atan.c 1.3 95/01/18 26 */ 27 28#include "jerry-libm-internal.h" 29 30/* atan(x) 31 * 32 * Method: 33 * 1. Reduce x to positive by atan(x) = -atan(-x). 34 * 2. According to the integer k=4t+0.25 chopped, t=x, the argument 35 * is further reduced to one of the following intervals and the 36 * arctangent of t is evaluated by the corresponding formula: 37 * 38 * [0,7/16] atan(x) = t-t^3*(a1+t^2*(a2+...(a10+t^2*a11)...) 39 * [7/16,11/16] atan(x) = atan(1/2) + atan( (t-0.5)/(1+t/2) ) 40 * [11/16.19/16] atan(x) = atan( 1 ) + atan( (t-1)/(1+t) ) 41 * [19/16,39/16] atan(x) = atan(3/2) + atan( (t-1.5)/(1+1.5t) ) 42 * [39/16,INF] atan(x) = atan(INF) + atan( -1/t ) 43 * 44 * Constants: 45 * The hexadecimal values are the intended ones for the following 46 * constants. The decimal values may be used, provided that the 47 * compiler will convert from decimal to binary accurately enough 48 * to produce the hexadecimal values shown. 49 */ 50 51static const double atanhi[] = 52{ 53 4.63647609000806093515e-01, /* atan(0.5)hi 0x3FDDAC67, 0x0561BB4F */ 54 7.85398163397448278999e-01, /* atan(1.0)hi 0x3FE921FB, 0x54442D18 */ 55 9.82793723247329054082e-01, /* atan(1.5)hi 0x3FEF730B, 0xD281F69B */ 56 1.57079632679489655800e+00, /* atan(inf)hi 0x3FF921FB, 0x54442D18 */ 57}; 58 59static const double atanlo[] = 60{ 61 2.26987774529616870924e-17, /* atan(0.5)lo 0x3C7A2B7F, 0x222F65E2 */ 62 3.06161699786838301793e-17, /* atan(1.0)lo 0x3C81A626, 0x33145C07 */ 63 1.39033110312309984516e-17, /* atan(1.5)lo 0x3C700788, 0x7AF0CBBD */ 64 6.12323399573676603587e-17, /* atan(inf)lo 0x3C91A626, 0x33145C07 */ 65}; 66 67#define aT0 3.33333333333329318027e-01 /* 0x3FD55555, 0x5555550D */ 68#define aT1 -1.99999999998764832476e-01 /* 0xBFC99999, 0x9998EBC4 */ 69#define aT2 1.42857142725034663711e-01 /* 0x3FC24924, 0x920083FF */ 70#define aT3 -1.11111104054623557880e-01 /* 0xBFBC71C6, 0xFE231671 */ 71#define aT4 9.09088713343650656196e-02 /* 0x3FB745CD, 0xC54C206E */ 72#define aT5 -7.69187620504482999495e-02 /* 0xBFB3B0F2, 0xAF749A6D */ 73#define aT6 6.66107313738753120669e-02 /* 0x3FB10D66, 0xA0D03D51 */ 74#define aT7 -5.83357013379057348645e-02 /* 0xBFADDE2D, 0x52DEFD9A */ 75#define aT8 4.97687799461593236017e-02 /* 0x3FA97B4B, 0x24760DEB */ 76#define aT9 -3.65315727442169155270e-02 /* 0xBFA2B444, 0x2C6A6C2F */ 77#define aT10 1.62858201153657823623e-02 /* 0x3F90AD3A, 0xE322DA11 */ 78 79#define one 1.0 80#define huge 1.0e300 81 82double 83atan (double x) 84{ 85 double w, s1, s2, z; 86 int ix, hx, id; 87 88 hx = __HI (x); 89 ix = hx & 0x7fffffff; 90 if (ix >= 0x44100000) /* if |x| >= 2^66 */ 91 { 92 if (ix > 0x7ff00000 || (ix == 0x7ff00000 && (__LO (x) != 0))) 93 { 94 return x + x; /* NaN */ 95 } 96 if (hx > 0) 97 { 98 return atanhi[3] + atanlo[3]; 99 } 100 else 101 { 102 return -atanhi[3] - atanlo[3]; 103 } 104 } 105 if (ix < 0x3fdc0000) /* |x| < 0.4375 */ 106 { 107 if (ix < 0x3e200000) /* |x| < 2^-29 */ 108 { 109 if (huge + x > one) /* raise inexact */ 110 { 111 return x; 112 } 113 } 114 id = -1; 115 } 116 else 117 { 118 x = fabs (x); 119 if (ix < 0x3ff30000) /* |x| < 1.1875 */ 120 { 121 if (ix < 0x3fe60000) /* 7/16 <= |x| < 11/16 */ 122 { 123 id = 0; 124 x = (2.0 * x - one) / (2.0 + x); 125 } 126 else /* 11/16 <= |x| < 19/16 */ 127 { 128 id = 1; 129 x = (x - one) / (x + one); 130 } 131 } 132 else 133 { 134 if (ix < 0x40038000) /* |x| < 2.4375 */ 135 { 136 id = 2; 137 x = (x - 1.5) / (one + 1.5 * x); 138 } 139 else /* 2.4375 <= |x| < 2^66 */ 140 { 141 id = 3; 142 x = -1.0 / x; 143 } 144 } 145 } 146 /* end of argument reduction */ 147 z = x * x; 148 w = z * z; 149 /* break sum from i=0 to 10 aT[i] z**(i+1) into odd and even poly */ 150 s1 = z * (aT0 + w * (aT2 + w * (aT4 + w * (aT6 + w * (aT8 + w * aT10))))); 151 s2 = w * (aT1 + w * (aT3 + w * (aT5 + w * (aT7 + w * aT9)))); 152 if (id < 0) 153 { 154 return x - x * (s1 + s2); 155 } 156 else 157 { 158 z = atanhi[id] - ((x * (s1 + s2) - atanlo[id]) - x); 159 return (hx < 0) ? -z : z; 160 } 161} /* atan */ 162 163#undef aT0 164#undef aT1 165#undef aT2 166#undef aT3 167#undef aT4 168#undef aT5 169#undef aT6 170#undef aT7 171#undef aT8 172#undef aT9 173#undef aT10 174#undef one 175#undef huge 176