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