ack/lang/cem/libcc.ansi/pure/math/exp.c

77 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 <float.h>
#include <errno.h>
#include "localmath.h"
1989-05-10 16:08:14 +00:00
double
exp(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
static double p[] = {
0.25000000000000000000e+0,
0.75753180159422776666e-2,
0.31555192765684646356e-4
1989-05-10 16:08:14 +00:00
};
static double q[] = {
0.50000000000000000000e+0,
0.56817302698551221787e-1,
0.63121894374398503557e-3,
0.75104028399870046114e-6
1989-05-10 16:08:14 +00:00
};
2018-06-21 20:33:47 +00:00
double xn, g;
int n;
int negative = x < 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 (x < M_LN_MIN_D)
{
1990-04-09 16:54:09 +00:00
errno = ERANGE;
1990-03-29 09:05:21 +00:00
return 0.0;
1989-05-10 16:08:14 +00:00
}
2018-06-21 20:33:47 +00:00
if (x > M_LN_MAX_D)
{
1990-04-09 16:54:09 +00:00
errno = ERANGE;
1990-03-29 09:05:21 +00:00
return HUGE_VAL;
1989-05-10 16:08:14 +00:00
}
2018-06-21 20:33:47 +00:00
if (negative)
x = -x;
/* ??? avoid underflow ??? */
2018-06-21 20:33:47 +00:00
n = x * M_LOG2E + 0.5; /* 1/ln(2) = log2(e), 0.5 added for rounding */
xn = n;
{
2018-06-21 20:33:47 +00:00
double x1 = (long)x;
double x2 = x - x1;
2018-06-21 20:33:47 +00:00
g = ((x1 - xn * 0.693359375) + x2) - xn * (-2.1219444005469058277e-4);
1989-05-10 16:08:14 +00:00
}
2018-06-21 20:33:47 +00:00
if (negative)
{
g = -g;
n = -n;
1989-05-10 16:08:14 +00:00
}
xn = g * g;
x = g * POLYNOM2(xn, p);
n += 1;
2018-06-21 20:33:47 +00:00
return (ldexp(0.5 + x / (POLYNOM3(xn, q) - x), n));
1989-05-10 16:08:14 +00:00
}