Fix a bug with 1.0.
[gnulib.git] / lib / frexpl.c
1 /* Emulation for frexpl.
2    Contributed by Paolo Bonzini
3
4    Copyright 2002, 2003, 2007 Free Software Foundation, Inc.
5
6    This file is part of gnulib.
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2, or (at your option)
11    any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License along
19    with this program; if not, write to the Free Software Foundation,
20    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
21
22 #include <config.h>
23
24 /* Specification.  */
25 #include <math.h>
26
27 #include <float.h>
28
29 /* Binary search.  Quite inefficient but portable. */
30 long double
31 frexpl(long double x, int *exp)
32 {
33   /* Since the exponent is an 'int', it fits in 64 bits.  Therefore the
34      loops are executed no more than 64 times.  */
35   long double exponents[64];
36   long double *next;
37   int exponent, bit;
38
39   /* Check for zero, nan and infinity. */
40   if (x != x || x + x == x)
41     {
42       *exp = 0;
43       return x;
44     }
45
46   if (x < 0)
47     return -frexpl(-x, exp);
48
49   exponent = 0;
50   if (x >= 1.0)
51     {
52       for (next = exponents, exponents[0] = 2.0L, bit = 1;
53            *next <= x + x;
54            bit <<= 1, next[1] = next[0] * next[0], next++);
55
56       for (; next >= exponents; bit >>= 1, next--)
57         if (x + x >= *next)
58           {
59             x /= *next;
60             exponent |= bit;
61           }
62
63     }
64
65   else if (x < 0.5)
66     {
67       for (next = exponents, exponents[0] = 0.5L, bit = 1;
68            *next > x;
69            bit <<= 1, next[1] = next[0] * next[0], next++);
70
71       for (; next >= exponents; bit >>= 1, next--)
72         if (x < *next)
73           {
74             x /= *next;
75             exponent |= bit;
76           }
77
78       exponent = -exponent;
79     }
80
81   *exp = exponent;
82   return x;
83 }
84
85 #if 0
86 int
87 main (void)
88 {
89   long double x;
90   int y;
91   x = frexpl(0.0L / 0.0L, &y); printf ("%.6Lg %d\n", x, y);
92   x = frexpl(1.0L / 0.0L, &y); printf ("%.6Lg %d\n", x, y);
93   x = frexpl(-1.0L / 0.0L, &y); printf ("%.6Lg %d\n", x, y);
94   x = frexpl(0.5L, &y); printf ("%.6Lg %d\n", x, y);
95   x = frexpl(0.75L, &y); printf ("%.6Lg %d\n", x, y);
96   x = frexpl(1.0L, &y); printf ("%.6Lg %d\n", x, y);
97   x = frexpl(3.6L, &y); printf ("%.6Lg %d\n", x, y);
98   x = frexpl(17.8L, &y); printf ("%.6Lg %d\n", x, y);
99   x = frexpl(8.0L, &y); printf ("%.6Lg %d\n", x, y);
100   x = frexpl(0.3L, &y); printf ("%.6Lg %d\n", x, y);
101   x = frexpl(0.49L, &y); printf ("%.6Lg %d\n", x, y);
102   x = frexpl(0.049L, &y); printf ("%.6Lg %d\n", x, y);
103   x = frexpl(0.0245L, &y); printf ("%.6Lg %d\n", x, y);
104   x = frexpl(0.0625L, &y); printf ("%.6Lg %d\n", x, y);
105 }
106 #endif
107