1/* Test file for mpfr_root. 2 3Copyright 2005, 2006, 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 26#include "mpfr-test.h" 27 28static void 29special (void) 30{ 31 mpfr_t x, y; 32 int i; 33 34 mpfr_init (x); 35 mpfr_init (y); 36 37 /* root(NaN) = NaN */ 38 mpfr_set_nan (x); 39 mpfr_root (y, x, 17, MPFR_RNDN); 40 if (!mpfr_nan_p (y)) 41 { 42 printf ("Error: root(NaN,17) <> NaN\n"); 43 exit (1); 44 } 45 46 /* root(+Inf) = +Inf */ 47 mpfr_set_inf (x, 1); 48 mpfr_root (y, x, 42, MPFR_RNDN); 49 if (!mpfr_inf_p (y) || mpfr_sgn (y) < 0) 50 { 51 printf ("Error: root(+Inf,42) <> +Inf\n"); 52 exit (1); 53 } 54 55 /* root(-Inf, 17) = -Inf */ 56 mpfr_set_inf (x, -1); 57 mpfr_root (y, x, 17, MPFR_RNDN); 58 if (!mpfr_inf_p (y) || mpfr_sgn (y) > 0) 59 { 60 printf ("Error: root(-Inf,17) <> -Inf\n"); 61 exit (1); 62 } 63 /* root(-Inf, 42) = NaN */ 64 mpfr_set_inf (x, -1); 65 mpfr_root (y, x, 42, MPFR_RNDN); 66 if (!mpfr_nan_p (y)) 67 { 68 printf ("Error: root(-Inf,42) <> -Inf\n"); 69 exit (1); 70 } 71 72 /* root(+/-0) = +/-0 */ 73 mpfr_set_ui (x, 0, MPFR_RNDN); 74 mpfr_root (y, x, 17, MPFR_RNDN); 75 if (mpfr_cmp_ui (y, 0) || mpfr_sgn (y) < 0) 76 { 77 printf ("Error: root(+0,17) <> +0\n"); 78 exit (1); 79 } 80 mpfr_neg (x, x, MPFR_RNDN); 81 mpfr_root (y, x, 42, MPFR_RNDN); 82 if (mpfr_cmp_ui (y, 0) || mpfr_sgn (y) > 0) 83 { 84 printf ("Error: root(-0,42) <> -0\n"); 85 exit (1); 86 } 87 88 mpfr_set_prec (x, 53); 89 mpfr_set_str (x, "8.39005285514734966412e-01", 10, MPFR_RNDN); 90 mpfr_root (x, x, 3, MPFR_RNDN); 91 if (mpfr_cmp_str1 (x, "9.43166207799662426048e-01")) 92 { 93 printf ("Error in root3 (1)\n"); 94 printf ("expected 9.43166207799662426048e-01\n"); 95 printf ("got "); 96 mpfr_dump (x); 97 exit (1); 98 } 99 100 mpfr_set_prec (x, 32); 101 mpfr_set_prec (y, 32); 102 mpfr_set_str_binary (x, "0.10000100001100101001001001011001"); 103 mpfr_root (x, x, 3, MPFR_RNDN); 104 mpfr_set_str_binary (y, "0.11001101011000100111000111111001"); 105 if (mpfr_cmp (x, y)) 106 { 107 printf ("Error in root3 (2)\n"); 108 exit (1); 109 } 110 111 mpfr_set_prec (x, 32); 112 mpfr_set_prec (y, 32); 113 mpfr_set_str_binary (x, "-0.1100001110110000010101011001011"); 114 mpfr_root (x, x, 3, MPFR_RNDD); 115 mpfr_set_str_binary (y, "-0.11101010000100100101000101011001"); 116 if (mpfr_cmp (x, y)) 117 { 118 printf ("Error in root3 (3)\n"); 119 exit (1); 120 } 121 122 mpfr_set_prec (x, 82); 123 mpfr_set_prec (y, 27); 124 mpfr_set_str_binary (x, "0.1010001111011101011011000111001011001101100011110110010011011011011010011001100101e-7"); 125 mpfr_root (y, x, 3, MPFR_RNDD); 126 mpfr_set_str_binary (x, "0.101011110001110001000100011E-2"); 127 if (mpfr_cmp (x, y)) 128 { 129 printf ("Error in root3 (4)\n"); 130 exit (1); 131 } 132 133 mpfr_set_prec (x, 204); 134 mpfr_set_prec (y, 38); 135 mpfr_set_str_binary (x, "0.101000000001101000000001100111111011111001110110100001111000100110100111001101100111110001110001011011010110010011100101111001111100001010010100111011101100000011011000101100010000000011000101001010001001E-5"); 136 mpfr_root (y, x, 3, MPFR_RNDD); 137 mpfr_set_str_binary (x, "0.10001001111010011011101000010110110010E-1"); 138 if (mpfr_cmp (x, y)) 139 { 140 printf ("Error in root3 (5)\n"); 141 exit (1); 142 } 143 144 /* Worst case found on 2006-11-25 */ 145 mpfr_set_prec (x, 53); 146 mpfr_set_prec (y, 53); 147 mpfr_set_str_binary (x, "1.0100001101101101001100110001001000000101001101100011E28"); 148 mpfr_root (y, x, 35, MPFR_RNDN); 149 mpfr_set_str_binary (x, "1.1100000010110101100011101011000010100001101100100011E0"); 150 if (mpfr_cmp (x, y)) 151 { 152 printf ("Error in mpfr_root (y, x, 35, MPFR_RNDN) for\n" 153 "x = 1.0100001101101101001100110001001000000101001101100011E28\n" 154 "Expected "); 155 mpfr_dump (x); 156 printf ("Got "); 157 mpfr_dump (y); 158 exit (1); 159 } 160 /* Worst cases found on 2006-11-26 */ 161 mpfr_set_str_binary (x, "1.1111010011101110001111010110000101110000110110101100E17"); 162 mpfr_root (y, x, 36, MPFR_RNDD); 163 mpfr_set_str_binary (x, "1.0110100111010001101001010111001110010100111111000010E0"); 164 if (mpfr_cmp (x, y)) 165 { 166 printf ("Error in mpfr_root (y, x, 36, MPFR_RNDD) for\n" 167 "x = 1.1111010011101110001111010110000101110000110110101100E17\n" 168 "Expected "); 169 mpfr_dump (x); 170 printf ("Got "); 171 mpfr_dump (y); 172 exit (1); 173 } 174 mpfr_set_str_binary (x, "1.1100011101101101100010110001000001110001111110010000E23"); 175 mpfr_root (y, x, 36, MPFR_RNDU); 176 mpfr_set_str_binary (x, "1.1001010100001110000110111111100011011101110011000100E0"); 177 if (mpfr_cmp (x, y)) 178 { 179 printf ("Error in mpfr_root (y, x, 36, MPFR_RNDU) for\n" 180 "x = 1.1100011101101101100010110001000001110001111110010000E23\n" 181 "Expected "); 182 mpfr_dump (x); 183 printf ("Got "); 184 mpfr_dump (y); 185 exit (1); 186 } 187 188 /* Check for k = 1 */ 189 mpfr_set_ui (x, 17, MPFR_RNDN); 190 i = mpfr_root (y, x, 1, MPFR_RNDN); 191 if (mpfr_cmp_ui (x, 17) || i != 0) 192 { 193 printf ("Error in root (17^(1/1))\n"); 194 exit (1); 195 } 196 197#if 0 198 /* Check for k == 0: 199 For 0 <= x < 1 => +0. 200 For x = 1 => 1. 201 For x > 1, => +Inf. 202 For x < 0 => NaN. */ 203 i = mpfr_root (y, x, 0, MPFR_RNDN); 204 if (!MPFR_IS_INF (y) || !MPFR_IS_POS (y) || i != 0) 205 { 206 printf ("Error in root 17^(1/0)\n"); 207 exit (1); 208 } 209 mpfr_set_ui (x, 1, MPFR_RNDN); 210 i = mpfr_root (y, x, 0, MPFR_RNDN); 211 if (mpfr_cmp_ui (y, 1) || i != 0) 212 { 213 printf ("Error in root 1^(1/0)\n"); 214 exit (1); 215 } 216 mpfr_set_ui (x, 0, MPFR_RNDN); 217 i = mpfr_root (y, x, 0, MPFR_RNDN); 218 if (!MPFR_IS_ZERO (y) || !MPFR_IS_POS (y) || i != 0) 219 { 220 printf ("Error in root 0+^(1/0)\n"); 221 exit (1); 222 } 223 MPFR_CHANGE_SIGN (x); 224 i = mpfr_root (y, x, 0, MPFR_RNDN); 225 if (!MPFR_IS_ZERO (y) || !MPFR_IS_POS (y) || i != 0) 226 { 227 printf ("Error in root 0-^(1/0)\n"); 228 exit (1); 229 } 230 mpfr_set_ui_2exp (x, 17, -5, MPFR_RNDD); 231 i = mpfr_root (y, x, 0, MPFR_RNDN); 232 if (!MPFR_IS_ZERO (y) || !MPFR_IS_POS (y) || i != 0) 233 { 234 printf ("Error in root (17/2^5)^(1/0)\n"); 235 exit (1); 236 } 237#endif 238 mpfr_set_ui (x, 0, MPFR_RNDN); 239 i = mpfr_root (y, x, 0, MPFR_RNDN); 240 if (!MPFR_IS_NAN (y) || i != 0) 241 { 242 printf ("Error in root 0+^(1/0)\n"); 243 exit (1); 244 } 245 /* Check for k==2 */ 246 mpfr_set_si (x, -17, MPFR_RNDD); 247 i = mpfr_root (y, x, 2, MPFR_RNDN); 248 if (!MPFR_IS_NAN (y) || i != 0) 249 { 250 printf ("Error in root (-17)^(1/2)\n"); 251 exit (1); 252 } 253 254 mpfr_clear (x); 255 mpfr_clear (y); 256} 257 258#define TEST_FUNCTION mpfr_root 259#define INTEGER_TYPE unsigned long 260#define INT_RAND_FUNCTION() (INTEGER_TYPE) (randlimb () % 3 +2) 261#include "tgeneric_ui.c" 262 263int 264main (void) 265{ 266 mpfr_t x; 267 int r; 268 mpfr_prec_t p; 269 unsigned long k; 270 271 tests_start_mpfr (); 272 273 special (); 274 275 mpfr_init (x); 276 277 for (p = 2; p < 100; p++) 278 { 279 mpfr_set_prec (x, p); 280 for (r = 0; r < MPFR_RND_MAX; r++) 281 { 282 mpfr_set_ui (x, 1, MPFR_RNDN); 283 k = 2 + randlimb () % 4; /* 2 <= k <= 5 */ 284 mpfr_root (x, x, k, (mpfr_rnd_t) r); 285 if (mpfr_cmp_ui (x, 1)) 286 { 287 printf ("Error in mpfr_root(%lu) for x=1, rnd=%s\ngot ", 288 k, mpfr_print_rnd_mode ((mpfr_rnd_t) r)); 289 mpfr_out_str (stdout, 2, 0, x, MPFR_RNDN); 290 printf ("\n"); 291 exit (1); 292 } 293 mpfr_set_si (x, -1, MPFR_RNDN); 294 if (k % 2) 295 { 296 mpfr_root (x, x, k, (mpfr_rnd_t) r); 297 if (mpfr_cmp_si (x, -1)) 298 { 299 printf ("Error in mpfr_root(%lu) for x=-1, rnd=%s\ngot ", 300 k, mpfr_print_rnd_mode ((mpfr_rnd_t) r)); 301 mpfr_out_str (stdout, 2, 0, x, MPFR_RNDN); 302 printf ("\n"); 303 exit (1); 304 } 305 } 306 307 if (p >= 5) 308 { 309 int i; 310 for (i = -12; i <= 12; i++) 311 { 312 mpfr_set_ui (x, 27, MPFR_RNDN); 313 mpfr_mul_2si (x, x, 3*i, MPFR_RNDN); 314 mpfr_root (x, x, 3, MPFR_RNDN); 315 if (mpfr_cmp_si_2exp (x, 3, i)) 316 { 317 printf ("Error in mpfr_root(3) for " 318 "x = 27.0 * 2^(%d), rnd=%s\ngot ", 319 3*i, mpfr_print_rnd_mode ((mpfr_rnd_t) r)); 320 mpfr_out_str (stdout, 2, 0, x, MPFR_RNDN); 321 printf ("\ninstead of 3 * 2^(%d)\n", i); 322 exit (1); 323 } 324 } 325 } 326 } 327 } 328 mpfr_clear (x); 329 330 test_generic_ui (2, 200, 30); 331 332 tests_end_mpfr (); 333 return 0; 334} 335