Support for MSVC compiler: Avoid division by a literal 0.
[gnulib.git] / tests / test-ldexpl.c
1 /* Test of multiplying a 'long double' by a power of 2.
2    Copyright (C) 2007-2011 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 "signature.h"
24 SIGNATURE_CHECK (ldexpl, long double, (long double, int));
25
26 #include <float.h>
27
28 #include "fpucw.h"
29 #include "isnanl-nolibm.h"
30 #include "minus-zero.h"
31 #include "infinity.h"
32 #include "nan.h"
33 #include "macros.h"
34
35 int
36 main ()
37 {
38   int i;
39   long double x;
40   long double y;
41   DECL_LONG_DOUBLE_ROUNDING
42
43   BEGIN_LONG_DOUBLE_ROUNDING ();
44
45   { /* NaN.  */
46     x = NaNl ();
47     y = ldexpl (x, 0); ASSERT (isnanl (y));
48     y = ldexpl (x, 5); ASSERT (isnanl (y));
49     y = ldexpl (x, -5); ASSERT (isnanl (y));
50   }
51
52   { /* Positive infinity.  */
53     x = Infinityl ();
54     y = ldexpl (x, 0); ASSERT (y == x);
55     y = ldexpl (x, 5); ASSERT (y == x);
56     y = ldexpl (x, -5); ASSERT (y == x);
57   }
58
59   { /* Negative infinity.  */
60     x = - Infinityl ();
61     y = ldexpl (x, 0); ASSERT (y == x);
62     y = ldexpl (x, 5); ASSERT (y == x);
63     y = ldexpl (x, -5); ASSERT (y == x);
64   }
65
66   { /* Positive zero.  */
67     x = 0.0L;
68     y = ldexpl (x, 0); ASSERT (y == x); ASSERT (!signbit (x));
69     y = ldexpl (x, 5); ASSERT (y == x); ASSERT (!signbit (x));
70     y = ldexpl (x, -5); ASSERT (y == x); ASSERT (!signbit (x));
71   }
72
73   { /* Negative zero.  */
74     x = minus_zerol;
75     y = ldexpl (x, 0); ASSERT (y == x); ASSERT (signbit (x));
76     y = ldexpl (x, 5); ASSERT (y == x); ASSERT (signbit (x));
77     y = ldexpl (x, -5); ASSERT (y == x); ASSERT (signbit (x));
78   }
79
80   { /* Positive finite number.  */
81     x = 1.73205L;
82     y = ldexpl (x, 0); ASSERT (y == x);
83     y = ldexpl (x, 5); ASSERT (y == x * 32.0L);
84     y = ldexpl (x, -5); ASSERT (y == x * 0.03125L);
85   }
86
87   { /* Negative finite number.  */
88     x = -20.085536923187667742L;
89     y = ldexpl (x, 0); ASSERT (y == x);
90     y = ldexpl (x, 5); ASSERT (y == x * 32.0L);
91     y = ldexpl (x, -5); ASSERT (y == x * 0.03125L);
92   }
93
94   for (i = 1, x = 1.73205L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
95     {
96       y = ldexpl (x, 0); ASSERT (y == x);
97       y = ldexpl (x, 5); ASSERT (y == x * 32.0L);
98       y = ldexpl (x, -5); ASSERT (y == x * 0.03125L);
99     }
100   for (i = 1, x = 1.73205L; i >= LDBL_MIN_EXP; i--, x *= 0.5L)
101     {
102       y = ldexpl (x, 0); ASSERT (y == x);
103       y = ldexpl (x, 5); ASSERT (y == x * 32.0L);
104       if (i - 5 >= LDBL_MIN_EXP)
105         {
106           y = ldexpl (x, -5); ASSERT (y == x * 0.03125L);
107         }
108     }
109   for (; i >= LDBL_MIN_EXP - 100 && x > 0.0L; i--, x *= 0.5L)
110     {
111       y = ldexpl (x, 0); ASSERT (y == x);
112       y = ldexpl (x, 5); ASSERT (y == x * 32.0L);
113     }
114
115   return 0;
116 }