Back to index

lightning-sunbird  0.9+nobinonly
e_asin.c
Go to the documentation of this file.
00001 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
00002  *
00003  * ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is Mozilla Communicator client code, released
00017  * March 31, 1998.
00018  *
00019  * The Initial Developer of the Original Code is
00020  * Sun Microsystems, Inc.
00021  * Portions created by the Initial Developer are Copyright (C) 1998
00022  * the Initial Developer. All Rights Reserved.
00023  *
00024  * Contributor(s):
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either of the GNU General Public License Version 2 or later (the "GPL"),
00028  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 /* @(#)e_asin.c 1.3 95/01/18 */
00041 /*
00042  * ====================================================
00043  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
00044  *
00045  * Developed at SunSoft, a Sun Microsystems, Inc. business.
00046  * Permission to use, copy, modify, and distribute this
00047  * software is freely granted, provided that this notice 
00048  * is preserved.
00049  * ====================================================
00050  */
00051 
00052 /* __ieee754_asin(x)
00053  * Method :                  
00054  *     Since  asin(x) = x + x^3/6 + x^5*3/40 + x^7*15/336 + ...
00055  *     we approximate asin(x) on [0,0.5] by
00056  *            asin(x) = x + x*x^2*R(x^2)
00057  *     where
00058  *            R(x^2) is a rational approximation of (asin(x)-x)/x^3 
00059  *     and its remez error is bounded by
00060  *            |(asin(x)-x)/x^3 - R(x^2)| < 2^(-58.75)
00061  *
00062  *     For x in [0.5,1]
00063  *            asin(x) = pi/2-2*asin(sqrt((1-x)/2))
00064  *     Let y = (1-x), z = y/2, s := sqrt(z), and pio2_hi+pio2_lo=pi/2;
00065  *     then for x>0.98
00066  *            asin(x) = pi/2 - 2*(s+s*z*R(z))
00067  *                   = pio2_hi - (2*(s+s*z*R(z)) - pio2_lo)
00068  *     For x<=0.98, let pio4_hi = pio2_hi/2, then
00069  *            f = hi part of s;
00070  *            c = sqrt(z) - f = (z-f*f)/(s+f)    ...f+c=sqrt(z)
00071  *     and
00072  *            asin(x) = pi/2 - 2*(s+s*z*R(z))
00073  *                   = pio4_hi+(pio4-2s)-(2s*z*R(z)-pio2_lo)
00074  *                   = pio4_hi+(pio4-2f)-(2s*z*R(z)-(pio2_lo+2c))
00075  *
00076  * Special cases:
00077  *     if x is NaN, return x itself;
00078  *     if |x|>1, return NaN with invalid signal.
00079  *
00080  */
00081 
00082 
00083 #include "fdlibm.h"
00084 
00085 #ifdef __STDC__
00086 static const double 
00087 #else
00088 static double 
00089 #endif
00090 one =  1.00000000000000000000e+00, /* 0x3FF00000, 0x00000000 */
00091 really_big =  1.000e+300,
00092 pio2_hi =  1.57079632679489655800e+00, /* 0x3FF921FB, 0x54442D18 */
00093 pio2_lo =  6.12323399573676603587e-17, /* 0x3C91A626, 0x33145C07 */
00094 pio4_hi =  7.85398163397448278999e-01, /* 0x3FE921FB, 0x54442D18 */
00095        /* coefficient for R(x^2) */
00096 pS0 =  1.66666666666666657415e-01, /* 0x3FC55555, 0x55555555 */
00097 pS1 = -3.25565818622400915405e-01, /* 0xBFD4D612, 0x03EB6F7D */
00098 pS2 =  2.01212532134862925881e-01, /* 0x3FC9C155, 0x0E884455 */
00099 pS3 = -4.00555345006794114027e-02, /* 0xBFA48228, 0xB5688F3B */
00100 pS4 =  7.91534994289814532176e-04, /* 0x3F49EFE0, 0x7501B288 */
00101 pS5 =  3.47933107596021167570e-05, /* 0x3F023DE1, 0x0DFDF709 */
00102 qS1 = -2.40339491173441421878e+00, /* 0xC0033A27, 0x1C8A2D4B */
00103 qS2 =  2.02094576023350569471e+00, /* 0x40002AE5, 0x9C598AC8 */
00104 qS3 = -6.88283971605453293030e-01, /* 0xBFE6066C, 0x1B8D0159 */
00105 qS4 =  7.70381505559019352791e-02; /* 0x3FB3B8C5, 0xB12E9282 */
00106 
00107 #ifdef __STDC__
00108        double __ieee754_asin(double x)
00109 #else
00110        double __ieee754_asin(x)
00111        double x;
00112 #endif
00113 {
00114         fd_twoints u;
00115        double w,t,p,q,c,r,s;
00116        int hx,ix;
00117         u.d = x;
00118        hx = __HI(u);
00119         x = u.d;
00120        ix = hx&0x7fffffff;
00121        if(ix>= 0x3ff00000) {              /* |x|>= 1 */
00122            if(((ix-0x3ff00000)|__LO(u))==0)
00123                   /* asin(1)=+-pi/2 with inexact */
00124               return x*pio2_hi+x*pio2_lo; 
00125            return (x-x)/(x-x);            /* asin(|x|>1) is NaN */   
00126        } else if (ix<0x3fe00000) { /* |x|<0.5 */
00127            if(ix<0x3e400000) {            /* if |x| < 2**-27 */
00128               if(really_big+x>one) return x;/* return x with inexact if x!=0*/
00129            } else 
00130               t = x*x;
00131               p = t*(pS0+t*(pS1+t*(pS2+t*(pS3+t*(pS4+t*pS5)))));
00132               q = one+t*(qS1+t*(qS2+t*(qS3+t*qS4)));
00133               w = p/q;
00134               return x+x*w;
00135        }
00136        /* 1> |x|>= 0.5 */
00137        w = one-fd_fabs(x);
00138        t = w*0.5;
00139        p = t*(pS0+t*(pS1+t*(pS2+t*(pS3+t*(pS4+t*pS5)))));
00140        q = one+t*(qS1+t*(qS2+t*(qS3+t*qS4)));
00141        s = fd_sqrt(t);
00142        if(ix>=0x3FEF3333) {        /* if |x| > 0.975 */
00143            w = p/q;
00144            t = pio2_hi-(2.0*(s+s*w)-pio2_lo);
00145        } else {
00146            u.d  = s;
00147            __LO(u) = 0;
00148             w = u.d;
00149            c  = (t-w*w)/(s+w);
00150            r  = p/q;
00151            p  = 2.0*s*r-(pio2_lo-2.0*c);
00152            q  = pio4_hi-2.0*w;
00153            t  = pio4_hi-(p-q);
00154        }    
00155        if(hx>0) return t; else return -t;    
00156 }