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

66 lines
984 B
C
Raw Normal View History

1989-05-10 16:08:14 +00:00
/*
* (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
* See the copyright notice in the ACK home directory, in the file "Copyright".
*/
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>
1989-05-10 16:08:14 +00:00
double
ldexp(double fl, int exp)
{
int sign = 1;
int currexp;
2018-06-21 20:33:47 +00:00
if (__IsNan(fl))
{
1991-03-19 16:39:40 +00:00
errno = EDOM;
return fl;
}
2018-06-21 20:33:47 +00:00
if (fl == 0.0)
return 0.0;
if (fl < 0)
{
1989-05-10 16:08:14 +00:00
fl = -fl;
sign = -1;
}
2018-06-21 20:33:47 +00:00
if (fl > DBL_MAX)
{ /* for infinity */
1991-03-19 16:39:40 +00:00
errno = ERANGE;
return sign * fl;
}
2018-06-21 20:33:47 +00:00
fl = frexp(fl, &currexp);
1989-05-10 16:08:14 +00:00
exp += currexp;
2018-06-21 20:33:47 +00:00
if (exp > 0)
{
if (exp > DBL_MAX_EXP)
{
errno = ERANGE;
1990-03-29 09:05:21 +00:00
return sign * HUGE_VAL;
}
2018-06-21 20:33:47 +00:00
while (exp > 30)
{
fl *= (double)(1L << 30);
1989-05-10 16:08:14 +00:00
exp -= 30;
}
2018-06-21 20:33:47 +00:00
fl *= (double)(1L << exp);
1989-05-10 16:08:14 +00:00
}
2018-06-21 20:33:47 +00:00
else
{
1990-03-29 09:05:21 +00:00
/* number need not be normalized */
2018-06-21 20:33:47 +00:00
if (exp < DBL_MIN_EXP - DBL_MANT_DIG)
{
1990-03-29 09:05:21 +00:00
return 0.0;
}
2018-06-21 20:33:47 +00:00
while (exp < -30)
{
fl /= (double)(1L << 30);
1989-05-10 16:08:14 +00:00
exp += 30;
}
2018-06-21 20:33:47 +00:00
fl /= (double)(1L << -exp);
1989-05-10 16:08:14 +00:00
}
return sign * fl;
}