xref: /netbsd-src/external/lgpl3/mpfr/dist/tests/tdiv_d.c (revision ba125506a622fe649968631a56eba5d42ff57863)
1 /* Test file for mpfr_div_d
2 
3 Copyright 2007-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 
27 static void
check_nans(void)28 check_nans (void)
29 {
30   mpfr_t  x, y;
31   int inexact;
32 
33   mpfr_init2 (x, 123);
34   mpfr_init2 (y, 123);
35 
36   /* nan / 1.0 is nan */
37   mpfr_set_nan (x);
38   mpfr_clear_flags ();
39   inexact = mpfr_div_d (y, x, 1.0, MPFR_RNDN);
40   MPFR_ASSERTN (inexact == 0);
41   MPFR_ASSERTN (__gmpfr_flags == MPFR_FLAGS_NAN);
42   MPFR_ASSERTN (mpfr_nan_p (y));
43 
44   /* +inf / 1.0 == +inf */
45   mpfr_set_inf (x, 1);
46   mpfr_clear_flags ();
47   inexact = mpfr_div_d (y, x, 1.0, MPFR_RNDN);
48   MPFR_ASSERTN (inexact == 0);
49   MPFR_ASSERTN (__gmpfr_flags == 0);
50   MPFR_ASSERTN (mpfr_inf_p (y));
51   MPFR_ASSERTN (MPFR_IS_POS (y));
52 
53   /* -inf / 1.0 == -inf */
54   mpfr_set_inf (x, -1);
55   mpfr_clear_flags ();
56   inexact = mpfr_div_d (y, x, 1.0, MPFR_RNDN);
57   MPFR_ASSERTN (inexact == 0);
58   MPFR_ASSERTN (__gmpfr_flags == 0);
59   MPFR_ASSERTN (mpfr_inf_p (y));
60   MPFR_ASSERTN (MPFR_IS_NEG (y));
61 
62   /* 0.0 / 0.0 is nan */
63   mpfr_set_ui (x, 0, MPFR_RNDN);
64   mpfr_clear_flags ();
65   inexact = mpfr_div_d (y, x, 0.0, MPFR_RNDN);
66   MPFR_ASSERTN (inexact == 0);
67   MPFR_ASSERTN (__gmpfr_flags == MPFR_FLAGS_NAN);
68   MPFR_ASSERTN (mpfr_nan_p (y));
69 
70   /* 1.0 / 0.0 == +inf */
71   mpfr_set_ui (x, 1, MPFR_RNDN);
72   mpfr_clear_flags ();
73   inexact = mpfr_div_d (y, x, 0.0, MPFR_RNDN);
74   MPFR_ASSERTN (inexact == 0);
75   MPFR_ASSERTN (__gmpfr_flags == MPFR_FLAGS_DIVBY0);
76   MPFR_ASSERTN (mpfr_inf_p (y));
77   MPFR_ASSERTN (MPFR_IS_POS (y));
78 
79   /* -1.0 / 0.0 == -inf */
80   mpfr_set_si (x, -1, MPFR_RNDN);
81   mpfr_clear_flags ();
82   inexact = mpfr_div_d (y, x, 0.0, MPFR_RNDN);
83   MPFR_ASSERTN (inexact == 0);
84   MPFR_ASSERTN (__gmpfr_flags == MPFR_FLAGS_DIVBY0);
85   MPFR_ASSERTN (mpfr_inf_p (y));
86   MPFR_ASSERTN (MPFR_IS_NEG (y));
87 
88   mpfr_clear (x);
89   mpfr_clear (y);
90 }
91 
92 #define TEST_FUNCTION mpfr_div_d
93 #define DOUBLE_ARG2
94 #define RAND_FUNCTION(x) mpfr_random2(x, MPFR_LIMB_SIZE (x), 1, RANDS)
95 #include "tgeneric.c"
96 
97 int
main(void)98 main (void)
99 {
100   mpfr_t x, y, z;
101   double d;
102   int inexact;
103 
104   tests_start_mpfr ();
105 
106   /* check with enough precision */
107   mpfr_init2 (x, IEEE_DBL_MANT_DIG);
108   mpfr_init2 (y, IEEE_DBL_MANT_DIG);
109   mpfr_init2 (z, IEEE_DBL_MANT_DIG);
110 
111   mpfr_set_str (y, "4096", 10, MPFR_RNDN);
112   d = 0.125;
113   mpfr_clear_flags ();
114   inexact = mpfr_div_d (x, y, d, MPFR_RNDN);
115   if (inexact != 0)
116     {
117       printf ("Inexact flag error in mpfr_div_d\n");
118       exit (1);
119     }
120   mpfr_set_str (z, "32768", 10, MPFR_RNDN);
121   if (mpfr_cmp (z, x))
122     {
123       printf ("Error in mpfr_div_d (");
124       mpfr_out_str (stdout, 10, 0, y, MPFR_RNDN);
125       printf (" + %.20g)\nexpected ", d);
126       mpfr_out_str (stdout, 10, 0, z, MPFR_RNDN);
127       printf ("\ngot     ");
128       mpfr_out_str (stdout, 10, 0, x, MPFR_RNDN);
129       printf ("\n");
130       exit (1);
131     }
132   mpfr_clears (x, y, z, (mpfr_ptr) 0);
133 
134   check_nans ();
135 
136   test_generic (MPFR_PREC_MIN, 1000, 100);
137 
138   tests_end_mpfr ();
139   return 0;
140 }
141