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

109 lines
1.7 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 <float.h>
#include <errno.h>
#include "localmath.h"
1989-05-10 16:08:14 +00:00
static double
sinus(double x, int cos_flag)
1989-05-10 16:08:14 +00:00
{
/* Algorithm and coefficients from:
"Software manual for the elementary functions"
by W.J. Cody and W. Waite, Prentice-Hall, 1980
*/
1989-05-10 16:08:14 +00:00
static double r[] = {
-0.16666666666666665052e+0,
2018-06-21 20:33:47 +00:00
0.83333333333331650314e-2,
-0.19841269841201840457e-3,
2018-06-21 20:33:47 +00:00
0.27557319210152756119e-5,
-0.25052106798274584544e-7,
2018-06-21 20:33:47 +00:00
0.16058936490371589114e-9,
-0.76429178068910467734e-12,
2018-06-21 20:33:47 +00:00
0.27204790957888846175e-14
1989-05-10 16:08:14 +00:00
};
2018-06-21 20:33:47 +00:00
double y;
int neg = 1;
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 (x < 0)
{
1989-05-10 16:08:14 +00:00
x = -x;
1991-03-19 16:39:40 +00:00
neg = -1;
1989-05-10 16:08:14 +00:00
}
2018-06-21 20:33:47 +00:00
if (cos_flag)
{
1991-03-19 16:39:40 +00:00
neg = 1;
y = M_PI_2 + x;
1989-05-10 16:08:14 +00:00
}
2018-06-21 20:33:47 +00:00
else
y = x;
/* ??? avoid loss of significance, if y is too large, error ??? */
y = y * M_1_PI + 0.5;
2018-06-21 20:33:47 +00:00
if (y >= DBL_MAX / M_PI)
return 0.0;
1991-03-19 16:39:40 +00:00
2018-06-21 20:33:47 +00:00
/* Use extended precision to calculate reduced argument.
Here we used 12 bits of the mantissa for a1.
Also split x in integer part x1 and fraction part x2.
*/
#define A1 3.1416015625
#define A2 -8.908910206761537356617e-6
{
double x1, x2;
modf(y, &y);
2018-06-21 20:33:47 +00:00
if (modf(0.5 * y, &x1))
neg = -neg;
if (cos_flag)
y -= 0.5;
x2 = modf(x, &x1);
x = x1 - y * A1;
1989-05-10 16:08:14 +00:00
x += x2;
x -= y * A2;
1989-05-10 16:08:14 +00:00
#undef A1
#undef A2
}
2018-06-21 20:33:47 +00:00
if (x < 0)
{
1991-03-19 16:39:40 +00:00
neg = -neg;
1989-05-10 16:08:14 +00:00
x = -x;
}
/* ??? avoid underflow ??? */
y = x * x;
x += x * y * POLYNOM7(y, r);
2018-06-21 20:33:47 +00:00
return neg == -1 ? -x : x;
1989-05-10 16:08:14 +00:00
}
double
sin(double x)
{
return sinus(x, 0);
}
double
cos(double x)
{
2018-06-21 20:33:47 +00:00
if (x < 0)
x = -x;
1989-05-10 16:08:14 +00:00
return sinus(x, 1);
}