xref: /netbsd-src/external/lgpl3/mpfr/dist/tests/td_div.c (revision 4d5abbe83f525258eb479e5fca29f25cb943f379)
1 /* Test file for mpfr_d_div
2 
3 Copyright 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 #include <float.h>
26 
27 #include "mpfr-test.h"
28 
29 #if MPFR_VERSION >= MPFR_VERSION_NUM(2,4,0)
30 
31 static void
32 check_nans (void)
33 {
34   mpfr_t  x, y;
35   int inexact;
36 
37   mpfr_init2 (x, 123);
38   mpfr_init2 (y, 123);
39 
40   /* 1.0 / nan is nan */
41   mpfr_set_nan (x);
42   mpfr_clear_flags ();
43   inexact = mpfr_d_div (y, 1.0, x, MPFR_RNDN);
44   MPFR_ASSERTN (inexact == 0);
45   MPFR_ASSERTN (__gmpfr_flags == MPFR_FLAGS_NAN);
46   MPFR_ASSERTN (mpfr_nan_p (y));
47 
48   /* 1.0 / +inf == +0 */
49   mpfr_set_inf (x, 1);
50   mpfr_clear_flags ();
51   inexact = mpfr_d_div (y, 1.0, x, MPFR_RNDN);
52   MPFR_ASSERTN (inexact == 0);
53   MPFR_ASSERTN (__gmpfr_flags == 0);
54   MPFR_ASSERTN (mpfr_zero_p (y));
55   MPFR_ASSERTN (MPFR_IS_POS (y));
56 
57   /* 1.0 / -inf == -0 */
58   mpfr_set_inf (x, -1);
59   mpfr_clear_flags ();
60   inexact = mpfr_d_div (y, 1.0, x, MPFR_RNDN);
61   MPFR_ASSERTN (inexact == 0);
62   MPFR_ASSERTN (__gmpfr_flags == 0);
63   MPFR_ASSERTN (mpfr_zero_p (y));
64   MPFR_ASSERTN (MPFR_IS_NEG (y));
65 
66 #if !defined(MPFR_ERRDIVZERO)
67 
68   /* 1.0 / 0 == +inf */
69   mpfr_set_ui (x, 0, MPFR_RNDN);
70   mpfr_clear_flags ();
71   inexact = mpfr_d_div (y, 1.0, x, MPFR_RNDN);
72   MPFR_ASSERTN (inexact == 0);
73   MPFR_ASSERTN (__gmpfr_flags == MPFR_FLAGS_DIVBY0);
74   MPFR_ASSERTN (mpfr_inf_p (y));
75   MPFR_ASSERTN (MPFR_IS_POS (y));
76 
77   /* -1.0 / 0 == -inf */
78   mpfr_set_ui (x, 0, MPFR_RNDN);
79   mpfr_clear_flags ();
80   inexact = mpfr_d_div (y, -1.0, x, MPFR_RNDN);
81   MPFR_ASSERTN (inexact == 0);
82   MPFR_ASSERTN (__gmpfr_flags == MPFR_FLAGS_DIVBY0);
83   MPFR_ASSERTN (mpfr_inf_p (y));
84   MPFR_ASSERTN (MPFR_IS_NEG (y));
85 
86   /* 1.0 / -0 == -inf */
87   mpfr_set_ui (x, 0, MPFR_RNDN);
88   mpfr_neg (x, x, MPFR_RNDN);
89   mpfr_clear_flags ();
90   inexact = mpfr_d_div (y, 1.0, x, MPFR_RNDN);
91   MPFR_ASSERTN (inexact == 0);
92   MPFR_ASSERTN (__gmpfr_flags == MPFR_FLAGS_DIVBY0);
93   MPFR_ASSERTN (mpfr_inf_p (y));
94   MPFR_ASSERTN (MPFR_IS_NEG (y));
95 
96   /* -1.0 / -0 == +inf */
97   mpfr_set_ui (x, 0, MPFR_RNDN);
98   mpfr_neg (x, x, MPFR_RNDN);
99   mpfr_clear_flags ();
100   inexact = mpfr_d_div (y, -1.0, x, MPFR_RNDN);
101   MPFR_ASSERTN (inexact == 0);
102   MPFR_ASSERTN (__gmpfr_flags == MPFR_FLAGS_DIVBY0);
103   MPFR_ASSERTN (mpfr_inf_p (y));
104   MPFR_ASSERTN (MPFR_IS_POS (y));
105 
106   /* +inf / 0 == +inf */
107   mpfr_set_ui (x, 0, MPFR_RNDN);
108   mpfr_clear_flags ();
109   inexact = mpfr_d_div (y, DBL_POS_INF, x, MPFR_RNDN);
110   MPFR_ASSERTN (inexact == 0);
111   MPFR_ASSERTN (__gmpfr_flags == 0);
112   MPFR_ASSERTN (mpfr_inf_p (y));
113   MPFR_ASSERTN (MPFR_IS_POS (y));
114 
115   /* -inf / 0 == -inf */
116   mpfr_set_ui (x, 0, MPFR_RNDN);
117   mpfr_clear_flags ();
118   inexact = mpfr_d_div (y, DBL_NEG_INF, x, MPFR_RNDN);
119   MPFR_ASSERTN (inexact == 0);
120   MPFR_ASSERTN (__gmpfr_flags == 0);
121   MPFR_ASSERTN (mpfr_inf_p (y));
122   MPFR_ASSERTN (MPFR_IS_NEG (y));
123 
124   /* +inf / -0 == -inf */
125   mpfr_set_ui (x, 0, MPFR_RNDN);
126   mpfr_neg (x, x, MPFR_RNDN);
127   mpfr_clear_flags ();
128   inexact = mpfr_d_div (y, DBL_POS_INF, x, MPFR_RNDN);
129   MPFR_ASSERTN (inexact == 0);
130   MPFR_ASSERTN (__gmpfr_flags == 0);
131   MPFR_ASSERTN (mpfr_inf_p (y));
132   MPFR_ASSERTN (MPFR_IS_NEG (y));
133 
134   /* -inf / -0 == +inf */
135   mpfr_set_ui (x, 0, MPFR_RNDN);
136   mpfr_neg (x, x, MPFR_RNDN);
137   mpfr_clear_flags ();
138   inexact = mpfr_d_div (y, DBL_NEG_INF, x, MPFR_RNDN);
139   MPFR_ASSERTN (inexact == 0);
140   MPFR_ASSERTN (__gmpfr_flags == 0);
141   MPFR_ASSERTN (mpfr_inf_p (y));
142   MPFR_ASSERTN (MPFR_IS_POS (y));
143 
144 #endif
145 
146   mpfr_clear (x);
147   mpfr_clear (y);
148 }
149 
150 #define TEST_FUNCTION mpfr_d_sub
151 #define DOUBLE_ARG1
152 #define RAND_FUNCTION(x) mpfr_random2(x, MPFR_LIMB_SIZE (x), 1, RANDS)
153 #include "tgeneric.c"
154 
155 int
156 main (void)
157 {
158   mpfr_t x, y, z;
159   double d;
160   int inexact;
161 
162   tests_start_mpfr ();
163 
164   /* check with enough precision */
165   mpfr_init2 (x, IEEE_DBL_MANT_DIG);
166   mpfr_init2 (y, IEEE_DBL_MANT_DIG);
167   mpfr_init2 (z, IEEE_DBL_MANT_DIG);
168 
169   mpfr_set_str (y, "4096", 10, MPFR_RNDN);
170   d = 0.125;
171   mpfr_clear_flags ();
172   inexact = mpfr_d_div (x, d, y, MPFR_RNDN);
173   if (inexact != 0)
174     {
175       printf ("Inexact flag error in mpfr_d_div\n");
176       exit (1);
177     }
178   mpfr_set_str (z, " 0.000030517578125", 10, MPFR_RNDN);
179   if (mpfr_cmp (z, x))
180     {
181       printf ("Error in mpfr_d_div (");
182       mpfr_out_str (stdout, 10, 0, y, MPFR_RNDN);
183       printf (" + %.20g)\nexpected ", d);
184       mpfr_out_str (stdout, 10, 0, z, MPFR_RNDN);
185       printf ("\ngot     ");
186       mpfr_out_str (stdout, 10, 0, x, MPFR_RNDN);
187       printf ("\n");
188       exit (1);
189     }
190   mpfr_clears (x, y, z, (mpfr_ptr) 0);
191 
192   check_nans ();
193 
194   test_generic (2, 1000, 100);
195 
196   tests_end_mpfr ();
197   return 0;
198 }
199 
200 #else
201 
202 int
203 main (void)
204 {
205   printf ("Warning! Test disabled for this MPFR version.\n");
206   return 0;
207 }
208 
209 #endif
210