ack/lang/basic/lib/sin.c

106 lines
1.7 KiB
C
Raw Normal View History

1984-11-29 14:22:02 +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".
1984-11-29 14:22:02 +00:00
*
* Author: Ceriel J.H. Jacobs
1984-11-29 14:22:02 +00:00
*/
1994-06-24 11:31:16 +00:00
/* $Id$ */
1984-11-29 14:22:02 +00:00
1988-07-25 11:40:57 +00:00
#define __NO_DEFS
#include <math.h>
1984-11-29 14:22:02 +00:00
static double
1989-06-19 15:56:30 +00:00
sinus(x, cos_flag)
double x;
{
1989-06-19 15:56:30 +00:00
/* Algorithm and coefficients from:
"Software manual for the elementary functions"
by W.J. Cody and W. Waite, Prentice-Hall, 1980
*/
1984-11-29 14:22:02 +00:00
1989-06-19 15:56:30 +00:00
static double r[] = {
-0.16666666666666665052e+0,
0.83333333333331650314e-2,
-0.19841269841201840457e-3,
0.27557319210152756119e-5,
-0.25052106798274584544e-7,
0.16058936490371589114e-9,
-0.76429178068910467734e-12,
0.27204790957888846175e-14
};
1984-11-29 14:22:02 +00:00
1989-06-19 15:56:30 +00:00
double xsqr;
double y;
int neg = 0;
1984-11-29 14:22:02 +00:00
if (x < 0) {
1984-11-29 14:22:02 +00:00
x = -x;
1989-06-19 15:56:30 +00:00
neg = 1;
1984-11-29 14:22:02 +00:00
}
1989-06-19 15:56:30 +00:00
if (cos_flag) {
neg = 0;
y = M_PI_2 + x;
1984-11-29 14:22:02 +00:00
}
1989-06-19 15:56:30 +00:00
else y = x;
/* ??? avoid loss of significance, if y is too large, error ??? */
y = y * M_1_PI + 0.5;
/* 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;
extern double _fif();
_fif(y, 1.0, &y);
if (_fif(y, 0.5, &x1)) neg = !neg;
if (cos_flag) y -= 0.5;
x2 = _fif(x, 1.0, &x1);
x = x1 - y * A1;
x += x2;
1989-06-19 15:56:30 +00:00
x -= y * A2;
#undef A1
#undef A2
}
1989-06-19 15:56:30 +00:00
if (x < 0) {
neg = !neg;
x = -x;
}
1989-06-19 15:56:30 +00:00
/* ??? avoid underflow ??? */
y = x * x;
x += x * y * POLYNOM7(y, r);
return neg ? -x : x;
1984-11-29 14:22:02 +00:00
}
double
_sin(x)
double x;
1984-11-29 14:22:02 +00:00
{
return sinus(x, 0);
1984-11-29 14:22:02 +00:00
}
double
_cos(x)
double x;
1984-11-29 14:22:02 +00:00
{
if (x < 0) x = -x;
return sinus(x, 1);
1984-11-29 14:22:02 +00:00
}
/* EXTENSION */
double
_tan(x)
double x;
1984-11-29 14:22:02 +00:00
{
return _sin(x)/_cos(x);
1984-11-29 14:22:02 +00:00
}