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