1/* Test file for mpfr_mul_d 2 3Copyright 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc. 4Contributed by the Arenaire and Cacao projects, INRIA. 5 6This file is part of the GNU MPFR Library. 7 8The GNU MPFR Library is free software; you can redistribute it and/or modify 9it under the terms of the GNU Lesser General Public License as published by 10the Free Software Foundation; either version 3 of the License, or (at your 11option) any later version. 12 13The GNU MPFR Library is distributed in the hope that it will be useful, but 14WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 15or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 16License for more details. 17 18You should have received a copy of the GNU Lesser General Public License 19along with the GNU MPFR Library; see the file COPYING.LESSER. If not, see 20http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc., 2151 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 31static void 32check_nans (void) 33{ 34 mpfr_t x, y; 35 int inexact; 36 37 mpfr_init2 (x, 123); 38 mpfr_init2 (y, 123); 39 40 /* nan * 1.0 is nan */ 41 mpfr_set_nan (x); 42 mpfr_clear_flags(); 43 inexact = mpfr_mul_d (y, x, 1.0, 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 /* +inf * 1.0 == +inf */ 49 mpfr_set_inf (x, 1); 50 mpfr_clear_flags(); 51 inexact = mpfr_mul_d (y, x, 1.0, MPFR_RNDN); 52 MPFR_ASSERTN (inexact == 0); 53 MPFR_ASSERTN (__gmpfr_flags == 0); 54 MPFR_ASSERTN (mpfr_inf_p (y)); 55 MPFR_ASSERTN (MPFR_IS_POS (y)); 56 57 /* +inf * 0.0 is nan */ 58 mpfr_clear_flags(); 59 inexact = mpfr_mul_d (y, x, 0.0, MPFR_RNDN); 60 MPFR_ASSERTN (inexact == 0); 61 MPFR_ASSERTN ((__gmpfr_flags ^ MPFR_FLAGS_NAN) == 0); 62 MPFR_ASSERTN (mpfr_nan_p (y)); 63 64 /* -inf * 1.0 == -inf */ 65 mpfr_set_inf (x, -1); 66 mpfr_clear_flags(); 67 inexact = mpfr_mul_d (y, x, 1.0, MPFR_RNDN); 68 MPFR_ASSERTN (inexact == 0); 69 MPFR_ASSERTN (__gmpfr_flags == 0); 70 MPFR_ASSERTN (mpfr_inf_p (y)); 71 MPFR_ASSERTN (MPFR_IS_NEG (y)); 72 73 mpfr_clear (x); 74 mpfr_clear (y); 75} 76 77#define TEST_FUNCTION mpfr_mul_d 78#define DOUBLE_ARG2 79#define RAND_FUNCTION(x) mpfr_random2(x, MPFR_LIMB_SIZE (x), 1, RANDS) 80#include "tgeneric.c" 81 82int 83main (void) 84{ 85 mpfr_t x, y, z; 86 double d; 87 int inexact; 88 89 tests_start_mpfr (); 90 91 /* check with enough precision */ 92 mpfr_init2 (x, IEEE_DBL_MANT_DIG); 93 mpfr_init2 (y, IEEE_DBL_MANT_DIG); 94 mpfr_init2 (z, IEEE_DBL_MANT_DIG); 95 96 mpfr_set_str (y, "4096", 10, MPFR_RNDN); 97 d = 0.125; 98 mpfr_clear_flags (); 99 inexact = mpfr_mul_d (x, y, d, MPFR_RNDN); 100 if (inexact != 0) 101 { 102 printf ("Inexact flag error in mpfr_mul_d\n"); 103 exit (1); 104 } 105 mpfr_set_str (z, "512", 10, MPFR_RNDN); 106 if (mpfr_cmp (z, x)) 107 { 108 printf ("Error in mpfr_mul_d ("); 109 mpfr_out_str (stdout, 10, 0, y, MPFR_RNDN); 110 printf (" + %.20g)\nexpected ", d); 111 mpfr_out_str (stdout, 10, 0, z, MPFR_RNDN); 112 printf ("\ngot "); 113 mpfr_out_str (stdout, 10, 0, x, MPFR_RNDN); 114 printf ("\n"); 115 exit (1); 116 } 117 mpfr_clears (x, y, z, (mpfr_ptr) 0); 118 119 check_nans (); 120 121 test_generic (2, 1000, 100); 122 123 tests_end_mpfr (); 124 return 0; 125} 126 127#else 128 129int 130main (void) 131{ 132 printf ("Warning! Test disabled for this MPFR version.\n"); 133 return 0; 134} 135 136#endif 137