Use a more portable replacement expression for -0.0L.
[gnulib.git] / tests / test-ldexpl.c
1 /* Test of multiplying a 'long double' by a power of 2.
2    Copyright (C) 2007-2008 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 3 of the License, or
7    (at your option) 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, see <http://www.gnu.org/licenses/>.  */
16
17 /* Written by Bruno Haible <bruno@clisp.org>, 2007.  */
18
19 #include <config.h>
20
21 #include <math.h>
22
23 #include <float.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26
27 #include "fpucw.h"
28 #include "isnanl-nolibm.h"
29
30 #define ASSERT(expr) \
31   do                                                                         \
32     {                                                                        \
33       if (!(expr))                                                           \
34         {                                                                    \
35           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
36           fflush (stderr);                                                   \
37           abort ();                                                          \
38         }                                                                    \
39     }                                                                        \
40   while (0)
41
42 /* On HP-UX 10.20, negating 0.0L does not yield -0.0L.
43    So we use minus_zero instead.  */
44 long double minus_zero = -LDBL_MIN * LDBL_MIN;
45
46 int
47 main ()
48 {
49   int i;
50   long double x;
51   long double y;
52   DECL_LONG_DOUBLE_ROUNDING
53
54   BEGIN_LONG_DOUBLE_ROUNDING ();
55
56   { /* NaN.  */
57     x = 0.0L / 0.0L;
58     y = ldexpl (x, 0); ASSERT (isnanl (y));
59     y = ldexpl (x, 5); ASSERT (isnanl (y));
60     y = ldexpl (x, -5); ASSERT (isnanl (y));
61   }
62
63   { /* Positive infinity.  */
64     x = 1.0L / 0.0L;
65     y = ldexpl (x, 0); ASSERT (y == x);
66     y = ldexpl (x, 5); ASSERT (y == x);
67     y = ldexpl (x, -5); ASSERT (y == x);
68   }
69
70   { /* Negative infinity.  */
71     x = -1.0L / 0.0L;
72     y = ldexpl (x, 0); ASSERT (y == x);
73     y = ldexpl (x, 5); ASSERT (y == x);
74     y = ldexpl (x, -5); ASSERT (y == x);
75   }
76
77   { /* Positive zero.  */
78     x = 0.0L;
79     y = ldexpl (x, 0); ASSERT (y == x); ASSERT (!signbit (x));
80     y = ldexpl (x, 5); ASSERT (y == x); ASSERT (!signbit (x));
81     y = ldexpl (x, -5); ASSERT (y == x); ASSERT (!signbit (x));
82   }
83
84   { /* Negative zero.  */
85     x = minus_zero;
86     y = ldexpl (x, 0); ASSERT (y == x); ASSERT (signbit (x));
87     y = ldexpl (x, 5); ASSERT (y == x); ASSERT (signbit (x));
88     y = ldexpl (x, -5); ASSERT (y == x); ASSERT (signbit (x));
89   }
90
91   { /* Positive finite number.  */
92     x = 1.73205L;
93     y = ldexpl (x, 0); ASSERT (y == x);
94     y = ldexpl (x, 5); ASSERT (y == x * 32.0L);
95     y = ldexpl (x, -5); ASSERT (y == x * 0.03125L);
96   }
97
98   { /* Negative finite number.  */
99     x = -20.085536923187667742L;
100     y = ldexpl (x, 0); ASSERT (y == x);
101     y = ldexpl (x, 5); ASSERT (y == x * 32.0L);
102     y = ldexpl (x, -5); ASSERT (y == x * 0.03125L);
103   }
104
105   for (i = 1, x = 1.73205L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
106     {
107       y = ldexpl (x, 0); ASSERT (y == x);
108       y = ldexpl (x, 5); ASSERT (y == x * 32.0L);
109       y = ldexpl (x, -5); ASSERT (y == x * 0.03125L);
110     }
111   for (i = 1, x = 1.73205L; i >= LDBL_MIN_EXP; i--, x *= 0.5L)
112     {
113       y = ldexpl (x, 0); ASSERT (y == x);
114       y = ldexpl (x, 5); ASSERT (y == x * 32.0L);
115       if (i - 5 >= LDBL_MIN_EXP)
116         {
117           y = ldexpl (x, -5); ASSERT (y == x * 0.03125L);
118         }
119     }
120   for (; i >= LDBL_MIN_EXP - 100 && x > 0.0L; i--, x *= 0.5L)
121     {
122       y = ldexpl (x, 0); ASSERT (y == x);
123       y = ldexpl (x, 5); ASSERT (y == x * 32.0L);
124     }
125
126   return 0;
127 }