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