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