1 /* Test file for mpfr_add_si, mpfr_sub_si, mpfr_si_sub, mpfr_mul_si, 2 mpfr_div_si, mpfr_si_div 3 4 Copyright 2004, 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc. 5 Contributed by the Arenaire and Cacao projects, INRIA. 6 7 This file is part of the GNU MPFR Library. 8 9 The GNU MPFR Library is free software; you can redistribute it and/or modify 10 it under the terms of the GNU Lesser General Public License as published by 11 the Free Software Foundation; either version 3 of the License, or (at your 12 option) any later version. 13 14 The GNU MPFR Library is distributed in the hope that it will be useful, but 15 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 16 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 17 License for more details. 18 19 You should have received a copy of the GNU Lesser General Public License 20 along with the GNU MPFR Library; see the file COPYING.LESSER. If not, see 21 http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc., 22 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */ 23 24 #include <stdio.h> 25 #include <stdlib.h> 26 27 #include "mpfr-test.h" 28 29 #define ERROR1(s, i, z, exp) \ 30 {\ 31 printf("Error for "s" and i=%d\n", i);\ 32 printf("Expected %s\n", exp);\ 33 printf("Got "); mpfr_out_str (stdout, 16, 0, z, MPFR_RNDN);\ 34 putchar ('\n');\ 35 exit(1);\ 36 } 37 38 const struct { 39 const char * op1; 40 long int op2; 41 const char * res_add; 42 const char * res_sub; 43 const char * res_mul; 44 const char * res_div; 45 } tab[] = { 46 {"10", 0x1, "11", "0F", "10", "10"}, 47 {"1", -1, "0", "2", "-1", "-1"}, 48 {"17.42", -0x17, "0.42", "2E.42", "-216.ee", "-1.02de9bd37a6f4"}, 49 {"-1024.0", -0x16, "-103A", "-100E", "16318", "bb.d1745d1745d0"} 50 }; 51 52 static void 53 check_invert (void) 54 { 55 mpfr_t x; 56 mpfr_init2 (x, MPFR_PREC_MIN); 57 58 mpfr_set_ui (x, 0xC, MPFR_RNDN); 59 mpfr_si_sub (x, -1, x, MPFR_RNDD); /* -0001 - 1100 = - 1101 --> -1 0000 */ 60 if (mpfr_cmp_si (x, -0x10) ) 61 { 62 printf ("Special rounding error\n"); 63 exit (1); 64 } 65 mpfr_clear (x); 66 } 67 68 #define TEST_FUNCTION mpfr_add_si 69 #define TEST_FUNCTION_NAME "mpfr_add_si" 70 #define INTEGER_TYPE long 71 #define RAND_FUNCTION(x) mpfr_random2(x, MPFR_LIMB_SIZE (x), 1, RANDS) 72 #define test_generic_ui test_generic_add_si 73 #include "tgeneric_ui.c" 74 75 #define TEST_FUNCTION mpfr_sub_si 76 #define TEST_FUNCTION_NAME "mpfr_sub_si" 77 #define INTEGER_TYPE long 78 #define RAND_FUNCTION(x) mpfr_random2(x, MPFR_LIMB_SIZE (x), 1, RANDS) 79 #define test_generic_ui test_generic_sub_si 80 #include "tgeneric_ui.c" 81 82 #define TEST_FUNCTION mpfr_mul_si 83 #define TEST_FUNCTION_NAME "mpfr_mul_si" 84 #define INTEGER_TYPE long 85 #define RAND_FUNCTION(x) mpfr_random2(x, MPFR_LIMB_SIZE (x), 1, RANDS) 86 #define test_generic_ui test_generic_mul_si 87 #include "tgeneric_ui.c" 88 89 #define TEST_FUNCTION mpfr_div_si 90 #define TEST_FUNCTION_NAME "mpfr_div_si" 91 #define INTEGER_TYPE long 92 #define RAND_FUNCTION(x) mpfr_random2(x, MPFR_LIMB_SIZE (x), 1, RANDS) 93 #define test_generic_ui test_generic_div_si 94 #include "tgeneric_ui.c" 95 96 97 int 98 main (int argc, char *argv[]) 99 { 100 mpfr_t x, z; 101 int y; 102 int i; 103 104 tests_start_mpfr (); 105 mpfr_inits2 (53, x, z, (mpfr_ptr) 0); 106 for(i = 0 ; i < numberof (tab) ; i++) 107 { 108 mpfr_set_str (x, tab[i].op1, 16, MPFR_RNDN); 109 y = tab[i].op2; 110 mpfr_add_si (z, x, y, MPFR_RNDZ); 111 if (mpfr_cmp_str (z, tab[i].res_add, 16, MPFR_RNDN)) 112 ERROR1("add_si", i, z, tab[i].res_add); 113 mpfr_sub_si (z, x, y, MPFR_RNDZ); 114 if (mpfr_cmp_str (z, tab[i].res_sub, 16, MPFR_RNDN)) 115 ERROR1("sub_si", i, z, tab[i].res_sub); 116 mpfr_si_sub (z, y, x, MPFR_RNDZ); 117 mpfr_neg (z, z, MPFR_RNDZ); 118 if (mpfr_cmp_str (z, tab[i].res_sub, 16, MPFR_RNDN)) 119 ERROR1("si_sub", i, z, tab[i].res_sub); 120 mpfr_mul_si (z, x, y, MPFR_RNDZ); 121 if (mpfr_cmp_str (z, tab[i].res_mul, 16, MPFR_RNDN)) 122 ERROR1("mul_si", i, z, tab[i].res_mul); 123 mpfr_div_si (z, x, y, MPFR_RNDZ); 124 if (mpfr_cmp_str (z, tab[i].res_div, 16, MPFR_RNDN)) 125 ERROR1("div_si", i, z, tab[i].res_div); 126 } 127 mpfr_set_str1 (x, "1"); 128 mpfr_si_div (z, 1024, x, MPFR_RNDN); 129 if (mpfr_cmp_str1 (z, "1024")) 130 ERROR1("si_div", i, z, "1024"); 131 mpfr_si_div (z, -1024, x, MPFR_RNDN); 132 if (mpfr_cmp_str1 (z, "-1024")) 133 ERROR1("si_div", i, z, "-1024"); 134 135 mpfr_clears (x, z, (mpfr_ptr) 0); 136 137 check_invert (); 138 139 test_generic_add_si (2, 200, 17); 140 test_generic_sub_si (2, 200, 17); 141 test_generic_mul_si (2, 200, 17); 142 test_generic_div_si (2, 200, 17); 143 144 tests_end_mpfr (); 145 return 0; 146 } 147