xref: /netbsd-src/external/lgpl3/mpfr/dist/tests/tminmax.c (revision 80d9064ac03cbb6a4174695f0d5b237c8766d3d0)
1 /* Test file for mpfr_min & mpfr_max.
2 
3 Copyright 2004, 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 int
29 main (void)
30 {
31   mpfr_t x, y, z;
32 
33   tests_start_mpfr ();
34 
35   mpfr_init (x);
36   mpfr_init (y);
37   mpfr_init (z);
38 
39   /* case x=NaN && y=NAN */
40   mpfr_set_nan (x);
41   mpfr_set_nan (y);
42   mpfr_min (z, x, y, MPFR_RNDN);
43   if (!mpfr_nan_p (z))
44     {
45       printf ("Error in mpfr_min (NaN, NaN)\n");
46       exit (1);
47     }
48   mpfr_max (z, x, y, MPFR_RNDN);
49   if (!mpfr_nan_p (z))
50     {
51       printf ("Error in mpfr_max (NaN, NaN)\n");
52       exit (1);
53     }
54   /* case x=NaN */
55   mpfr_set_nan (x);
56   mpfr_set_ui (y, 0, MPFR_RNDN);
57   mpfr_min (z, x, y, MPFR_RNDN);
58   if (mpfr_cmp_ui (z, 0))
59     {
60       printf ("Error in mpfr_min (NaN, 0)\n");
61       exit (1);
62     }
63   mpfr_min (z, y, x, MPFR_RNDN);
64   if (mpfr_cmp_ui (z, 0))
65     {
66       printf ("Error in mpfr_min (0, NaN)\n");
67       exit (1);
68     }
69   mpfr_max (z, x, y, MPFR_RNDN);
70   if (mpfr_cmp_ui (z, 0))
71     {
72       printf ("Error in mpfr_max (NaN, 0)\n");
73       exit (1);
74     }
75   mpfr_max (z, y, x, MPFR_RNDN);
76   if (mpfr_cmp_ui (z, 0))
77     {
78       printf ("Error in mpfr_max (0, NaN)\n");
79       exit (1);
80     }
81   /* Case x=0+ and x=0- */
82   mpfr_set_ui (x, 0, MPFR_RNDN);
83   mpfr_set_ui (y, 0, MPFR_RNDN); MPFR_SET_NEG(y);
84   mpfr_max (z, x, y, MPFR_RNDN);
85   if (!MPFR_IS_ZERO(z) || MPFR_IS_NEG(z))
86     {
87       printf ("Error in mpfr_max (0+, 0-)\n");
88       exit (1);
89     }
90   mpfr_min (z, x, y, MPFR_RNDN);
91   if (!MPFR_IS_ZERO(z) || MPFR_IS_POS(z))
92     {
93       printf ("Error in mpfr_min (0+, 0-)\n");
94       exit (1);
95     }
96   /* Case x=0- and y=0+ */
97   mpfr_set_ui (x, 0, MPFR_RNDN); MPFR_SET_NEG(x);
98   mpfr_set_ui (y, 0, MPFR_RNDN);
99   mpfr_max (z, x, y, MPFR_RNDN);
100   if (!MPFR_IS_ZERO(z) || MPFR_IS_NEG(z))
101     {
102       printf ("Error in mpfr_max (0+, 0-)\n");
103       exit (1);
104     }
105   mpfr_min (z, x, y, MPFR_RNDN);
106   if (!MPFR_IS_ZERO(z) || MPFR_IS_POS(z))
107     {
108       printf ("Error in mpfr_min (0+, 0-)\n");
109       exit (1);
110     }
111 
112   /* case x=+Inf */
113   mpfr_set_inf (x, 1);
114   mpfr_set_si (y, -12, MPFR_RNDN);
115   mpfr_min (z, x, y, MPFR_RNDN);
116   if ( mpfr_cmp_si (z, -12) )
117     {
118       printf ("Error in mpfr_min (+Inf, -12)\n");
119       exit (1);
120     }
121   mpfr_max (z, x, y, MPFR_RNDN);
122   if ( !MPFR_IS_INF(z) || MPFR_IS_NEG(z) )
123     {
124       printf ("Error in mpfr_max (+Inf, 12)\n");
125       exit (1);
126     }
127   /* case x=-Inf */
128   mpfr_set_inf (x, -1);
129   mpfr_set_ui (y, 12, MPFR_RNDN);
130   mpfr_max (z, x, y, MPFR_RNDN);
131   if ( mpfr_cmp_ui (z, 12) )
132     {
133       printf ("Error in mpfr_max (-Inf, 12)\n");
134       exit (1);
135     }
136   mpfr_min (z, x, y, MPFR_RNDN);
137   if ( !MPFR_IS_INF(z) || MPFR_IS_POS(z) )
138     {
139       printf ("Error in mpfr_min (-Inf, 12)\n");
140       exit (1);
141     }
142 
143   /* case x=17 and y=42 */
144   mpfr_set_ui (x, 17, MPFR_RNDN);
145   mpfr_set_ui (y, 42, MPFR_RNDN);
146   mpfr_max (z, x, y, MPFR_RNDN);
147   if ( mpfr_cmp_ui (z, 42) )
148     {
149       printf ("Error in mpfr_max (17, 42)\n");
150       exit (1);
151     }
152   mpfr_max (z, y, x, MPFR_RNDN);
153   if ( mpfr_cmp_ui (z, 42) )
154     {
155       printf ("Error in mpfr_max (42, 17)\n");
156       exit (1);
157     }
158   mpfr_min (z, y, x, MPFR_RNDN);
159   if ( mpfr_cmp_ui (z, 17) )
160     {
161       printf ("Error in mpfr_min (42, 17)\n");
162       exit (1);
163     }
164 
165   mpfr_clear (x);
166   mpfr_clear (y);
167   mpfr_clear (z);
168 
169   tests_end_mpfr ();
170   return 0;
171 }
172