1/* Test file for mpfr_eq. 2 3Copyright 1999, 2000, 2001, 2002, 2003, 2004, 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 29teq (mpfr_t x) 30{ 31 mpfr_t y; 32 unsigned long k, px, mx; 33 34 mpfr_init2 (y, MPFR_PREC(x)); 35 36 mx = (MPFR_PREC(x) - 1) / mp_bits_per_limb; 37 px = mp_bits_per_limb - 2; 38 39 for (k = 2; k < MPFR_PREC(x); k++) 40 { 41 mpfr_set (y, x, MPFR_RNDN); 42 43 MPFR_MANT(y) [mx] ^= (mp_limb_t) 1 << px; 44 45 if (mpfr_eq(y, x, k) || !mpfr_eq(y, x, k - 1)) 46 { 47 printf ("Error in eq.\n"); 48 printf ("x = "); mpfr_print_binary (x); printf ("\n"); 49 printf ("y = "); mpfr_print_binary (y); printf ("\n"); 50 printf ("k = %lu\n", k); 51 printf ("mpfr_eq(y, x, k) = %d\nmpfr_eq(y, x, k - 1) = %d\n", 52 mpfr_eq (y, x, k), mpfr_eq (y, x, k - 1)); 53 exit (1); 54 } 55 56 if (px) 57 { 58 --px; 59 } 60 else 61 { 62 --mx; 63 px = mp_bits_per_limb - 1; 64 } 65 } 66 mpfr_clear (y); 67} 68 69static void 70special (void) 71{ 72 mpfr_t x, y, z; 73 int i, error = 0; 74 75 mpfr_init2 (x, 53); 76 mpfr_init2 (y, 53); 77 mpfr_init2 (z, 53); 78 79 mpfr_set_str (x, "1", 10, (mpfr_rnd_t) 0); 80 mpfr_set_str (y, "1e-10000", 10, (mpfr_rnd_t) 0); 81 mpfr_add (z, x, y, MPFR_RNDU); 82 83 for (i = 1; i <= 52; i++) 84 if (mpfr_eq (x, z, i) == 0) 85 error = 1; 86 for (i = 53; i <= 100; i++) 87 if (mpfr_eq (x, z, i) != 0) 88 error = 1; 89 if (mpfr_eq (x, z, 1000) != 0) 90 error = 1; 91 92 if (error) 93 { 94 printf ("Error in mpfr_eq (1, 1+1e-1000)\n"); 95 exit (1); 96 } 97 98 mpfr_set_nan (x); 99 mpfr_set_nan (y); 100 MPFR_ASSERTN(mpfr_eq (x, y, 1) == 0); 101 102 mpfr_set_inf (y, 1); 103 MPFR_ASSERTN(mpfr_eq (x, y, 1) == 0); 104 105 mpfr_set_ui (y, 0, MPFR_RNDN); 106 MPFR_ASSERTN(mpfr_eq (x, y, 1) == 0); 107 108 mpfr_set_inf (x, 1); 109 mpfr_set_inf (y, 1); 110 MPFR_ASSERTN(mpfr_eq (x, y, 1)); 111 112 mpfr_set_inf (x, 1); 113 mpfr_set_inf (y, -1); 114 MPFR_ASSERTN(mpfr_eq (x, y, 1) == 0); 115 116 mpfr_set_inf (x, -1); 117 mpfr_set_inf (y, -1); 118 MPFR_ASSERTN(mpfr_eq (x, y, 1)); 119 120 mpfr_set_inf (x, 1); 121 mpfr_set_ui (y, 0, MPFR_RNDN); 122 MPFR_ASSERTN(mpfr_eq (x, y, 1) == 0); 123 124 mpfr_set_ui (x, 1, MPFR_RNDN); 125 mpfr_set_ui (y, 0, MPFR_RNDN); 126 MPFR_ASSERTN(mpfr_eq (x, y, 1) == 0); 127 MPFR_ASSERTN(mpfr_eq (y, x, 1) == 0); 128 129 mpfr_set_ui (x, 0, MPFR_RNDN); 130 mpfr_set_ui (y, 0, MPFR_RNDN); 131 MPFR_ASSERTN(mpfr_eq (x, y, 1)); 132 133 mpfr_neg (y, y, MPFR_RNDN); 134 MPFR_ASSERTN(mpfr_eq (x, y, 1)); 135 136 mpfr_neg (x, x, MPFR_RNDN); 137 MPFR_ASSERTN(mpfr_eq (x, y, 1)); 138 139 mpfr_set_ui (x, 1, MPFR_RNDN); 140 mpfr_neg (y, x, MPFR_RNDN); 141 MPFR_ASSERTN(mpfr_eq (x, y, 1) == 0); 142 143 mpfr_set_prec (x, 2 * mp_bits_per_limb); 144 mpfr_set_prec (y, mp_bits_per_limb); 145 mpfr_set_ui (x, 1, MPFR_RNDN); 146 mpfr_set_ui (y, 1, MPFR_RNDN); 147 MPFR_ASSERTN(mpfr_eq (x, y, mp_bits_per_limb - 1)); 148 MPFR_ASSERTN(mpfr_eq (x, y, mp_bits_per_limb)); 149 MPFR_ASSERTN(mpfr_eq (x, y, mp_bits_per_limb + 1)); 150 MPFR_ASSERTN(mpfr_eq (x, y, 2 * mp_bits_per_limb - 1)); 151 MPFR_ASSERTN(mpfr_eq (x, y, 2 * mp_bits_per_limb)); 152 MPFR_ASSERTN(mpfr_eq (x, y, 2 * mp_bits_per_limb + 1)); 153 154 mpfr_nextabove (x); 155 MPFR_ASSERTN(mpfr_eq (x, y, mp_bits_per_limb - 1)); 156 MPFR_ASSERTN(mpfr_eq (x, y, mp_bits_per_limb)); 157 MPFR_ASSERTN(mpfr_eq (x, y, mp_bits_per_limb + 1)); 158 MPFR_ASSERTN(mpfr_eq (x, y, 2 * mp_bits_per_limb - 1)); 159 MPFR_ASSERTN(mpfr_eq (x, y, 2 * mp_bits_per_limb) == 0); 160 MPFR_ASSERTN(mpfr_eq (x, y, 2 * mp_bits_per_limb + 1) == 0); 161 MPFR_ASSERTN(mpfr_eq (y, x, mp_bits_per_limb - 1)); 162 MPFR_ASSERTN(mpfr_eq (y, x, mp_bits_per_limb)); 163 MPFR_ASSERTN(mpfr_eq (y, x, mp_bits_per_limb + 1)); 164 MPFR_ASSERTN(mpfr_eq (y, x, 2 * mp_bits_per_limb - 1)); 165 MPFR_ASSERTN(mpfr_eq (y, x, 2 * mp_bits_per_limb) == 0); 166 MPFR_ASSERTN(mpfr_eq (y, x, 2 * mp_bits_per_limb + 1) == 0); 167 168 mpfr_set_ui (x, 1, MPFR_RNDN); 169 mpfr_set_ui (y, 2, MPFR_RNDN); 170 MPFR_ASSERTN(mpfr_eq (x, y, 1) == 0); 171 172 mpfr_set_prec (x, 2 * mp_bits_per_limb); 173 mpfr_set_prec (y, 2 * mp_bits_per_limb); 174 mpfr_set_ui (x, 2, MPFR_RNDN); 175 mpfr_set_ui (y, 3, MPFR_RNDN); 176 MPFR_ASSERTN(mpfr_eq (x, y, 1)); 177 MPFR_ASSERTN(mpfr_eq (x, y, 2) == 0); 178 MPFR_ASSERTN(mpfr_eq (x, y, mp_bits_per_limb) == 0); 179 MPFR_ASSERTN(mpfr_eq (x, y, mp_bits_per_limb + 1) == 0); 180 181 mpfr_clear (x); 182 mpfr_clear (y); 183 mpfr_clear (z); 184} 185 186int 187main (void) 188{ 189 int j; 190 mpfr_t x; 191 192 tests_start_mpfr (); 193 194 special (); 195 196 mpfr_init2 (x, 500); 197 198 for (j = 0; j < 500; j++) 199 { 200 mpfr_urandomb (x, RANDS); 201 teq (x); 202 } 203 204 mpfr_clear (x); 205 206 tests_end_mpfr (); 207 return 0; 208} 209