1/* Test file for mpfr_pow. 2 3Copyright 2001-2023 Free Software Foundation, Inc. 4Contributed by the AriC and Caramba 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 20https://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 "mpfr-test.h" 24 25 26int 27main (int argc, char *argv[]) 28{ 29 mpfr_t x, y, z; 30 31 mpfr_prec_t prec, yprec; 32 mpfr_t t, s; 33 mpfr_rnd_t rnd; 34 int inexact, compare, compare2; 35 unsigned int n, err; 36 37 mpfr_prec_t p0=2, p1=100; 38 unsigned int N=25; 39 40 tests_start_mpfr (); 41 42 mpfr_init (x); 43 mpfr_init2 (y,sizeof(unsigned long int)*CHAR_BIT); 44 mpfr_init (z); 45 46 mpfr_init (s); 47 mpfr_init (t); 48 49 /* generic test */ 50 for (prec = p0; prec <= p1; prec++) 51 { 52 mpfr_set_prec (x, prec); 53 mpfr_set_prec (s, sizeof(unsigned long int)*CHAR_BIT); 54 mpfr_set_prec (z, prec); 55 mpfr_set_prec (t, prec); 56 yprec = prec + 10; 57 58 for (n=0; n<N; n++) 59 { 60 mpfr_urandomb (x, RANDS); 61 mpfr_urandomb (s, RANDS); 62 if (RAND_BOOL ()) 63 mpfr_neg (s, s, MPFR_RNDN); 64 rnd = RND_RAND_NO_RNDF (); 65 mpfr_set_prec (y, yprec); 66 compare = mpfr_pow (y, x, s, rnd); 67 err = (rnd == MPFR_RNDN) ? yprec + 1 : yprec; 68 if (mpfr_can_round (y, err, rnd, rnd, prec)) 69 { 70 mpfr_set (t, y, rnd); 71 inexact = mpfr_pow (z, x, s, rnd); 72 if (mpfr_cmp (t, z)) 73 { 74 printf ("results differ for x^y with\n"); 75 printf (" x="); 76 mpfr_dump (x); 77 printf (" y="); 78 mpfr_dump (s); 79 printf (" with prec=%u rnd_mode=%s\n", (unsigned int) prec, 80 mpfr_print_rnd_mode (rnd)); 81 printf ("got "); 82 mpfr_dump (z); 83 printf ("expected "); 84 mpfr_dump (t); 85 printf ("approx "); 86 mpfr_dump (y); 87 exit (1); 88 } 89 compare2 = mpfr_cmp (t, y); 90 /* if rounding to nearest, cannot know the sign of t - f(x) 91 because of composed rounding: y = o(f(x)) and t = o(y) */ 92 if ((rnd != MPFR_RNDN) && (compare * compare2 >= 0)) 93 compare = compare + compare2; 94 else 95 compare = inexact; /* cannot determine sign(t-f(x)) */ 96 if (((inexact == 0) && (compare != 0)) || 97 ((inexact > 0) && (compare <= 0)) || 98 ((inexact < 0) && (compare >= 0))) 99 { 100 printf ("Wrong inexact flag for rnd=%s: expected %d, got %d" 101 "\n", mpfr_print_rnd_mode (rnd), compare, inexact); 102 printf ("x="); mpfr_dump (x); 103 printf ("y="); mpfr_dump (y); 104 printf ("t="); mpfr_dump (t); 105 exit (1); 106 } 107 } 108 } 109 } 110 111 mpfr_clear (s); 112 mpfr_clear (t); 113 114 mpfr_clear (x); 115 mpfr_clear (y); 116 mpfr_clear (z); 117 118 tests_end_mpfr (); 119 return 0; 120} 121