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