1 /* Test file for mpfr_cbrt. 2 3 Copyright 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 static void 29 special (void) 30 { 31 mpfr_t x, y; 32 33 mpfr_init (x); 34 mpfr_init (y); 35 36 /* cbrt(NaN) = NaN */ 37 mpfr_set_nan (x); 38 mpfr_cbrt (y, x, MPFR_RNDN); 39 if (!mpfr_nan_p (y)) 40 { 41 printf ("Error: cbrt(NaN) <> NaN\n"); 42 exit (1); 43 } 44 45 /* cbrt(+Inf) = +Inf */ 46 mpfr_set_inf (x, 1); 47 mpfr_cbrt (y, x, MPFR_RNDN); 48 if (!mpfr_inf_p (y) || mpfr_sgn (y) < 0) 49 { 50 printf ("Error: cbrt(+Inf) <> +Inf\n"); 51 exit (1); 52 } 53 54 /* cbrt(-Inf) = -Inf */ 55 mpfr_set_inf (x, -1); 56 mpfr_cbrt (y, x, MPFR_RNDN); 57 if (!mpfr_inf_p (y) || mpfr_sgn (y) > 0) 58 { 59 printf ("Error: cbrt(-Inf) <> -Inf\n"); 60 exit (1); 61 } 62 63 /* cbrt(+/-0) = +/-0 */ 64 mpfr_set_ui (x, 0, MPFR_RNDN); 65 mpfr_cbrt (y, x, MPFR_RNDN); 66 if (mpfr_cmp_ui (y, 0) || mpfr_sgn (y) < 0) 67 { 68 printf ("Error: cbrt(+0) <> +0\n"); 69 exit (1); 70 } 71 mpfr_neg (x, x, MPFR_RNDN); 72 mpfr_cbrt (y, x, MPFR_RNDN); 73 if (mpfr_cmp_ui (y, 0) || mpfr_sgn (y) > 0) 74 { 75 printf ("Error: cbrt(-0) <> -0\n"); 76 exit (1); 77 } 78 79 mpfr_set_prec (x, 53); 80 mpfr_set_str (x, "8.39005285514734966412e-01", 10, MPFR_RNDN); 81 mpfr_cbrt (x, x, MPFR_RNDN); 82 if (mpfr_cmp_str1 (x, "9.43166207799662426048e-01")) 83 { 84 printf ("Error in crbrt (1)\n"); 85 exit (1); 86 } 87 88 mpfr_set_prec (x, 32); 89 mpfr_set_prec (y, 32); 90 mpfr_set_str_binary (x, "0.10000100001100101001001001011001"); 91 mpfr_cbrt (x, x, MPFR_RNDN); 92 mpfr_set_str_binary (y, "0.11001101011000100111000111111001"); 93 if (mpfr_cmp (x, y)) 94 { 95 printf ("Error in cbrt (2)\n"); 96 exit (1); 97 } 98 99 mpfr_set_prec (x, 32); 100 mpfr_set_prec (y, 32); 101 mpfr_set_str_binary (x, "-0.1100001110110000010101011001011"); 102 mpfr_cbrt (x, x, MPFR_RNDD); 103 mpfr_set_str_binary (y, "-0.11101010000100100101000101011001"); 104 if (mpfr_cmp (x, y)) 105 { 106 printf ("Error in cbrt (3)\n"); 107 exit (1); 108 } 109 110 mpfr_set_prec (x, 82); 111 mpfr_set_prec (y, 27); 112 mpfr_set_str_binary (x, "0.1010001111011101011011000111001011001101100011110110010011011011011010011001100101e-7"); 113 mpfr_cbrt (y, x, MPFR_RNDD); 114 mpfr_set_str_binary (x, "0.101011110001110001000100011E-2"); 115 if (mpfr_cmp (x, y)) 116 { 117 printf ("Error in cbrt (4)\n"); 118 exit (1); 119 } 120 121 mpfr_set_prec (x, 204); 122 mpfr_set_prec (y, 38); 123 mpfr_set_str_binary (x, "0.101000000001101000000001100111111011111001110110100001111000100110100111001101100111110001110001011011010110010011100101111001111100001010010100111011101100000011011000101100010000000011000101001010001001E-5"); 124 mpfr_cbrt (y, x, MPFR_RNDD); 125 mpfr_set_str_binary (x, "0.10001001111010011011101000010110110010E-1"); 126 if (mpfr_cmp (x, y)) 127 { 128 printf ("Error in cbrt (5)\n"); 129 exit (1); 130 } 131 132 /* Bug (in the compiler?) found on Linux/m68k with gcc 4.0.2 */ 133 mpfr_set_prec (x, 5); 134 mpfr_set_prec (y, 5); 135 mpfr_set_str_binary (x, "1.1000E-2"); 136 mpfr_cbrt (y, x, MPFR_RNDN); 137 mpfr_set_str_binary (x, "1.0111E-1"); 138 if (mpfr_cmp (x, y)) 139 { 140 printf ("Error in cbrt (6)\n"); 141 exit (1); 142 } 143 144 mpfr_clear (x); 145 mpfr_clear (y); 146 } 147 148 #define TEST_FUNCTION mpfr_cbrt 149 #include "tgeneric.c" 150 151 int 152 main (void) 153 { 154 mpfr_t x; 155 int r; 156 mpfr_prec_t p; 157 158 tests_start_mpfr (); 159 160 special (); 161 162 mpfr_init (x); 163 164 for (p=2; p<100; p++) 165 { 166 mpfr_set_prec (x, p); 167 for (r = 0; r < MPFR_RND_MAX; r++) 168 { 169 mpfr_set_ui (x, 1, MPFR_RNDN); 170 mpfr_cbrt (x, x, (mpfr_rnd_t) r); 171 if (mpfr_cmp_ui (x, 1)) 172 { 173 printf ("Error in mpfr_cbrt for x=1, rnd=%s\ngot ", 174 mpfr_print_rnd_mode ((mpfr_rnd_t) r)); 175 mpfr_out_str (stdout, 2, 0, x, MPFR_RNDN); 176 printf ("\n"); 177 exit (1); 178 } 179 mpfr_set_si (x, -1, MPFR_RNDN); 180 mpfr_cbrt (x, x, (mpfr_rnd_t) r); 181 if (mpfr_cmp_si (x, -1)) 182 { 183 printf ("Error in mpfr_cbrt for x=-1, rnd=%s\ngot ", 184 mpfr_print_rnd_mode ((mpfr_rnd_t) r)); 185 mpfr_out_str (stdout, 2, 0, x, MPFR_RNDN); 186 printf ("\n"); 187 exit (1); 188 } 189 190 if (p >= 5) 191 { 192 int i; 193 for (i = -12; i <= 12; i++) 194 { 195 mpfr_set_ui (x, 27, MPFR_RNDN); 196 mpfr_mul_2si (x, x, 3*i, MPFR_RNDN); 197 mpfr_cbrt (x, x, MPFR_RNDN); 198 if (mpfr_cmp_si_2exp (x, 3, i)) 199 { 200 printf ("Error in mpfr_cbrt for " 201 "x = 27.0 * 2^(%d), rnd=%s\ngot ", 202 3*i, mpfr_print_rnd_mode ((mpfr_rnd_t) r)); 203 mpfr_out_str (stdout, 2, 0, x, MPFR_RNDN); 204 printf ("\ninstead of 3 * 2^(%d)\n", i); 205 exit (1); 206 } 207 } 208 } 209 } 210 } 211 mpfr_clear (x); 212 213 test_generic (2, 200, 10); 214 215 data_check ("data/cbrt", mpfr_cbrt, "mpfr_cbrt"); 216 217 tests_end_mpfr (); 218 return 0; 219 } 220