xref: /netbsd-src/external/lgpl3/mpfr/dist/tests/tlog10p1.c (revision 70f7362772ba52b749c976fb5e86e39a8b2c9afc)
1 /* Test file for mpfr_log10p1.
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 #define TEST_FUNCTION mpfr_log10p1
26 #define TEST_RANDOM_EMAX 80
27 #include "tgeneric.c"
28 
29 static void
30 special (void)
31 {
32   mpfr_t x;
33   int inex;
34 
35   mpfr_init2 (x, MPFR_PREC_MIN);
36 
37   mpfr_set_nan (x);
38   mpfr_clear_flags ();
39   inex = mpfr_log10p1 (x, x, MPFR_RNDN);
40   MPFR_ASSERTN (mpfr_nan_p (x) && inex == 0);
41   MPFR_ASSERTN (__gmpfr_flags == MPFR_FLAGS_NAN);
42 
43   mpfr_set_inf (x, -1);
44   mpfr_clear_flags ();
45   inex = mpfr_log10p1 (x, x, MPFR_RNDN);
46   MPFR_ASSERTN (mpfr_nan_p (x) && inex == 0);
47   MPFR_ASSERTN (__gmpfr_flags == MPFR_FLAGS_NAN);
48 
49   mpfr_set_inf (x, 1);
50   mpfr_clear_flags ();
51   inex = mpfr_log10p1 (x, x, MPFR_RNDN);
52   MPFR_ASSERTN (mpfr_inf_p (x) && mpfr_sgn (x) > 0 && inex == 0);
53   MPFR_ASSERTN (__gmpfr_flags == 0);
54 
55   mpfr_set_ui (x, 0, MPFR_RNDN);
56   mpfr_clear_flags ();
57   inex = mpfr_log10p1 (x, x, MPFR_RNDN);
58   MPFR_ASSERTN (mpfr_cmp_ui (x, 0) == 0 && MPFR_IS_POS (x) && inex == 0);
59   MPFR_ASSERTN (__gmpfr_flags == 0);
60   mpfr_neg (x, x, MPFR_RNDN);
61   mpfr_clear_flags ();
62   inex = mpfr_log10p1 (x, x, MPFR_RNDN);
63   MPFR_ASSERTN (mpfr_cmp_ui (x, 0) == 0 && MPFR_IS_NEG (x) && inex == 0);
64   MPFR_ASSERTN (__gmpfr_flags == 0);
65 
66   mpfr_set_si (x, -1, MPFR_RNDN);
67   mpfr_clear_flags ();
68   inex = mpfr_log10p1 (x, x, MPFR_RNDN);
69   MPFR_ASSERTN (mpfr_inf_p (x) && mpfr_sgn (x) < 0 && inex == 0);
70   MPFR_ASSERTN (__gmpfr_flags == MPFR_FLAGS_DIVBY0);
71 
72   mpfr_set_si (x, -2, MPFR_RNDN);
73   mpfr_clear_flags ();
74   inex = mpfr_log10p1 (x, x, MPFR_RNDN);
75   MPFR_ASSERTN (mpfr_nan_p (x) && inex == 0);
76   MPFR_ASSERTN (__gmpfr_flags == MPFR_FLAGS_NAN);
77 
78   /* include one hard-coded test */
79   mpfr_set_prec (x, 32);
80   mpfr_set_ui (x, 17, MPFR_RNDN);
81   inex = mpfr_log10p1 (x, x, MPFR_RNDN);
82   MPFR_ASSERTN (mpfr_cmp_ui_2exp (x, 1347838589UL, -30) == 0);
83   MPFR_ASSERTN (inex < 0);
84 
85   mpfr_clear (x);
86 }
87 
88 /* check exact cases, when 1+x = 10^k (can only occur for k >= 0) */
89 static void
90 check_exact (void)
91 {
92   mpfr_exp_t k;
93   mpfr_t x;
94   int inex, r;
95 
96 #define KMAX 100
97   mpfr_init2 (x, 333); /* 10^100 has 333 bits */
98   for (k = 0; k <= KMAX; k++)
99     RND_LOOP (r)
100       {
101         inex = mpfr_ui_pow_ui (x, 10, k, (mpfr_rnd_t) r);
102         MPFR_ASSERTN(inex == 0);
103         inex = mpfr_sub_ui (x, x, 1, (mpfr_rnd_t) r);
104         MPFR_ASSERTN(inex == 0);
105         inex = mpfr_log10p1 (x, x, (mpfr_rnd_t) r);
106         MPFR_ASSERTN(mpfr_cmp_si0 (x, k) == 0);
107         MPFR_ASSERTN(inex == 0);
108       }
109   mpfr_clear (x);
110 }
111 
112 int
113 main (int argc, char *argv[])
114 {
115   tests_start_mpfr ();
116 
117   special ();
118 
119   check_exact ();
120 
121   test_generic (MPFR_PREC_MIN, 100, 50);
122 
123   tests_end_mpfr ();
124   return 0;
125 }
126