1 /* Test file for mpfr_expm1. 2 3 Copyright 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc. 4 Contributed by the Arenaire and Cacao 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 26 #include "mpfr-test.h" 27 28 #ifdef CHECK_EXTERNAL 29 static int 30 test_expm1 (mpfr_ptr a, mpfr_srcptr b, mpfr_rnd_t rnd_mode) 31 { 32 int res; 33 int ok = rnd_mode == MPFR_RNDN && mpfr_number_p (b) && mpfr_get_prec (a)>=53; 34 if (ok) 35 { 36 mpfr_print_raw (b); 37 } 38 res = mpfr_expm1 (a, b, rnd_mode); 39 if (ok) 40 { 41 printf (" "); 42 mpfr_print_raw (a); 43 printf ("\n"); 44 } 45 return res; 46 } 47 #else 48 #define test_expm1 mpfr_expm1 49 #endif 50 51 #define TEST_FUNCTION test_expm1 52 #define TEST_RANDOM_EMIN -36 53 #define TEST_RANDOM_EMAX 36 54 #include "tgeneric.c" 55 56 static void 57 special (void) 58 { 59 mpfr_t x, y; 60 int i; 61 62 mpfr_init (x); 63 mpfr_init (y); 64 65 mpfr_set_nan (x); 66 test_expm1 (y, x, MPFR_RNDN); 67 if (!mpfr_nan_p (y)) 68 { 69 printf ("Error for expm1(NaN)\n"); 70 exit (1); 71 } 72 73 mpfr_set_inf (x, 1); 74 test_expm1 (y, x, MPFR_RNDN); 75 if (!mpfr_inf_p (y) || mpfr_sgn (y) < 0) 76 { 77 printf ("Error for expm1(+Inf)\n"); 78 exit (1); 79 } 80 81 mpfr_set_inf (x, -1); 82 test_expm1 (y, x, MPFR_RNDN); 83 if (mpfr_cmp_si (y, -1)) 84 { 85 printf ("Error for expm1(-Inf)\n"); 86 exit (1); 87 } 88 89 mpfr_set_ui (x, 0, MPFR_RNDN); 90 test_expm1 (y, x, MPFR_RNDN); 91 if (mpfr_cmp_ui (y, 0) || mpfr_sgn (y) < 0) 92 { 93 printf ("Error for expm1(+0)\n"); 94 exit (1); 95 } 96 97 mpfr_neg (x, x, MPFR_RNDN); 98 test_expm1 (y, x, MPFR_RNDN); 99 if (mpfr_cmp_ui (y, 0) || mpfr_sgn (y) > 0) 100 { 101 printf ("Error for expm1(-0)\n"); 102 exit (1); 103 } 104 105 /* Check overflow of expm1(x) */ 106 mpfr_clear_flags (); 107 mpfr_set_str_binary (x, "1.1E1000000000"); 108 i = test_expm1 (x, x, MPFR_RNDN); 109 MPFR_ASSERTN (MPFR_IS_INF (x) && MPFR_SIGN (x) > 0); 110 MPFR_ASSERTN (mpfr_overflow_p ()); 111 MPFR_ASSERTN (i == 1); 112 113 mpfr_clear_flags (); 114 mpfr_set_str_binary (x, "1.1E1000000000"); 115 i = test_expm1 (x, x, MPFR_RNDU); 116 MPFR_ASSERTN (MPFR_IS_INF (x) && MPFR_SIGN (x) > 0); 117 MPFR_ASSERTN (mpfr_overflow_p ()); 118 MPFR_ASSERTN (i == 1); 119 120 mpfr_clear_flags (); 121 mpfr_set_str_binary (x, "1.1E1000000000"); 122 i = test_expm1 (x, x, MPFR_RNDD); 123 MPFR_ASSERTN (!MPFR_IS_INF (x) && MPFR_SIGN (x) > 0); 124 MPFR_ASSERTN (mpfr_overflow_p ()); 125 MPFR_ASSERTN (i == -1); 126 127 /* Check internal underflow of expm1 (x) */ 128 mpfr_set_prec (x, 2); 129 mpfr_clear_flags (); 130 mpfr_set_str_binary (x, "-1.1E1000000000"); 131 i = test_expm1 (x, x, MPFR_RNDN); 132 MPFR_ASSERTN (mpfr_cmp_si (x, -1) == 0); 133 MPFR_ASSERTN (!mpfr_overflow_p () && !mpfr_underflow_p ()); 134 MPFR_ASSERTN (i == -1); 135 136 mpfr_set_str_binary (x, "-1.1E1000000000"); 137 i = test_expm1 (x, x, MPFR_RNDD); 138 MPFR_ASSERTN (mpfr_cmp_si (x, -1) == 0); 139 MPFR_ASSERTN (!mpfr_overflow_p () && !mpfr_underflow_p ()); 140 MPFR_ASSERTN (i == -1); 141 142 mpfr_set_str_binary (x, "-1.1E1000000000"); 143 i = test_expm1 (x, x, MPFR_RNDZ); 144 MPFR_ASSERTN (mpfr_cmp_str (x, "-0.11", 2, MPFR_RNDN) == 0); 145 MPFR_ASSERTN (!mpfr_overflow_p () && !mpfr_underflow_p ()); 146 MPFR_ASSERTN (i == 1); 147 148 mpfr_set_str_binary (x, "-1.1E1000000000"); 149 i = test_expm1 (x, x, MPFR_RNDU); 150 MPFR_ASSERTN (mpfr_cmp_str (x, "-0.11", 2, MPFR_RNDN) == 0); 151 MPFR_ASSERTN (!mpfr_overflow_p () && !mpfr_underflow_p ()); 152 MPFR_ASSERTN (i == 1); 153 154 mpfr_clear (x); 155 mpfr_clear (y); 156 } 157 158 int 159 main (int argc, char *argv[]) 160 { 161 tests_start_mpfr (); 162 163 special (); 164 165 test_generic (2, 100, 100); 166 167 data_check ("data/expm1", mpfr_expm1, "mpfr_expm1"); 168 bad_cases (mpfr_expm1, mpfr_log1p, "mpfr_expm1", 256, -256, 255, 169 4, 128, 800, 40); 170 171 tests_end_mpfr (); 172 return 0; 173 } 174