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