1198090Srdivacky/* Test file for mpfr_d_div 2198090Srdivacky 3198090SrdivackyCopyright 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc. 4198090SrdivackyContributed by the Arenaire and Cacao projects, INRIA. 5198090Srdivacky 6198090SrdivackyThis file is part of the GNU MPFR Library. 7198090Srdivacky 8198090SrdivackyThe GNU MPFR Library is free software; you can redistribute it and/or modify 9198090Srdivackyit under the terms of the GNU Lesser General Public License as published by 10198090Srdivackythe Free Software Foundation; either version 3 of the License, or (at your 11198090Srdivackyoption) any later version. 12198090Srdivacky 13198090SrdivackyThe GNU MPFR Library is distributed in the hope that it will be useful, but 14198090SrdivackyWITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 15198090Srdivackyor FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 16198090SrdivackyLicense for more details. 17198090Srdivacky 18263509SdimYou should have received a copy of the GNU Lesser General Public License 19198090Srdivackyalong with the GNU MPFR Library; see the file COPYING.LESSER. If not, see 20198090Srdivackyhttp://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc., 21226890Sdim51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */ 22226890Sdim 23226890Sdim#include <stdio.h> 24226890Sdim#include <stdlib.h> 25226890Sdim#include <float.h> 26198090Srdivacky 27198090Srdivacky#include "mpfr-test.h" 28198090Srdivacky 29198090Srdivacky#if MPFR_VERSION >= MPFR_VERSION_NUM(2,4,0) 30198090Srdivacky 31198090Srdivackystatic void 32198090Srdivackycheck_nans (void) 33198090Srdivacky{ 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) == 0); 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 /* 1.0 / 0 == +inf */ 67 mpfr_set_d (x, 0.0, MPFR_RNDN); 68 mpfr_clear_flags (); 69 inexact = mpfr_d_div (y, 1.0, x, MPFR_RNDN); 70 MPFR_ASSERTN (inexact == 0); 71 MPFR_ASSERTN (__gmpfr_flags == 0); 72 MPFR_ASSERTN (mpfr_inf_p (y)); 73 MPFR_ASSERTN (MPFR_IS_POS (y)); 74 75 mpfr_clear (x); 76 mpfr_clear (y); 77} 78 79#define TEST_FUNCTION mpfr_d_sub 80#define DOUBLE_ARG1 81#define RAND_FUNCTION(x) mpfr_random2(x, MPFR_LIMB_SIZE (x), 1, RANDS) 82#include "tgeneric.c" 83 84int 85main (void) 86{ 87 mpfr_t x, y, z; 88 double d; 89 int inexact; 90 91 tests_start_mpfr (); 92 93 /* check with enough precision */ 94 mpfr_init2 (x, IEEE_DBL_MANT_DIG); 95 mpfr_init2 (y, IEEE_DBL_MANT_DIG); 96 mpfr_init2 (z, IEEE_DBL_MANT_DIG); 97 98 mpfr_set_str (y, "4096", 10, MPFR_RNDN); 99 d = 0.125; 100 mpfr_clear_flags (); 101 inexact = mpfr_d_div (x, d, y, MPFR_RNDN); 102 if (inexact != 0) 103 { 104 printf ("Inexact flag error in mpfr_d_div\n"); 105 exit (1); 106 } 107 mpfr_set_str (z, " 0.000030517578125", 10, MPFR_RNDN); 108 if (mpfr_cmp (z, x)) 109 { 110 printf ("Error in mpfr_d_div ("); 111 mpfr_out_str (stdout, 10, 0, y, MPFR_RNDN); 112 printf (" + %.20g)\nexpected ", d); 113 mpfr_out_str (stdout, 10, 0, z, MPFR_RNDN); 114 printf ("\ngot "); 115 mpfr_out_str (stdout, 10, 0, x, MPFR_RNDN); 116 printf ("\n"); 117 exit (1); 118 } 119 mpfr_clears (x, y, z, (mpfr_ptr) 0); 120 121 check_nans (); 122 123 test_generic (2, 1000, 100); 124 125 tests_end_mpfr (); 126 return 0; 127} 128 129#else 130 131int 132main (void) 133{ 134 printf ("Warning! Test disabled for this MPFR version.\n"); 135 return 0; 136} 137 138#endif 139