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