xref: /netbsd-src/external/lgpl3/mpfr/dist/tests/texpm1.c (revision 33881f779a77dce6440bdc44610d94de75bebefe)
1 /* Test file for mpfr_expm1.
2 
3 Copyright 2001-2018 Free Software Foundation, Inc.
4 Contributed by the AriC and Caramba projects, INRIA.
5 
6 This file is part of the GNU MPFR Library.
7 
8 The GNU MPFR Library is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or (at your
11 option) any later version.
12 
13 The GNU MPFR Library is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
16 License for more details.
17 
18 You should have received a copy of the GNU Lesser General Public License
19 along with the GNU MPFR Library; see the file COPYING.LESSER.  If not, see
20 http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
21 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
22 
23 #include "mpfr-test.h"
24 
25 #ifdef CHECK_EXTERNAL
26 static int
27 test_expm1 (mpfr_ptr a, mpfr_srcptr b, mpfr_rnd_t rnd_mode)
28 {
29   int res;
30   int ok = rnd_mode == MPFR_RNDN && mpfr_number_p (b) && mpfr_get_prec (a)>=53;
31   if (ok)
32     {
33       mpfr_print_raw (b);
34     }
35   res = mpfr_expm1 (a, b, rnd_mode);
36   if (ok)
37     {
38       printf (" ");
39       mpfr_print_raw (a);
40       printf ("\n");
41     }
42   return res;
43 }
44 #else
45 #define test_expm1 mpfr_expm1
46 #endif
47 
48 #define TEST_FUNCTION test_expm1
49 #define TEST_RANDOM_EMIN -36
50 #define TEST_RANDOM_EMAX 36
51 #include "tgeneric.c"
52 
53 static void
54 special (void)
55 {
56   mpfr_t x, y;
57   int i;
58 
59   mpfr_init (x);
60   mpfr_init (y);
61 
62   mpfr_set_nan (x);
63   test_expm1 (y, x, MPFR_RNDN);
64   if (!mpfr_nan_p (y))
65     {
66       printf ("Error for expm1(NaN)\n");
67       exit (1);
68     }
69 
70   mpfr_set_inf (x, 1);
71   test_expm1 (y, x, MPFR_RNDN);
72   if (!mpfr_inf_p (y) || mpfr_sgn (y) < 0)
73     {
74       printf ("Error for expm1(+Inf)\n");
75       exit (1);
76     }
77 
78   mpfr_set_inf (x, -1);
79   test_expm1 (y, x, MPFR_RNDN);
80   if (mpfr_cmp_si (y, -1))
81     {
82       printf ("Error for expm1(-Inf)\n");
83       exit (1);
84     }
85 
86   mpfr_set_ui (x, 0, MPFR_RNDN);
87   test_expm1 (y, x, MPFR_RNDN);
88   if (MPFR_NOTZERO (y) || MPFR_IS_NEG (y))
89     {
90       printf ("Error for expm1(+0)\n");
91       exit (1);
92     }
93 
94   mpfr_neg (x, x, MPFR_RNDN);
95   test_expm1 (y, x, MPFR_RNDN);
96   if (MPFR_NOTZERO (y) || MPFR_IS_POS (y))
97     {
98       printf ("Error for expm1(-0)\n");
99       exit (1);
100     }
101 
102   /* Check overflow of expm1(x) */
103   mpfr_clear_flags ();
104   mpfr_set_str_binary (x, "1.1E1000000000");
105   i = test_expm1 (x, x, MPFR_RNDN);
106   MPFR_ASSERTN (MPFR_IS_INF (x) && MPFR_IS_POS (x));
107   MPFR_ASSERTN (mpfr_overflow_p ());
108   MPFR_ASSERTN (i == 1);
109 
110   mpfr_clear_flags ();
111   mpfr_set_str_binary (x, "1.1E1000000000");
112   i = test_expm1 (x, x, MPFR_RNDU);
113   MPFR_ASSERTN (MPFR_IS_INF (x) && MPFR_IS_POS (x));
114   MPFR_ASSERTN (mpfr_overflow_p ());
115   MPFR_ASSERTN (i == 1);
116 
117   mpfr_clear_flags ();
118   mpfr_set_str_binary (x, "1.1E1000000000");
119   i = test_expm1 (x, x, MPFR_RNDD);
120   MPFR_ASSERTN (!MPFR_IS_INF (x) && MPFR_IS_POS (x));
121   MPFR_ASSERTN (mpfr_overflow_p ());
122   MPFR_ASSERTN (i == -1);
123 
124   /* Check internal underflow of expm1 (x) */
125   mpfr_set_prec (x, 2);
126   mpfr_clear_flags ();
127   mpfr_set_str_binary (x, "-1.1E1000000000");
128   i = test_expm1 (x, x, MPFR_RNDN);
129   MPFR_ASSERTN (mpfr_cmp_si (x, -1) == 0);
130   MPFR_ASSERTN (!mpfr_overflow_p () && !mpfr_underflow_p ());
131   MPFR_ASSERTN (i == -1);
132 
133   mpfr_set_str_binary (x, "-1.1E1000000000");
134   i = test_expm1 (x, x, MPFR_RNDD);
135   MPFR_ASSERTN (mpfr_cmp_si (x, -1) == 0);
136   MPFR_ASSERTN (!mpfr_overflow_p () && !mpfr_underflow_p ());
137   MPFR_ASSERTN (i == -1);
138 
139   mpfr_set_str_binary (x, "-1.1E1000000000");
140   i = test_expm1 (x, x, MPFR_RNDZ);
141   MPFR_ASSERTN (mpfr_cmp_str (x, "-0.11", 2, MPFR_RNDN) == 0);
142   MPFR_ASSERTN (!mpfr_overflow_p () && !mpfr_underflow_p ());
143   MPFR_ASSERTN (i == 1);
144 
145   mpfr_set_str_binary (x, "-1.1E1000000000");
146   i = test_expm1 (x, x, MPFR_RNDU);
147   MPFR_ASSERTN (mpfr_cmp_str (x, "-0.11", 2, MPFR_RNDN) == 0);
148   MPFR_ASSERTN (!mpfr_overflow_p () && !mpfr_underflow_p ());
149   MPFR_ASSERTN (i == 1);
150 
151   mpfr_clear (x);
152   mpfr_clear (y);
153 }
154 
155 int
156 main (int argc, char *argv[])
157 {
158   tests_start_mpfr ();
159 
160   special ();
161 
162   test_generic (MPFR_PREC_MIN, 100, 100);
163 
164   data_check ("data/expm1", mpfr_expm1, "mpfr_expm1");
165   bad_cases (mpfr_expm1, mpfr_log1p, "mpfr_expm1", 256, -256, 255,
166              4, 128, 800, 40);
167 
168   tests_end_mpfr ();
169   return 0;
170 }
171