1 /* mpfr_tli2 -- test file for dilogarithm function
2
3 Copyright 2007-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_li2
26 #include "tgeneric.c"
27
28 static void
special(void)29 special (void)
30 {
31 mpfr_t x, y;
32 mpfr_init (x);
33 mpfr_init (y);
34
35 mpfr_set_nan (x);
36 mpfr_li2 (y, x, MPFR_RNDN);
37 if (!mpfr_nan_p (y))
38 {
39 printf ("Error for li2(NaN)\n");
40 exit (1);
41 }
42
43 mpfr_set_inf (x, -1);
44 mpfr_li2 (y, x, MPFR_RNDN);
45 if (!MPFR_IS_INF (y) || MPFR_IS_POS (y))
46 {
47 printf ("Error for li2(-Inf)\n");
48 exit (1);
49 }
50
51 mpfr_set_inf (x, 1);
52 mpfr_li2 (y, x, MPFR_RNDN);
53 if (!MPFR_IS_INF (y) || MPFR_IS_POS (y))
54 {
55 printf ("Error for li2(+Inf)\n");
56 exit (1);
57 }
58
59 mpfr_set_ui (x, 0, MPFR_RNDN);
60 mpfr_li2 (y, x, MPFR_RNDN);
61 if (!MPFR_IS_ZERO (y) || MPFR_IS_NEG (y))
62 {
63 printf ("Error for li2(+0)\n");
64 exit (1);
65 }
66
67 mpfr_set_ui (x, 0, MPFR_RNDN);
68 mpfr_neg (x, x, MPFR_RNDN);
69 mpfr_li2 (y, x, MPFR_RNDN);
70 if (!MPFR_IS_ZERO (y) || MPFR_IS_POS (y))
71 {
72 printf ("Error for li2(-0)\n");
73 exit (1);
74 }
75
76 mpfr_clear (x);
77 mpfr_clear (y);
78 }
79
80 static void
normal(void)81 normal (void)
82 {
83 int inexact;
84 mpfr_t x, y;
85 mpfr_init (x);
86 mpfr_init (y);
87
88 /* x1 = 2^-3 */
89 mpfr_set_str (x, "1p-3", 2, MPFR_RNDD);
90 mpfr_li2 (x, x, MPFR_RNDN);
91 if (mpfr_cmp_str (x, "0x1087a7a9e42141p-55", 16, MPFR_RNDN) != 0)
92 {
93 printf ("Error for li2(x1)\n");
94 exit (1);
95 }
96
97 /* check MPFR_FAST_COMPUTE_IF_SMALL_INPUT */
98 mpfr_set_prec (x, 2);
99 mpfr_set_prec (y, 20);
100 mpfr_set_ui_2exp (x, 1, -21, MPFR_RNDN);
101 mpfr_li2 (y, x, MPFR_RNDN);
102 MPFR_ASSERTN(mpfr_cmp (y, x) == 0);
103
104 mpfr_set_si_2exp (x, -1, -21, MPFR_RNDN);
105 mpfr_li2 (y, x, MPFR_RNDN);
106 MPFR_ASSERTN(mpfr_cmp (y, x) == 0);
107
108 /* worst case */
109 /* x2 = 0x7F18EA6537E00E983196CDDC6EFAC57Fp-129
110 Li2(x2) = 2^-2 + 2^-6 + 2^-120 */
111 mpfr_set_prec (x, 128);
112 mpfr_set_str (x, "7F18EA6537E00E983196CDDC6EFAC57Fp-129", 16, MPFR_RNDN);
113
114 /* round to nearest mode and 4 bits of precision,
115 it should be rounded to 2^-2 + 2^-5 and */
116 mpfr_set_prec (y, 4);
117 inexact = mpfr_li2 (y, x, MPFR_RNDN);
118 if (inexact != 1 || mpfr_cmp_str (y, "0.1001p-1", 2, MPFR_RNDN) != 0)
119 {
120 printf ("Error for li2(x2, RNDN)\n");
121 exit (1);
122 }
123
124 /* round toward zero mode and 5 bits of precision,
125 it should be rounded to 2^-2 + 2^-6 */
126 mpfr_set_prec (y, 5);
127 inexact = mpfr_li2 (y, x, MPFR_RNDZ);
128 if (inexact != -1 || mpfr_cmp_str (y, "0.10001p-1", 2, MPFR_RNDN) != 0)
129 {
130 printf ("Error for li2(x2, RNDZ)\n");
131 exit (1);
132 }
133
134 /* round away from zero mode and 5 bits of precision,
135 it should be rounded to 2^-2 + 2^-5 */
136 inexact = mpfr_li2 (y, x, MPFR_RNDU);
137 if (inexact != 1 || mpfr_cmp_str (y, "0.10010p-1", 2, MPFR_RNDN) != 0)
138 {
139 printf ("Error for li2(x2, RNDU)\n");
140 exit (1);
141 }
142
143 mpfr_clear (x);
144 mpfr_clear (y);
145 }
146
147 static void
bug20091013(void)148 bug20091013 (void)
149 {
150 mpfr_t x, y;
151 int inex;
152
153 mpfr_init2 (x, 17);
154 mpfr_init2 (y, 2);
155 mpfr_set_str_binary (x, "0.10000000000000000E-16");
156 inex = mpfr_li2 (y, x, MPFR_RNDN);
157 if (mpfr_cmp_ui_2exp (y, 1, -17) != 0)
158 {
159 printf ("Error in bug20091013()\n");
160 printf ("expected 2^(-17)\n");
161 printf ("got ");
162 mpfr_dump (y);
163 exit (1);
164 }
165 if (inex >= 0)
166 {
167 printf ("Error in bug20091013()\n");
168 printf ("expected negative ternary value, got %d\n", inex);
169 exit (1);
170 }
171 mpfr_clear (x);
172 mpfr_clear (y);
173 }
174
175 int
main(int argc,char * argv[])176 main (int argc, char *argv[])
177 {
178 tests_start_mpfr ();
179
180 bug20091013 ();
181
182 special ();
183
184 normal ();
185
186 test_generic (MPFR_PREC_MIN, 100, 2);
187
188 data_check ("data/li2", mpfr_li2, "mpfr_li2");
189
190 tests_end_mpfr ();
191 return 0;
192 }
193