fe481eec850604bc91b7e16df7421d73bdaaaa2d
[gnulib.git] / tests / test-printf-frexpl.c
1 /* Test of splitting a 'long double' into fraction and mantissa.
2    Copyright (C) 2007 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2, or (at your option)
7    any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software Foundation,
16    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
17
18 /* Written by Bruno Haible <bruno@clisp.org>, 2007.  */
19
20 #include <config.h>
21
22 #include "printf-frexpl.h"
23
24 #include <float.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27
28 #include "fpucw.h"
29
30 #define ASSERT(expr) \
31   do                                                                         \
32     {                                                                        \
33       if (!(expr))                                                           \
34         {                                                                    \
35           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
36           abort ();                                                          \
37         }                                                                    \
38     }                                                                        \
39   while (0)
40
41 /* On MIPS IRIX machines, LDBL_MIN_EXP is -1021, but the smallest reliable
42    exponent for 'long double' is -964.  For exponents below that, the
43    precision may be truncated to the precision used for 'double'.  */
44 #ifdef __sgi
45 # define MIN_NORMAL_EXP (LDBL_MIN_EXP + 57)
46 # define MIN_SUBNORMAL_EXP MIN_NORMAL_EXP
47 #else
48 # define MIN_NORMAL_EXP LDBL_MIN_EXP
49 # define MIN_SUBNORMAL_EXP (LDBL_MIN_EXP - 100)
50 #endif
51
52 static long double
53 my_ldexp (long double x, int d)
54 {
55   for (; d > 0; d--)
56     x *= 2.0L;
57   for (; d < 0; d++)
58     x *= 0.5L;
59   return x;
60 }
61
62 int
63 main ()
64 {
65   int i;
66   long double x;
67   DECL_LONG_DOUBLE_ROUNDING
68
69   BEGIN_LONG_DOUBLE_ROUNDING ();
70
71   for (i = 1, x = 1.0L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
72     {
73       int exp = -9999;
74       long double mantissa = printf_frexpl (x, &exp);
75       ASSERT (exp == i - 1);
76       ASSERT (mantissa == 1.0L);
77     }
78   for (i = 1, x = 1.0L; i >= MIN_NORMAL_EXP; i--, x *= 0.5L)
79     {
80       int exp = -9999;
81       long double mantissa = printf_frexpl (x, &exp);
82       ASSERT (exp == i - 1);
83       ASSERT (mantissa == 1.0L);
84     }
85   for (; i >= MIN_SUBNORMAL_EXP && x > 0.0L; i--, x *= 0.5L)
86     {
87       int exp = -9999;
88       long double mantissa = printf_frexpl (x, &exp);
89       ASSERT (exp == LDBL_MIN_EXP - 1);
90       ASSERT (mantissa == my_ldexp (1.0L, i - LDBL_MIN_EXP));
91     }
92
93   for (i = 1, x = 1.01L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
94     {
95       int exp = -9999;
96       long double mantissa = printf_frexpl (x, &exp);
97       ASSERT (exp == i - 1);
98       ASSERT (mantissa == 1.01L);
99     }
100   for (i = 1, x = 1.01L; i >= MIN_NORMAL_EXP; i--, x *= 0.5L)
101     {
102       int exp = -9999;
103       long double mantissa = printf_frexpl (x, &exp);
104       ASSERT (exp == i - 1);
105       ASSERT (mantissa == 1.01L);
106     }
107   for (; i >= MIN_SUBNORMAL_EXP && x > 0.0L; i--, x *= 0.5L)
108     {
109       int exp = -9999;
110       long double mantissa = printf_frexpl (x, &exp);
111       ASSERT (exp == LDBL_MIN_EXP - 1);
112       ASSERT (mantissa >= my_ldexp (1.0L, i - LDBL_MIN_EXP));
113       ASSERT (mantissa <= my_ldexp (2.0L, i - LDBL_MIN_EXP));
114       ASSERT (mantissa == my_ldexp (x, - exp));
115     }
116
117   for (i = 1, x = 1.73205L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
118     {
119       int exp = -9999;
120       long double mantissa = printf_frexpl (x, &exp);
121       ASSERT (exp == i - 1);
122       ASSERT (mantissa == 1.73205L);
123     }
124   for (i = 1, x = 1.73205L; i >= MIN_NORMAL_EXP; i--, x *= 0.5L)
125     {
126       int exp = -9999;
127       long double mantissa = printf_frexpl (x, &exp);
128       ASSERT (exp == i - 1);
129       ASSERT (mantissa == 1.73205L);
130     }
131   for (; i >= MIN_SUBNORMAL_EXP && x > 0.0L; i--, x *= 0.5L)
132     {
133       int exp = -9999;
134       long double mantissa = printf_frexpl (x, &exp);
135       ASSERT (exp == LDBL_MIN_EXP - 1);
136       ASSERT (mantissa >= my_ldexp (1.0L, i - LDBL_MIN_EXP));
137       ASSERT (mantissa <= my_ldexp (2.0L, i - LDBL_MIN_EXP));
138       ASSERT (mantissa == my_ldexp (x, - exp));
139     }
140
141   return 0;
142 }