ack/lang/cem/libcc.ansi/core/math/asin.c

93 lines
1.3 KiB
C
Raw Normal View History

1989-05-10 16:08:14 +00:00
/*
* (c) copyright 1988 by the Vrije Universiteit, Amsterdam, The Netherlands.
* See the copyright notice in the ACK home directory, in the file "Copyright".
*
* Author: Ceriel J.H. Jacobs
*/
1994-06-24 14:02:31 +00:00
/* $Id$ */
1989-05-10 16:08:14 +00:00
2018-06-21 20:33:47 +00:00
#include <math.h>
#include <errno.h>
#include "localmath.h"
1989-05-10 16:08:14 +00:00
static double
asin_acos(double x, int cosfl)
{
int negative = x < 0;
2018-06-21 20:33:47 +00:00
int i;
double g;
static double p[] = {
-0.27368494524164255994e+2,
2018-06-21 20:33:47 +00:00
0.57208227877891731407e+2,
-0.39688862997540877339e+2,
2018-06-21 20:33:47 +00:00
0.10152522233806463645e+2,
-0.69674573447350646411e+0
};
static double q[] = {
-0.16421096714498560795e+3,
2018-06-21 20:33:47 +00:00
0.41714430248260412556e+3,
-0.38186303361750149284e+3,
2018-06-21 20:33:47 +00:00
0.15095270841030604719e+3,
-0.23823859153670238830e+2,
2018-06-21 20:33:47 +00:00
1.0
};
1989-05-10 16:08:14 +00:00
2018-06-21 20:33:47 +00:00
if (__IsNan(x))
{
1991-03-19 16:39:40 +00:00
errno = EDOM;
return x;
}
2018-06-21 20:33:47 +00:00
if (negative)
{
1989-05-10 16:08:14 +00:00
x = -x;
}
2018-06-21 20:33:47 +00:00
if (x > 0.5)
{
i = 1;
2018-06-21 20:33:47 +00:00
if (x > 1)
{
errno = EDOM;
return 0;
}
g = 0.5 - 0.5 * x;
2018-06-21 20:33:47 +00:00
x = -sqrt(g);
x += x;
1989-05-10 16:08:14 +00:00
}
2018-06-21 20:33:47 +00:00
else
{
/* ??? avoid underflow ??? */
i = 0;
g = x * x;
1989-05-10 16:08:14 +00:00
}
x += x * g * POLYNOM4(g, p) / POLYNOM5(g, q);
2018-06-21 20:33:47 +00:00
if (cosfl)
{
if (!negative)
x = -x;
}
2018-06-21 20:33:47 +00:00
if ((cosfl == 0) == (i == 1))
{
x = (x + M_PI_4) + M_PI_4;
}
2018-06-21 20:33:47 +00:00
else if (cosfl && negative && i == 1)
{
x = (x + M_PI_2) + M_PI_2;
1989-05-10 16:08:14 +00:00
}
2018-06-21 20:33:47 +00:00
if (!cosfl && negative)
x = -x;
1989-05-10 16:08:14 +00:00
return x;
}
double
asin(double x)
{
return asin_acos(x, 0);
}
double
acos(double x)
{
return asin_acos(x, 1);
}