3 * Copyright (C) 2002, Gert Van den Eynde
4 * Copyright (C) 2007, Brian Gough
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or (at
9 * your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 #include <gsl/gsl_math.h>
26 gsl_ldexp (const double x, const int e)
36 double y = gsl_frexp (x, &ex);
37 double e2 = e + ex, p2;
39 if (e2 >= DBL_MAX_EXP)
41 y *= pow (2.0, e2 - DBL_MAX_EXP + 1);
44 else if (e2 <= DBL_MIN_EXP)
46 y *= pow (2.0, e2 - DBL_MIN_EXP - 1);
56 gsl_frexp (const double x, int *e)
68 else if (fabs (x) >= 0.5 && fabs (x) < 1) /* Handle the common case */
75 double ex = ceil (log (fabs (x)) / M_LN2);
79 /* Prevent underflow and overflow of 2**(-ei) */
83 if (ei > -DBL_MIN_EXP)
86 f = x * pow (2.0, -ei);
90 /* This should not happen */
95 while (fabs (f) >= 1.0)
101 while (fabs (f) > 0 && fabs (f) < 0.5)