1/* Test file for mpfr_log1p.
2
3Copyright 2001-2023 Free Software Foundation, Inc.
4Contributed by the AriC and Caramba projects, INRIA.
5
6This file is part of the GNU MPFR Library.
7
8The GNU MPFR Library is free software; you can redistribute it and/or modify
9it under the terms of the GNU Lesser General Public License as published by
10the Free Software Foundation; either version 3 of the License, or (at your
11option) any later version.
12
13The GNU MPFR Library is distributed in the hope that it will be useful, but
14WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
16License for more details.
17
18You should have received a copy of the GNU Lesser General Public License
19along with the GNU MPFR Library; see the file COPYING.LESSER.  If not, see
20https://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
2151 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
22
23#include "mpfr-test.h"
24
25#ifdef CHECK_EXTERNAL
26static int
27test_log1p (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_log1p (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_log1p mpfr_log1p
46#endif
47
48#define TEST_FUNCTION test_log1p
49#define TEST_RANDOM_EMAX 80
50#include "tgeneric.c"
51
52static void
53special (void)
54{
55  mpfr_t x;
56  int inex;
57
58  mpfr_init (x);
59
60  mpfr_set_nan (x);
61  mpfr_clear_flags ();
62  inex = test_log1p (x, x, MPFR_RNDN);
63  MPFR_ASSERTN (mpfr_nan_p (x) && inex == 0);
64  MPFR_ASSERTN (__gmpfr_flags == MPFR_FLAGS_NAN);
65
66  mpfr_set_inf (x, -1);
67  mpfr_clear_flags ();
68  inex = test_log1p (x, x, MPFR_RNDN);
69  MPFR_ASSERTN (mpfr_nan_p (x) && inex == 0);
70  MPFR_ASSERTN (__gmpfr_flags == MPFR_FLAGS_NAN);
71
72  mpfr_set_inf (x, 1);
73  mpfr_clear_flags ();
74  inex = test_log1p (x, x, MPFR_RNDN);
75  MPFR_ASSERTN (mpfr_inf_p (x) && mpfr_sgn (x) > 0 && inex == 0);
76  MPFR_ASSERTN (__gmpfr_flags == 0);
77
78  mpfr_set_ui (x, 0, MPFR_RNDN);
79  mpfr_clear_flags ();
80  inex = test_log1p (x, x, MPFR_RNDN);
81  MPFR_ASSERTN (mpfr_cmp_ui (x, 0) == 0 && MPFR_IS_POS (x) && inex == 0);
82  MPFR_ASSERTN (__gmpfr_flags == 0);
83  mpfr_neg (x, x, MPFR_RNDN);
84  mpfr_clear_flags ();
85  inex = test_log1p (x, x, MPFR_RNDN);
86  MPFR_ASSERTN (mpfr_cmp_ui (x, 0) == 0 && MPFR_IS_NEG (x) && inex == 0);
87  MPFR_ASSERTN (__gmpfr_flags == 0);
88
89  mpfr_set_si (x, -1, MPFR_RNDN);
90  mpfr_clear_flags ();
91  inex = test_log1p (x, x, MPFR_RNDN);
92  MPFR_ASSERTN (mpfr_inf_p (x) && mpfr_sgn (x) < 0 && inex == 0);
93  MPFR_ASSERTN (__gmpfr_flags == MPFR_FLAGS_DIVBY0);
94
95  mpfr_set_si (x, -2, MPFR_RNDN);
96  mpfr_clear_flags ();
97  inex = test_log1p (x, x, MPFR_RNDN);
98  MPFR_ASSERTN (mpfr_nan_p (x) && inex == 0);
99  MPFR_ASSERTN (__gmpfr_flags == MPFR_FLAGS_NAN);
100
101  mpfr_clear (x);
102}
103
104static void
105other (void)
106{
107  mpfr_t x, y;
108
109  /* Bug reported by Guillaume Melquiond on 2006-08-14. */
110  mpfr_init2 (x, 53);
111  mpfr_set_str (x, "-1.5e4f72873ed9a@-100", 16, MPFR_RNDN);
112  mpfr_init2 (y, 57);
113  mpfr_log1p (y, x, MPFR_RNDU);
114  if (mpfr_cmp (x, y) != 0)
115    {
116      printf ("Error in tlog1p for x = ");
117      mpfr_out_str (stdout, 16, 0, x, MPFR_RNDN);
118      printf (", rnd = MPFR_RNDU\nExpected ");
119      mpfr_out_str (stdout, 16, 15, x, MPFR_RNDN);
120      printf ("\nGot      ");
121      mpfr_out_str (stdout, 16, 15, y, MPFR_RNDN);
122      printf ("\n");
123      exit (1);
124    }
125
126  mpfr_clear (y);
127  mpfr_clear (x);
128  return;
129}
130
131int
132main (int argc, char *argv[])
133{
134  tests_start_mpfr ();
135
136  special ();
137  other ();
138
139  test_generic (MPFR_PREC_MIN, 100, 50);
140
141  data_check ("data/log1p", mpfr_log1p, "mpfr_log1p");
142  bad_cases (mpfr_log1p, mpfr_expm1, "mpfr_log1p", 256, -64, 9,
143             4, 128, 800, 40);
144
145  tests_end_mpfr ();
146  return 0;
147}
148