1/* Test file for mpfr_cmp_d.
2
3Copyright 1999, 2001-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#include "ieee_floats.h"
25
26int
27main (void)
28{
29  mpfr_t x;
30  mpfr_exp_t emin;
31
32  tests_start_mpfr ();
33  emin = mpfr_get_emin ();
34
35  mpfr_init2 (x, IEEE_DBL_MANT_DIG);
36
37  mpfr_set_d (x, 2.34763465, MPFR_RNDN);
38  if (mpfr_cmp_d (x, 2.34763465) != 0)
39    {
40      printf ("Error in mpfr_cmp_d 2.34763465 and ");
41      mpfr_out_str (stdout, 10, 0, x, MPFR_RNDN); putchar('\n');
42      exit (1);
43    }
44  if (mpfr_cmp_d (x, 2.345) <= 0)
45    {
46      printf ("Error in mpfr_cmp_d 2.345 and ");
47      mpfr_out_str (stdout, 10, 0, x, MPFR_RNDN); putchar('\n');
48      exit (1);
49    }
50  if (mpfr_cmp_d (x, 2.4) >= 0)
51    {
52      printf ("Error in mpfr_cmp_d 2.4 and ");
53      mpfr_out_str (stdout, 10, 0, x, MPFR_RNDN); putchar('\n');
54      exit (1);
55    }
56
57  mpfr_set_ui (x, 0, MPFR_RNDZ);
58  mpfr_neg (x, x, MPFR_RNDZ);
59  if (mpfr_cmp_d (x, 0.0))
60    {
61      printf ("Error in mpfr_cmp_d 0.0 and ");
62      mpfr_out_str (stdout, 10, 0, x, MPFR_RNDN); putchar('\n');
63      exit (1);
64    }
65
66  mpfr_set_ui (x, 0, MPFR_RNDN);
67  mpfr_ui_div (x, 1, x, MPFR_RNDU);
68  if (mpfr_cmp_d (x, 0.0) == 0)
69    {
70      printf ("Error in mpfr_cmp_d (Inf, 0)\n");
71      exit (1);
72    }
73
74  /* Test in reduced exponent range. */
75  set_emin (1);
76  mpfr_set_ui (x, 1, MPFR_RNDN);
77  if (mpfr_cmp_d (x, 0.9) <= 0)
78    {
79      printf ("Error in reduced exponent range.\n");
80      exit (1);
81    }
82  set_emin (emin);
83
84#if !defined(MPFR_ERRDIVZERO)
85  /* Check NAN */
86  {
87    int c;
88
89    mpfr_clear_flags ();
90    c = mpfr_cmp_d (x, MPFR_DBL_NAN);
91    if (c != 0 || __gmpfr_flags != MPFR_FLAGS_ERANGE)
92      {
93        printf ("ERROR for NAN (1)\n");
94        printf ("Expected 0, got %d\n", c);
95        printf ("Expected flags:");
96        flags_out (MPFR_FLAGS_ERANGE);
97        printf ("Got flags:     ");
98        flags_out (__gmpfr_flags);
99#ifdef MPFR_NANISNAN
100        printf ("The reason is that NAN == NAN. Please look at the configure "
101                "output\nand Section \"In case of problem\" of the INSTALL "
102                "file.\n");
103#endif
104        exit (1);
105      }
106
107    mpfr_set_nan (x);
108    mpfr_clear_flags ();
109    c = mpfr_cmp_d (x, 2.0);
110    if (c != 0 || __gmpfr_flags != MPFR_FLAGS_ERANGE)
111      {
112        printf ("ERROR for NAN (2)\n");
113        printf ("Expected 0, got %d\n", c);
114        printf ("Expected flags:");
115        flags_out (MPFR_FLAGS_ERANGE);
116        printf ("Got flags:     ");
117        flags_out (__gmpfr_flags);
118#ifdef MPFR_NANISNAN
119        printf ("The reason is that NAN == NAN. Please look at the configure "
120                "output\nand Section \"In case of problem\" of the INSTALL "
121                "file.\n");
122#endif
123        exit (1);
124      }
125  }
126#endif  /* MPFR_ERRDIVZERO */
127
128  mpfr_clear (x);
129
130  tests_end_mpfr ();
131  return 0;
132}
133