| 1 |
/* sys/ldfrexp.c |
| 2 |
* |
| 3 |
* Copyright (C) 2002, Gert Van den Eynde |
| 4 |
* |
| 5 |
* This program is free software; you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation; either version 3 of the License, or (at |
| 8 |
* your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, but |
| 11 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
* General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License |
| 16 |
* along with this program; if not, write to the Free Software |
| 17 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 18 |
*/ |
| 19 |
|
| 20 |
#include "gsl__config.h" |
| 21 |
#include <math.h> |
| 22 |
#include "gsl_math.h" |
| 23 |
|
| 24 |
double |
| 25 |
gsl_ldexp (const double x, const int e) |
| 26 |
{ |
| 27 |
double p2 = pow (2.0, (double)e); |
| 28 |
return x * p2; |
| 29 |
} |
| 30 |
|
| 31 |
double |
| 32 |
gsl_frexp (const double x, int *e) |
| 33 |
{ |
| 34 |
if (x == 0.0) |
| 35 |
{ |
| 36 |
*e = 0; |
| 37 |
return 0.0; |
| 38 |
} |
| 39 |
else |
| 40 |
{ |
| 41 |
double ex = ceil (log (fabs (x)) / M_LN2); |
| 42 |
int ei = (int) ex; |
| 43 |
double f = gsl_ldexp (x, -ei); |
| 44 |
|
| 45 |
while (fabs (f) >= 1.0) |
| 46 |
{ |
| 47 |
ei++; |
| 48 |
f /= 2.0; |
| 49 |
} |
| 50 |
|
| 51 |
while (fabs (f) < 0.5) |
| 52 |
{ |
| 53 |
ei--; |
| 54 |
f *= 2.0; |
| 55 |
} |
| 56 |
|
| 57 |
*e = ei; |
| 58 |
return f; |
| 59 |
} |
| 60 |
} |