Back to index

lightning-sunbird  0.9+nobinonly
e_acos.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_acos.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_acos(x)
00053  * Method :                  
00054  *     acos(x)  = pi/2 - asin(x)
00055  *     acos(-x) = pi/2 + asin(x)
00056  * For |x|<=0.5
00057  *     acos(x) = pi/2 - (x + x*x^2*R(x^2))       (see asin.c)
00058  * For x>0.5
00059  *     acos(x) = pi/2 - (pi/2 - 2asin(sqrt((1-x)/2)))
00060  *            = 2asin(sqrt((1-x)/2))  
00061  *            = 2s + 2s*z*R(z)     ...z=(1-x)/2, s=sqrt(z)
00062  *            = 2f + (2c + 2s*z*R(z))
00063  *     where f=hi part of s, and c = (z-f*f)/(s+f) is the correction term
00064  *     for f so that f+c ~ sqrt(z).
00065  * For x<-0.5
00066  *     acos(x) = pi - 2asin(sqrt((1-|x|)/2))
00067  *            = pi - 0.5*(s+s*z*R(z)), where z=(1-|x|)/2,s=sqrt(z)
00068  *
00069  * Special cases:
00070  *     if x is NaN, return x itself;
00071  *     if |x|>1, return NaN with invalid signal.
00072  *
00073  * Function needed: sqrt
00074  */
00075 
00076 #include "fdlibm.h"
00077 
00078 #ifdef __STDC__
00079 static const double 
00080 #else
00081 static double 
00082 #endif
00083 one=  1.00000000000000000000e+00, /* 0x3FF00000, 0x00000000 */
00084 pi =  3.14159265358979311600e+00, /* 0x400921FB, 0x54442D18 */
00085 pio2_hi =  1.57079632679489655800e+00, /* 0x3FF921FB, 0x54442D18 */
00086 pio2_lo =  6.12323399573676603587e-17, /* 0x3C91A626, 0x33145C07 */
00087 pS0 =  1.66666666666666657415e-01, /* 0x3FC55555, 0x55555555 */
00088 pS1 = -3.25565818622400915405e-01, /* 0xBFD4D612, 0x03EB6F7D */
00089 pS2 =  2.01212532134862925881e-01, /* 0x3FC9C155, 0x0E884455 */
00090 pS3 = -4.00555345006794114027e-02, /* 0xBFA48228, 0xB5688F3B */
00091 pS4 =  7.91534994289814532176e-04, /* 0x3F49EFE0, 0x7501B288 */
00092 pS5 =  3.47933107596021167570e-05, /* 0x3F023DE1, 0x0DFDF709 */
00093 qS1 = -2.40339491173441421878e+00, /* 0xC0033A27, 0x1C8A2D4B */
00094 qS2 =  2.02094576023350569471e+00, /* 0x40002AE5, 0x9C598AC8 */
00095 qS3 = -6.88283971605453293030e-01, /* 0xBFE6066C, 0x1B8D0159 */
00096 qS4 =  7.70381505559019352791e-02; /* 0x3FB3B8C5, 0xB12E9282 */
00097 
00098 #ifdef __STDC__
00099        double __ieee754_acos(double x)
00100 #else
00101        double __ieee754_acos(x)
00102        double x;
00103 #endif
00104 {
00105         fd_twoints u;
00106         double df;
00107        double z,p,q,r,w,s,c;
00108        int hx,ix;
00109         u.d = x;
00110        hx = __HI(u);
00111        ix = hx&0x7fffffff;
00112        if(ix>=0x3ff00000) { /* |x| >= 1 */
00113            if(((ix-0x3ff00000)|__LO(u))==0) {    /* |x|==1 */
00114               if(hx>0) return 0.0;        /* acos(1) = 0  */
00115               else return pi+2.0*pio2_lo; /* acos(-1)= pi */
00116            }
00117            return (x-x)/(x-x);            /* acos(|x|>1) is NaN */
00118        }
00119        if(ix<0x3fe00000) {  /* |x| < 0.5 */
00120            if(ix<=0x3c600000) return pio2_hi+pio2_lo;/*if|x|<2**-57*/
00121            z = x*x;
00122            p = z*(pS0+z*(pS1+z*(pS2+z*(pS3+z*(pS4+z*pS5)))));
00123            q = one+z*(qS1+z*(qS2+z*(qS3+z*qS4)));
00124            r = p/q;
00125            return pio2_hi - (x - (pio2_lo-x*r));
00126        } else  if (hx<0) {         /* x < -0.5 */
00127            z = (one+x)*0.5;
00128            p = z*(pS0+z*(pS1+z*(pS2+z*(pS3+z*(pS4+z*pS5)))));
00129            q = one+z*(qS1+z*(qS2+z*(qS3+z*qS4)));
00130            s = fd_sqrt(z);
00131            r = p/q;
00132            w = r*s-pio2_lo;
00133            return pi - 2.0*(s+w);
00134        } else {                    /* x > 0.5 */
00135            z = (one-x)*0.5;
00136            s = fd_sqrt(z);
00137            u.d = s;
00138            __LO(u) = 0;
00139             df = u.d;
00140            c  = (z-df*df)/(s+df);
00141            p = z*(pS0+z*(pS1+z*(pS2+z*(pS3+z*(pS4+z*pS5)))));
00142            q = one+z*(qS1+z*(qS2+z*(qS3+z*qS4)));
00143            r = p/q;
00144            w = r*s+c;
00145            return 2.0*(df+w);
00146        }
00147 }