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