xref: /netbsd-src/external/lgpl3/mpfr/dist/tests/tcmpabs.c (revision 37afb7eb6895c833050f8bfb1d1bb2f99f332539)
1 /* Test file for mpfr_cmpabs.
2 
3 Copyright 2004, 2005, 2006, 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 #define ERROR(s) do { printf(s); exit(1); } while (0)
29 
30 int
31 main (void)
32 {
33   mpfr_t xx, yy;
34   int c;
35 
36   tests_start_mpfr ();
37 
38   mpfr_init2 (xx, 2);
39   mpfr_init2 (yy, 2);
40 
41   mpfr_clear_erangeflag ();
42   MPFR_SET_NAN (xx);
43   MPFR_SET_NAN (yy);
44   if (mpfr_cmpabs (xx, yy) != 0)
45     ERROR ("mpfr_cmpabs (NAN,NAN) returns non-zero\n");
46   if (!mpfr_erangeflag_p ())
47     ERROR ("mpfr_cmpabs (NAN,NAN) doesn't set erange flag\n");
48 
49   mpfr_set_str_binary (xx, "0.10E0");
50   mpfr_set_str_binary (yy, "-0.10E0");
51   if (mpfr_cmpabs (xx, yy) != 0)
52     ERROR ("mpfr_cmpabs (xx, yy) returns non-zero for prec=2\n");
53 
54   mpfr_set_prec (xx, 65);
55   mpfr_set_prec (yy, 65);
56   mpfr_set_str_binary (xx, "-0.10011010101000110101010000000011001001001110001011101011111011101E623");
57   mpfr_set_str_binary (yy, "0.10011010101000110101010000000011001001001110001011101011111011100E623");
58   if (mpfr_cmpabs (xx, yy) <= 0)
59     ERROR ("Error (1) in mpfr_cmpabs\n");
60 
61   mpfr_set_str_binary (xx, "-0.10100010001110110111000010001000010011111101000100011101000011100");
62   mpfr_set_str_binary (yy, "-0.10100010001110110111000010001000010011111101000100011101000011011");
63   if (mpfr_cmpabs (xx, yy) <= 0)
64     ERROR ("Error (2) in mpfr_cmpabs\n");
65 
66   mpfr_set_prec (xx, 160);
67   mpfr_set_prec (yy, 160);
68   mpfr_set_str_binary (xx, "0.1E1");
69   mpfr_set_str_binary (yy, "-0.1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111100000110001110100");
70   if (mpfr_cmpabs (xx, yy) <= 0)
71     ERROR ("Error (3) in mpfr_cmpabs\n");
72 
73   mpfr_set_prec(xx, 53);
74   mpfr_set_prec(yy, 200);
75   mpfr_set_ui (xx, 1, (mpfr_rnd_t) 0);
76   mpfr_set_ui (yy, 1, (mpfr_rnd_t) 0);
77   if (mpfr_cmpabs(xx, yy) != 0)
78     ERROR ("Error in mpfr_cmpabs: 1.0 != 1.0\n");
79 
80   mpfr_set_prec (yy, 31);
81   mpfr_set_str (xx, "-1.0000000002", 10, (mpfr_rnd_t) 0);
82   mpfr_set_ui (yy, 1, (mpfr_rnd_t) 0);
83   if (!(mpfr_cmpabs(xx,yy)>0))
84     ERROR ("Error in mpfr_cmpabs: not 1.0000000002 > 1.0\n");
85   mpfr_set_prec(yy, 53);
86 
87   mpfr_set_ui(xx, 0, MPFR_RNDN);
88   mpfr_set_str (yy, "-0.1", 10, MPFR_RNDN);
89   if (mpfr_cmpabs(xx, yy) >= 0)
90     ERROR ("Error in mpfr_cmpabs(0.0, 0.1)\n");
91 
92   mpfr_set_inf (xx, -1);
93   mpfr_set_str (yy, "23489745.0329", 10, MPFR_RNDN);
94   if (mpfr_cmpabs(xx, yy) <= 0)
95     ERROR ("Error in mpfr_cmp(-Inf, 23489745.0329)\n");
96 
97   mpfr_set_inf (xx, 1);
98   mpfr_set_inf (yy, -1);
99   if (mpfr_cmpabs(xx, yy) != 0)
100     ERROR ("Error in mpfr_cmpabs(Inf, -Inf)\n");
101 
102   mpfr_set_inf (yy, -1);
103   mpfr_set_str (xx, "2346.09234", 10, MPFR_RNDN);
104   if (mpfr_cmpabs (xx, yy) >= 0)
105     ERROR ("Error in mpfr_cmpabs(-Inf, 2346.09234)\n");
106 
107   mpfr_set_prec (xx, 2);
108   mpfr_set_prec (yy, 128);
109   mpfr_set_str_binary (xx, "0.1E10");
110   mpfr_set_str_binary (yy,
111                        "0.100000000000000000000000000000000000000000000000"
112                        "00000000000000000000000000000000000000000000001E10");
113   if (mpfr_cmpabs (xx, yy) >= 0)
114     ERROR ("Error in mpfr_cmpabs(10.235, 2346.09234)\n");
115   mpfr_swap (xx, yy);
116   if (mpfr_cmpabs(xx, yy) <= 0)
117     ERROR ("Error in mpfr_cmpabs(2346.09234, 10.235)\n");
118   mpfr_swap (xx, yy);
119 
120   /* Check for NAN */
121   mpfr_set_nan (xx);
122   mpfr_clear_erangeflag ();
123   c = (mpfr_cmp) (xx, yy);
124   if (c != 0 || !mpfr_erangeflag_p () )
125     {
126       printf ("NAN error (1)\n");
127       exit (1);
128     }
129   mpfr_clear_erangeflag ();
130   c = (mpfr_cmp) (yy, xx);
131   if (c != 0 || !mpfr_erangeflag_p () )
132     {
133       printf ("NAN error (2)\n");
134       exit (1);
135     }
136   mpfr_clear_erangeflag ();
137   c = (mpfr_cmp) (xx, xx);
138   if (c != 0 || !mpfr_erangeflag_p () )
139     {
140       printf ("NAN error (3)\n");
141       exit (1);
142     }
143 
144   mpfr_clear (xx);
145   mpfr_clear (yy);
146 
147   tests_end_mpfr ();
148   return 0;
149 }
150