1*84d9c625SLionel Sambuc /* $NetBSD: n_asincos.c,v 1.8 2013/11/24 14:41:53 martin Exp $ */
22fe8fb19SBen Gras /*
32fe8fb19SBen Gras * Copyright (c) 1985, 1993
42fe8fb19SBen Gras * The Regents of the University of California. All rights reserved.
52fe8fb19SBen Gras *
62fe8fb19SBen Gras * Redistribution and use in source and binary forms, with or without
72fe8fb19SBen Gras * modification, are permitted provided that the following conditions
82fe8fb19SBen Gras * are met:
92fe8fb19SBen Gras * 1. Redistributions of source code must retain the above copyright
102fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer.
112fe8fb19SBen Gras * 2. Redistributions in binary form must reproduce the above copyright
122fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer in the
132fe8fb19SBen Gras * documentation and/or other materials provided with the distribution.
142fe8fb19SBen Gras * 3. Neither the name of the University nor the names of its contributors
152fe8fb19SBen Gras * may be used to endorse or promote products derived from this software
162fe8fb19SBen Gras * without specific prior written permission.
172fe8fb19SBen Gras *
182fe8fb19SBen Gras * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
192fe8fb19SBen Gras * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
202fe8fb19SBen Gras * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
212fe8fb19SBen Gras * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
222fe8fb19SBen Gras * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
232fe8fb19SBen Gras * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
242fe8fb19SBen Gras * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
252fe8fb19SBen Gras * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
262fe8fb19SBen Gras * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
272fe8fb19SBen Gras * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
282fe8fb19SBen Gras * SUCH DAMAGE.
292fe8fb19SBen Gras */
302fe8fb19SBen Gras
312fe8fb19SBen Gras #ifndef lint
322fe8fb19SBen Gras #if 0
332fe8fb19SBen Gras static char sccsid[] = "@(#)asincos.c 8.1 (Berkeley) 6/4/93";
342fe8fb19SBen Gras #endif
352fe8fb19SBen Gras #endif /* not lint */
362fe8fb19SBen Gras
372fe8fb19SBen Gras /* ASIN(X)
382fe8fb19SBen Gras * RETURNS ARC SINE OF X
392fe8fb19SBen Gras * DOUBLE PRECISION (IEEE DOUBLE 53 bits, VAX D FORMAT 56 bits)
402fe8fb19SBen Gras * CODED IN C BY K.C. NG, 4/16/85, REVISED ON 6/10/85.
412fe8fb19SBen Gras *
422fe8fb19SBen Gras * Required system supported functions:
432fe8fb19SBen Gras * copysign(x,y)
442fe8fb19SBen Gras * sqrt(x)
452fe8fb19SBen Gras *
462fe8fb19SBen Gras * Required kernel function:
472fe8fb19SBen Gras * atan2(y,x)
482fe8fb19SBen Gras *
492fe8fb19SBen Gras * Method :
502fe8fb19SBen Gras * asin(x) = atan2(x,sqrt(1-x*x)); for better accuracy, 1-x*x is
512fe8fb19SBen Gras * computed as follows
522fe8fb19SBen Gras * 1-x*x if x < 0.5,
532fe8fb19SBen Gras * 2*(1-|x|)-(1-|x|)*(1-|x|) if x >= 0.5.
542fe8fb19SBen Gras *
552fe8fb19SBen Gras * Special cases:
562fe8fb19SBen Gras * if x is NaN, return x itself;
572fe8fb19SBen Gras * if |x|>1, return NaN.
582fe8fb19SBen Gras *
592fe8fb19SBen Gras * Accuracy:
602fe8fb19SBen Gras * 1) If atan2() uses machine PI, then
612fe8fb19SBen Gras *
622fe8fb19SBen Gras * asin(x) returns (PI/pi) * (the exact arc sine of x) nearly rounded;
632fe8fb19SBen Gras * and PI is the exact pi rounded to machine precision (see atan2 for
642fe8fb19SBen Gras * details):
652fe8fb19SBen Gras *
662fe8fb19SBen Gras * in decimal:
672fe8fb19SBen Gras * pi = 3.141592653589793 23846264338327 .....
682fe8fb19SBen Gras * 53 bits PI = 3.141592653589793 115997963 ..... ,
692fe8fb19SBen Gras * 56 bits PI = 3.141592653589793 227020265 ..... ,
702fe8fb19SBen Gras *
712fe8fb19SBen Gras * in hexadecimal:
722fe8fb19SBen Gras * pi = 3.243F6A8885A308D313198A2E....
732fe8fb19SBen Gras * 53 bits PI = 3.243F6A8885A30 = 2 * 1.921FB54442D18 error=.276ulps
742fe8fb19SBen Gras * 56 bits PI = 3.243F6A8885A308 = 4 * .C90FDAA22168C2 error=.206ulps
752fe8fb19SBen Gras *
762fe8fb19SBen Gras * In a test run with more than 200,000 random arguments on a VAX, the
772fe8fb19SBen Gras * maximum observed error in ulps (units in the last place) was
782fe8fb19SBen Gras * 2.06 ulps. (comparing against (PI/pi)*(exact asin(x)));
792fe8fb19SBen Gras *
802fe8fb19SBen Gras * 2) If atan2() uses true pi, then
812fe8fb19SBen Gras *
822fe8fb19SBen Gras * asin(x) returns the exact asin(x) with error below about 2 ulps.
832fe8fb19SBen Gras *
842fe8fb19SBen Gras * In a test run with more than 1,024,000 random arguments on a VAX, the
852fe8fb19SBen Gras * maximum observed error in ulps (units in the last place) was
862fe8fb19SBen Gras * 1.99 ulps.
872fe8fb19SBen Gras */
882fe8fb19SBen Gras
892fe8fb19SBen Gras #include "mathimpl.h"
902fe8fb19SBen Gras
912fe8fb19SBen Gras double
asin(double x)922fe8fb19SBen Gras asin(double x)
932fe8fb19SBen Gras {
942fe8fb19SBen Gras double s,t,one=1.0;
952fe8fb19SBen Gras #if !defined(__vax__)&&!defined(tahoe)
962fe8fb19SBen Gras if(x!=x) return(x); /* x is NaN */
972fe8fb19SBen Gras #endif /* !defined(__vax__)&&!defined(tahoe) */
982fe8fb19SBen Gras s=copysign(x,one);
992fe8fb19SBen Gras if(s <= 0.5)
1002fe8fb19SBen Gras return(atan2(x,sqrt(one-x*x)));
1012fe8fb19SBen Gras else
1022fe8fb19SBen Gras { t=one-s; s=t+t; return(atan2(x,sqrt(s-t*t))); }
1032fe8fb19SBen Gras
1042fe8fb19SBen Gras }
1052fe8fb19SBen Gras
106*84d9c625SLionel Sambuc float
asinf(float x)107*84d9c625SLionel Sambuc asinf(float x)
108*84d9c625SLionel Sambuc {
109*84d9c625SLionel Sambuc return (float)asin(x);
110*84d9c625SLionel Sambuc }
111*84d9c625SLionel Sambuc
1122fe8fb19SBen Gras /* ACOS(X)
1132fe8fb19SBen Gras * RETURNS ARC COS OF X
1142fe8fb19SBen Gras * DOUBLE PRECISION (IEEE DOUBLE 53 bits, VAX D FORMAT 56 bits)
1152fe8fb19SBen Gras * CODED IN C BY K.C. NG, 4/16/85, REVISED ON 6/10/85.
1162fe8fb19SBen Gras *
1172fe8fb19SBen Gras * Required system supported functions:
1182fe8fb19SBen Gras * copysign(x,y)
1192fe8fb19SBen Gras * sqrt(x)
1202fe8fb19SBen Gras *
1212fe8fb19SBen Gras * Required kernel function:
1222fe8fb19SBen Gras * atan2(y,x)
1232fe8fb19SBen Gras *
1242fe8fb19SBen Gras * Method :
1252fe8fb19SBen Gras * ________
1262fe8fb19SBen Gras * / 1 - x
1272fe8fb19SBen Gras * acos(x) = 2*atan2( / -------- , 1 ) .
1282fe8fb19SBen Gras * \/ 1 + x
1292fe8fb19SBen Gras *
1302fe8fb19SBen Gras * Special cases:
1312fe8fb19SBen Gras * if x is NaN, return x itself;
1322fe8fb19SBen Gras * if |x|>1, return NaN.
1332fe8fb19SBen Gras *
1342fe8fb19SBen Gras * Accuracy:
1352fe8fb19SBen Gras * 1) If atan2() uses machine PI, then
1362fe8fb19SBen Gras *
1372fe8fb19SBen Gras * acos(x) returns (PI/pi) * (the exact arc cosine of x) nearly rounded;
1382fe8fb19SBen Gras * and PI is the exact pi rounded to machine precision (see atan2 for
1392fe8fb19SBen Gras * details):
1402fe8fb19SBen Gras *
1412fe8fb19SBen Gras * in decimal:
1422fe8fb19SBen Gras * pi = 3.141592653589793 23846264338327 .....
1432fe8fb19SBen Gras * 53 bits PI = 3.141592653589793 115997963 ..... ,
1442fe8fb19SBen Gras * 56 bits PI = 3.141592653589793 227020265 ..... ,
1452fe8fb19SBen Gras *
1462fe8fb19SBen Gras * in hexadecimal:
1472fe8fb19SBen Gras * pi = 3.243F6A8885A308D313198A2E....
1482fe8fb19SBen Gras * 53 bits PI = 3.243F6A8885A30 = 2 * 1.921FB54442D18 error=.276ulps
1492fe8fb19SBen Gras * 56 bits PI = 3.243F6A8885A308 = 4 * .C90FDAA22168C2 error=.206ulps
1502fe8fb19SBen Gras *
1512fe8fb19SBen Gras * In a test run with more than 200,000 random arguments on a VAX, the
1522fe8fb19SBen Gras * maximum observed error in ulps (units in the last place) was
1532fe8fb19SBen Gras * 2.07 ulps. (comparing against (PI/pi)*(exact acos(x)));
1542fe8fb19SBen Gras *
1552fe8fb19SBen Gras * 2) If atan2() uses true pi, then
1562fe8fb19SBen Gras *
1572fe8fb19SBen Gras * acos(x) returns the exact acos(x) with error below about 2 ulps.
1582fe8fb19SBen Gras *
1592fe8fb19SBen Gras * In a test run with more than 1,024,000 random arguments on a VAX, the
1602fe8fb19SBen Gras * maximum observed error in ulps (units in the last place) was
1612fe8fb19SBen Gras * 2.15 ulps.
1622fe8fb19SBen Gras */
1632fe8fb19SBen Gras
1642fe8fb19SBen Gras double
acos(double x)1652fe8fb19SBen Gras acos(double x)
1662fe8fb19SBen Gras {
1672fe8fb19SBen Gras double t,one=1.0;
1682fe8fb19SBen Gras #if !defined(__vax__)&&!defined(tahoe)
1692fe8fb19SBen Gras if(x!=x) return(x);
1702fe8fb19SBen Gras #endif /* !defined(__vax__)&&!defined(tahoe) */
1712fe8fb19SBen Gras if( x != -1.0)
1722fe8fb19SBen Gras t=atan2(sqrt((one-x)/(one+x)),one);
1732fe8fb19SBen Gras else
1742fe8fb19SBen Gras t=atan2(one,0.0); /* t = PI/2 */
1752fe8fb19SBen Gras return(t+t);
1762fe8fb19SBen Gras }
177*84d9c625SLionel Sambuc
178*84d9c625SLionel Sambuc float
acosf(float x)179*84d9c625SLionel Sambuc acosf(float x)
180*84d9c625SLionel Sambuc {
181*84d9c625SLionel Sambuc return (float)acos(x);
182*84d9c625SLionel Sambuc }
183