xref: /netbsd-src/external/lgpl3/mpfr/dist/tests/tcmp.c (revision 5dd36a3bc8bf2a9dec29ceb6349550414570c447)
1 /* Test file for mpfr_cmp.
2 
3 Copyright 1999, 2001-2018 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 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 "mpfr-test.h"
24 
25 int
26 main (void)
27 {
28   double x, y;
29   mpfr_t xx, yy;
30   int c;
31   long i;
32   mpfr_prec_t p;
33 
34   tests_start_mpfr ();
35 
36   mpfr_init (xx);
37   mpfr_init (yy);
38 
39   mpfr_set_prec (xx, 2);
40   mpfr_set_prec (yy, 2);
41   mpfr_set_str_binary(xx, "-0.10E0");
42   mpfr_set_str_binary(yy, "-0.10E0");
43   if ((mpfr_cmp) (xx, yy))
44     {
45       printf ("mpfr_cmp (xx, yy) returns non-zero for prec=2\n");
46       exit (1);
47     }
48 
49   mpfr_set_prec (xx, 65);
50   mpfr_set_prec (yy, 65);
51   mpfr_set_str_binary(xx, "0.10011010101000110101010000000011001001001110001011101011111011101E623");
52   mpfr_set_str_binary(yy, "0.10011010101000110101010000000011001001001110001011101011111011100E623");
53   p = 0;
54   if (mpfr_cmp2 (xx, yy, &p) <= 0 || p != 64)
55     {
56       printf ("Error (1) in mpfr_cmp2\n");
57       exit (1);
58     }
59   mpfr_set_str_binary(xx, "0.10100010001110110111000010001000010011111101000100011101000011100");
60   mpfr_set_str_binary(yy, "0.10100010001110110111000010001000010011111101000100011101000011011");
61   p = 0;
62   if (mpfr_cmp2 (xx, yy, &p) <= 0 || p != 64)
63     {
64       printf ("Error (2) in mpfr_cmp2\n");
65       exit (1);
66     }
67 
68   mpfr_set_prec (xx, 160); mpfr_set_prec (yy, 160);
69   mpfr_set_str_binary (xx, "0.1E1");
70   mpfr_set_str_binary (yy, "0.1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111100000110001110100");
71   p = 0;
72   if (mpfr_cmp2 (xx, yy, &p) <= 0 || p != 144)
73     {
74       printf ("Error (3) in mpfr_cmp2\n");
75       exit (1);
76     }
77 
78   mpfr_set_prec (xx, 53);
79   mpfr_set_prec (yy, 200);
80   mpfr_set_ui (xx, 1, (mpfr_rnd_t) 0);
81   mpfr_set_ui (yy, 1, (mpfr_rnd_t) 0);
82   if (mpfr_cmp (xx, yy) != 0)
83     {
84       printf ("Error in mpfr_cmp: 1.0 != 1.0\n");
85       exit (1);
86     }
87   mpfr_set_prec (yy, 31);
88   mpfr_set_str (xx, "1.0000000002", 10, (mpfr_rnd_t) 0);
89   mpfr_set_ui (yy, 1, (mpfr_rnd_t) 0);
90   if (!(mpfr_cmp (xx,yy)>0))
91     {
92       printf ("Error in mpfr_cmp: not 1.0000000002 > 1.0\n");
93       exit (1);
94     }
95   mpfr_set_prec (yy, 53);
96 
97   /* bug found by Gerardo Ballabio */
98   mpfr_set_ui(xx, 0, MPFR_RNDN);
99   mpfr_set_str (yy, "0.1", 10, MPFR_RNDN);
100   if ((c = mpfr_cmp (xx, yy)) >= 0)
101     {
102       printf ("Error in mpfr_cmp(0.0, 0.1), gives %d\n", c);
103       exit (1);
104     }
105 
106   mpfr_set_inf (xx, 1);
107   mpfr_set_str (yy, "-23489745.0329", 10, MPFR_RNDN);
108   if ((c = mpfr_cmp (xx, yy)) <= 0)
109     {
110       printf ("Error in mpfr_cmp(Infp, 23489745.0329), gives %d\n", c);
111       exit (1);
112     }
113 
114   mpfr_set_inf (xx, 1);
115   mpfr_set_inf (yy, -1);
116   if ((c = mpfr_cmp (xx, yy)) <= 0)
117     {
118       printf ("Error in mpfr_cmp(Infp, Infm), gives %d\n", c);
119       exit (1);
120     }
121 
122   mpfr_set_inf (xx, -1);
123   mpfr_set_inf (yy, 1);
124   if ((c = mpfr_cmp (xx, yy)) >= 0)
125     {
126       printf ("Error in mpfr_cmp(Infm, Infp), gives %d\n", c);
127       exit (1);
128     }
129 
130   mpfr_set_inf (xx, 1);
131   mpfr_set_inf (yy, 1);
132   if ((c = mpfr_cmp (xx, yy)) != 0)
133     {
134       printf ("Error in mpfr_cmp(Infp, Infp), gives %d\n", c);
135       exit (1);
136     }
137 
138   mpfr_set_inf (xx, -1);
139   mpfr_set_inf (yy, -1);
140   if ((c = mpfr_cmp (xx, yy)) != 0)
141     {
142       printf ("Error in mpfr_cmp(Infm, Infm), gives %d\n", c);
143       exit (1);
144     }
145 
146   mpfr_set_inf (xx, -1);
147   mpfr_set_str (yy, "2346.09234", 10, MPFR_RNDN);
148   if ((c = mpfr_cmp (xx, yy)) >= 0)
149     {
150       printf ("Error in mpfr_cmp(Infm, 2346.09234), gives %d\n", c);
151       exit (1);
152     }
153 
154   mpfr_set_ui (xx, 0, MPFR_RNDN);
155   mpfr_set_ui (yy, 1, MPFR_RNDN);
156   if ((c = mpfr_cmp3 (xx, yy, 1)) >= 0)
157     {
158       printf ("Error: mpfr_cmp3 (0, 1, 1) gives %d instead of"
159               " a negative value\n", c);
160       exit (1);
161     }
162   if ((c = mpfr_cmp3 (xx, yy, -1)) <= 0)
163     {
164       printf ("Error: mpfr_cmp3 (0, 1, -1) gives %d instead of"
165               " a positive value\n", c);
166       exit (1);
167     }
168 
169   for (i=0; i<500000; )
170     {
171       x = DBL_RAND ();
172       y = DBL_RAND ();
173       if (!Isnan(x) && !Isnan(y))
174         {
175           i++;
176           mpfr_set_d (xx, x, MPFR_RNDN);
177           mpfr_set_d (yy, y, MPFR_RNDN);
178           c = mpfr_cmp (xx,yy);
179           if ((c>0 && x<=y) || (c==0 && x!=y) || (c<0 && x>=y))
180             {
181               printf ("Error in mpfr_cmp with x=%1.20e, y=%1.20e"
182                       " mpfr_cmp(x,y)=%d\n", x, y, c);
183               exit (1);
184             }
185         }
186     }
187 
188   /* Check for NAN */
189   mpfr_set_nan (xx);
190   mpfr_clear_erangeflag ();
191   c = (mpfr_cmp) (xx, yy);
192   if (c != 0 || !mpfr_erangeflag_p () )
193     {
194       printf ("NAN error (1)\n");
195       exit (1);
196     }
197   mpfr_clear_erangeflag ();
198   c = (mpfr_cmp) (yy, xx);
199   if (c != 0 || !mpfr_erangeflag_p () )
200     {
201       printf ("NAN error (2)\n");
202       exit (1);
203     }
204   mpfr_clear_erangeflag ();
205   c = (mpfr_cmp) (xx, xx);
206   if (c != 0 || !mpfr_erangeflag_p () )
207     {
208       printf ("NAN error (3)\n");
209       exit (1);
210     }
211 
212   mpfr_clear (xx);
213   mpfr_clear (yy);
214 
215   tests_end_mpfr ();
216   return 0;
217 }
218