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

56 lines
971 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".
*/
/* $Header$ */
#include <math.h>
1990-03-29 09:05:21 +00:00
#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;
1991-03-19 16:39:40 +00:00
if (__IsNan(fl)) {
errno = EDOM;
return fl;
}
1990-03-29 09:05:21 +00:00
if (fl == 0.0) return 0.0;
1989-05-10 16:08:14 +00:00
if (fl<0) {
fl = -fl;
sign = -1;
}
1991-03-19 16:39:40 +00:00
if (fl > DBL_MAX) { /* for infinity */
errno = ERANGE;
return sign * fl;
}
1989-05-10 16:08:14 +00:00
fl = frexp(fl,&currexp);
exp += currexp;
if (exp > 0) {
1990-03-29 09:05:21 +00:00
if (exp > DBL_MAX_EXP) {
errno = ERANGE;
1990-03-29 09:05:21 +00:00
return sign * HUGE_VAL;
}
1989-05-10 16:08:14 +00:00
while (exp>30) {
fl *= (double) (1L << 30);
exp -= 30;
}
fl *= (double) (1L << exp);
}
else {
1990-03-29 09:05:21 +00:00
/* number need not be normalized */
if (exp < DBL_MIN_EXP - DBL_MANT_DIG) {
return 0.0;
}
1989-05-10 16:08:14 +00:00
while (exp<-30) {
fl /= (double) (1L << 30);
exp += 30;
}
fl /= (double) (1L << -exp);
}
return sign * fl;
}