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

82 lines
1.5 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
double
tan(double x)
{
/* 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
*/
int negative = x < 0;
int invert = 0;
2018-06-21 20:33:47 +00:00
double y;
static double p[] = {
1.0,
-0.13338350006421960681e+0,
2018-06-21 20:33:47 +00:00
0.34248878235890589960e-2,
-0.17861707342254426711e-4
};
2018-06-21 20:33:47 +00:00
static double q[] = {
1.0,
-0.46671683339755294240e+0,
2018-06-21 20:33:47 +00:00
0.25663832289440112864e-1,
-0.31181531907010027307e-3,
2018-06-21 20:33:47 +00:00
0.49819433993786512270e-6
};
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)
x = -x;
/* ??? avoid loss of significance, error if x is too large ??? */
1989-05-10 16:08:14 +00:00
y = x * M_2_PI + 0.5;
1989-05-10 16:08:14 +00:00
2018-06-21 20:33:47 +00:00
if (y >= DBL_MAX / M_PI_2)
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.
*/
2018-06-21 20:33:47 +00:00
#define A1 1.57080078125
#define A2 -4.454455103380768678308e-6
{
double x1, x2;
modf(y, &y);
2018-06-21 20:33:47 +00:00
if (modf(0.5 * y, &x1))
invert = 1;
x2 = modf(x, &x1);
x = x1 - y * A1;
x += x2;
x -= y * A2;
2018-06-21 20:33:47 +00:00
#undef A1
#undef A2
1989-05-10 16:08:14 +00:00
}
/* ??? avoid underflow ??? */
y = x * x;
2018-06-21 20:33:47 +00:00
x += x * y * POLYNOM2(y, p + 1);
y = POLYNOM4(y, q);
2018-06-21 20:33:47 +00:00
if (negative)
x = -x;
return invert ? -y / x : x / y;
1989-05-10 16:08:14 +00:00
}