1/* Test file for mpfr_min & mpfr_max.
2
3Copyright 2004, 2006-2023 Free Software Foundation, Inc.
4Contributed by the AriC and Caramba projects, INRIA.
5
6This file is part of the GNU MPFR Library.
7
8The GNU MPFR Library is free software; you can redistribute it and/or modify
9it under the terms of the GNU Lesser General Public License as published by
10the Free Software Foundation; either version 3 of the License, or (at your
11option) any later version.
12
13The GNU MPFR Library is distributed in the hope that it will be useful, but
14WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
16License for more details.
17
18You should have received a copy of the GNU Lesser General Public License
19along with the GNU MPFR Library; see the file COPYING.LESSER.  If not, see
20https://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
2151 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
22
23#include "mpfr-test.h"
24
25int
26main (void)
27{
28  mpfr_t x, y, z;
29
30  tests_start_mpfr ();
31
32  mpfr_init (x);
33  mpfr_init (y);
34  mpfr_init (z);
35
36  /* case x=NaN && y=NAN */
37  mpfr_set_nan (x);
38  mpfr_set_nan (y);
39  mpfr_min (z, x, y, MPFR_RNDN);
40  if (!mpfr_nan_p (z))
41    {
42      printf ("Error in mpfr_min (NaN, NaN)\n");
43      exit (1);
44    }
45  mpfr_max (z, x, y, MPFR_RNDN);
46  if (!mpfr_nan_p (z))
47    {
48      printf ("Error in mpfr_max (NaN, NaN)\n");
49      exit (1);
50    }
51  /* case x=NaN */
52  mpfr_set_nan (x);
53  mpfr_set_ui (y, 0, MPFR_RNDN);
54  mpfr_min (z, x, y, MPFR_RNDN);
55  if (mpfr_cmp_ui (z, 0))
56    {
57      printf ("Error in mpfr_min (NaN, 0)\n");
58      exit (1);
59    }
60  mpfr_min (z, y, x, MPFR_RNDN);
61  if (mpfr_cmp_ui (z, 0))
62    {
63      printf ("Error in mpfr_min (0, NaN)\n");
64      exit (1);
65    }
66  mpfr_max (z, x, y, MPFR_RNDN);
67  if (mpfr_cmp_ui (z, 0))
68    {
69      printf ("Error in mpfr_max (NaN, 0)\n");
70      exit (1);
71    }
72  mpfr_max (z, y, x, MPFR_RNDN);
73  if (mpfr_cmp_ui (z, 0))
74    {
75      printf ("Error in mpfr_max (0, NaN)\n");
76      exit (1);
77    }
78  /* Case x=0+ and x=0- */
79  mpfr_set_ui (x, 0, MPFR_RNDN);
80  mpfr_set_ui (y, 0, MPFR_RNDN); MPFR_SET_NEG(y);
81  mpfr_max (z, x, y, MPFR_RNDN);
82  if (!MPFR_IS_ZERO(z) || MPFR_IS_NEG(z))
83    {
84      printf ("Error in mpfr_max (0+, 0-)\n");
85      exit (1);
86    }
87  mpfr_min (z, x, y, MPFR_RNDN);
88  if (!MPFR_IS_ZERO(z) || MPFR_IS_POS(z))
89    {
90      printf ("Error in mpfr_min (0+, 0-)\n");
91      exit (1);
92    }
93  /* Case x=0- and y=0+ */
94  mpfr_set_ui (x, 0, MPFR_RNDN); MPFR_SET_NEG(x);
95  mpfr_set_ui (y, 0, MPFR_RNDN);
96  mpfr_max (z, x, y, MPFR_RNDN);
97  if (!MPFR_IS_ZERO(z) || MPFR_IS_NEG(z))
98    {
99      printf ("Error in mpfr_max (0+, 0-)\n");
100      exit (1);
101    }
102  mpfr_min (z, x, y, MPFR_RNDN);
103  if (!MPFR_IS_ZERO(z) || MPFR_IS_POS(z))
104    {
105      printf ("Error in mpfr_min (0+, 0-)\n");
106      exit (1);
107    }
108
109  /* case x=+Inf */
110  mpfr_set_inf (x, 1);
111  mpfr_set_si (y, -12, MPFR_RNDN);
112  mpfr_min (z, x, y, MPFR_RNDN);
113  if ( mpfr_cmp_si (z, -12) )
114    {
115      printf ("Error in mpfr_min (+Inf, -12)\n");
116      exit (1);
117    }
118  mpfr_max (z, x, y, MPFR_RNDN);
119  if ( !MPFR_IS_INF(z) || MPFR_IS_NEG(z) )
120    {
121      printf ("Error in mpfr_max (+Inf, 12)\n");
122      exit (1);
123    }
124  /* case x=-Inf */
125  mpfr_set_inf (x, -1);
126  mpfr_set_ui (y, 12, MPFR_RNDN);
127  mpfr_max (z, x, y, MPFR_RNDN);
128  if ( mpfr_cmp_ui (z, 12) )
129    {
130      printf ("Error in mpfr_max (-Inf, 12)\n");
131      exit (1);
132    }
133  mpfr_min (z, x, y, MPFR_RNDN);
134  if ( !MPFR_IS_INF(z) || MPFR_IS_POS(z) )
135    {
136      printf ("Error in mpfr_min (-Inf, 12)\n");
137      exit (1);
138    }
139
140  /* case x=17 and y=42 */
141  mpfr_set_ui (x, 17, MPFR_RNDN);
142  mpfr_set_ui (y, 42, MPFR_RNDN);
143  mpfr_max (z, x, y, MPFR_RNDN);
144  if ( mpfr_cmp_ui (z, 42) )
145    {
146      printf ("Error in mpfr_max (17, 42)\n");
147      exit (1);
148    }
149  mpfr_max (z, y, x, MPFR_RNDN);
150  if ( mpfr_cmp_ui (z, 42) )
151    {
152      printf ("Error in mpfr_max (42, 17)\n");
153      exit (1);
154    }
155  mpfr_min (z, y, x, MPFR_RNDN);
156  if ( mpfr_cmp_ui (z, 17) )
157    {
158      printf ("Error in mpfr_min (42, 17)\n");
159      exit (1);
160    }
161
162  mpfr_clear (x);
163  mpfr_clear (y);
164  mpfr_clear (z);
165
166  tests_end_mpfr ();
167  return 0;
168}
169