xref: /netbsd-src/external/lgpl3/mpfr/dist/tests/texpm1.c (revision ba125506a622fe649968631a56eba5d42ff57863)
1 /* Test file for mpfr_expm1.
2 
3 Copyright 2001-2023 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 https://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
test_expm1(mpfr_ptr a,mpfr_srcptr b,mpfr_rnd_t rnd_mode)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
special(void)54 special (void)
55 {
56   mpfr_t x, y;
57   int inex;
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   inex = test_expm1 (x, x, MPFR_RNDN);
106   MPFR_ASSERTN (MPFR_IS_INF (x) && MPFR_IS_POS (x));
107   MPFR_ASSERTN (__gmpfr_flags == (MPFR_FLAGS_OVERFLOW | MPFR_FLAGS_INEXACT));
108   MPFR_ASSERTN (inex > 0);
109 
110   mpfr_clear_flags ();
111   mpfr_set_str_binary (x, "1.1E1000000000");
112   inex = test_expm1 (x, x, MPFR_RNDU);
113   MPFR_ASSERTN (MPFR_IS_INF (x) && MPFR_IS_POS (x));
114   MPFR_ASSERTN (__gmpfr_flags == (MPFR_FLAGS_OVERFLOW | MPFR_FLAGS_INEXACT));
115   MPFR_ASSERTN (inex > 0);
116 
117   mpfr_clear_flags ();
118   mpfr_set_str_binary (x, "1.1E1000000000");
119   inex = test_expm1 (x, x, MPFR_RNDD);
120   MPFR_ASSERTN (!MPFR_IS_INF (x) && MPFR_IS_POS (x));
121   MPFR_ASSERTN (__gmpfr_flags == (MPFR_FLAGS_OVERFLOW | MPFR_FLAGS_INEXACT));
122   MPFR_ASSERTN (inex < 0);
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   inex = test_expm1 (x, x, MPFR_RNDN);
129   MPFR_ASSERTN (mpfr_cmp_si0 (x, -1) == 0);
130   MPFR_ASSERTN (__gmpfr_flags == MPFR_FLAGS_INEXACT);
131   MPFR_ASSERTN (inex < 0);
132 
133   mpfr_set_str_binary (x, "-1.1E1000000000");
134   inex = test_expm1 (x, x, MPFR_RNDD);
135   MPFR_ASSERTN (mpfr_cmp_si0 (x, -1) == 0);
136   MPFR_ASSERTN (__gmpfr_flags == MPFR_FLAGS_INEXACT);
137   MPFR_ASSERTN (inex < 0);
138 
139   mpfr_set_str_binary (x, "-1.1E1000000000");
140   inex = test_expm1 (x, x, MPFR_RNDZ);
141   MPFR_ASSERTN (mpfr_cmp_str (x, "-0.11", 2, MPFR_RNDN) == 0);
142   MPFR_ASSERTN (__gmpfr_flags == MPFR_FLAGS_INEXACT);
143   MPFR_ASSERTN (inex > 0);
144 
145   mpfr_set_str_binary (x, "-1.1E1000000000");
146   inex = test_expm1 (x, x, MPFR_RNDU);
147   MPFR_ASSERTN (mpfr_cmp_str (x, "-0.11", 2, MPFR_RNDN) == 0);
148   MPFR_ASSERTN (__gmpfr_flags == MPFR_FLAGS_INEXACT);
149   MPFR_ASSERTN (inex > 0);
150 
151   mpfr_set_str_binary (x, "-1.1E1000000000");
152   inex = test_expm1 (x, x, MPFR_RNDA);
153   MPFR_ASSERTN (mpfr_cmp_si0 (x, -1) == 0);
154   MPFR_ASSERTN (__gmpfr_flags == MPFR_FLAGS_INEXACT);
155   MPFR_ASSERTN (inex < 0);
156 
157   mpfr_clear (x);
158   mpfr_clear (y);
159 }
160 
161 int
main(int argc,char * argv[])162 main (int argc, char *argv[])
163 {
164   tests_start_mpfr ();
165 
166   special ();
167 
168   test_generic (MPFR_PREC_MIN, 100, 100);
169 
170   data_check ("data/expm1", mpfr_expm1, "mpfr_expm1");
171   bad_cases (mpfr_expm1, mpfr_log1p, "mpfr_expm1", 64, -256, 255,
172              4, 128, 800, 80);
173 
174   tests_end_mpfr ();
175   return 0;
176 }
177