(root)/
mpfr-4.2.1/
tests/
tabs.c
       1  /* Test file for mpfr_abs.
       2  
       3  Copyright 2000-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 <float.h>
      24  
      25  #include "mpfr-test.h"
      26  
      27  static void
      28  check_inexact (void)
      29  {
      30    mpfr_prec_t p, q;
      31    mpfr_t x, y, absx;
      32    int rnd;
      33    int inexact, cmp;
      34  
      35    mpfr_init (x);
      36    mpfr_init (y);
      37    mpfr_init (absx);
      38  
      39    for (p=2; p<500; p++)
      40      {
      41        mpfr_set_prec (x, p);
      42        mpfr_set_prec (absx, p);
      43        mpfr_urandomb (x, RANDS);
      44        if (RAND_BOOL ())
      45          {
      46            mpfr_set (absx, x, MPFR_RNDN);
      47            mpfr_neg (x, x, MPFR_RNDN);
      48          }
      49        else
      50          mpfr_set (absx, x, MPFR_RNDN);
      51        for (q=2; q<2*p; q++)
      52          {
      53            mpfr_set_prec (y, q);
      54            RND_LOOP_NO_RNDF (rnd)
      55              {
      56                inexact = mpfr_abs (y, x, (mpfr_rnd_t) rnd);
      57                cmp = mpfr_cmp (y, absx);
      58                if (((inexact == 0) && (cmp != 0)) ||
      59                    ((inexact > 0) && (cmp <= 0)) ||
      60                    ((inexact < 0) && (cmp >= 0)))
      61                  {
      62                    printf ("Wrong inexact flag for %s: expected %d, got %d\n",
      63                            mpfr_print_rnd_mode ((mpfr_rnd_t) rnd), cmp,
      64                            inexact);
      65                    printf ("x="); mpfr_dump (x);
      66                    printf ("absx="); mpfr_dump (absx);
      67                    printf ("y="); mpfr_dump (y);
      68                    exit (1);
      69                  }
      70              }
      71          }
      72      }
      73  
      74    mpfr_clear (x);
      75    mpfr_clear (y);
      76    mpfr_clear (absx);
      77  }
      78  
      79  static void
      80  check_cmp (int argc, char *argv[])
      81  {
      82    mpfr_t x, y;
      83    mpfr_ptr p[2];
      84    int inexact;
      85    int n, k;
      86  
      87    mpfr_inits2 (53, x, y, (mpfr_ptr) 0);
      88  
      89    /* will test with DEST != SRC and with DEST == SRC */
      90    p[0] = y;  /* y first */
      91    p[1] = x;  /* x last since it may be modified */
      92  
      93    for (k = 0; k <= 1; k++)
      94      {
      95        mpfr_set_nan (p[k]);
      96        mpfr_set_ui (x, 1, MPFR_RNDN);
      97        inexact = mpfr_abs (p[k], x, MPFR_RNDN);
      98        if (mpfr_cmp_ui (p[k], 1) || inexact != 0)
      99          {
     100            printf ("Error in mpfr_abs(1.0) for k = %d\n", k);
     101            exit (1);
     102          }
     103  
     104        mpfr_set_nan (p[k]);
     105        mpfr_set_ui (x, 1, MPFR_RNDN);
     106        inexact = (mpfr_abs) (p[k], x, MPFR_RNDN);
     107        if (mpfr_cmp_ui (p[k], 1) || inexact != 0)
     108          {
     109            printf ("Error in (mpfr_abs)(1.0) for k = %d\n", k);
     110            exit (1);
     111          }
     112  
     113        mpfr_set_nan (p[k]);
     114        mpfr_set_si (x, -1, MPFR_RNDN);
     115        inexact = mpfr_abs (p[k], x, MPFR_RNDN);
     116        if (mpfr_cmp_ui (p[k], 1) || inexact != 0)
     117          {
     118            printf ("Error in mpfr_abs(-1.0) for k = %d\n", k);
     119            exit (1);
     120          }
     121  
     122        mpfr_set_nan (p[k]);
     123        mpfr_set_si (x, -1, MPFR_RNDN);
     124        inexact = (mpfr_abs) (p[k], x, MPFR_RNDN);
     125        if (mpfr_cmp_ui (p[k], 1) || inexact != 0)
     126          {
     127            printf ("Error in (mpfr_abs)(-1.0) for k = %d\n", k);
     128            exit (1);
     129          }
     130  
     131        mpfr_set_nan (p[k]);
     132        mpfr_set_inf (x, 1);
     133        inexact = mpfr_abs (p[k], x, MPFR_RNDN);
     134        if (! mpfr_inf_p (p[k]) || mpfr_sgn (p[k]) <= 0 || inexact != 0)
     135          {
     136            printf ("Error in mpfr_abs(Inf) for k = %d\n", k);
     137            exit (1);
     138          }
     139  
     140        mpfr_set_nan (p[k]);
     141        mpfr_set_inf (x, 1);
     142        inexact = (mpfr_abs) (p[k], x, MPFR_RNDN);
     143        if (! mpfr_inf_p (p[k]) || mpfr_sgn (p[k]) <= 0 || inexact != 0)
     144          {
     145            printf ("Error in (mpfr_abs)(Inf) for k = %d\n", k);
     146            exit (1);
     147          }
     148  
     149        mpfr_set_nan (p[k]);
     150        mpfr_set_inf (x, -1);
     151        inexact = mpfr_abs (p[k], x, MPFR_RNDN);
     152        if (! mpfr_inf_p (p[k]) || mpfr_sgn (p[k]) <= 0 || inexact != 0)
     153          {
     154            printf ("Error in mpfr_abs(-Inf) for k = %d\n", k);
     155            exit (1);
     156          }
     157  
     158        mpfr_set_nan (p[k]);
     159        mpfr_set_inf (x, -1);
     160        inexact = (mpfr_abs) (p[k], x, MPFR_RNDN);
     161        if (! mpfr_inf_p (p[k]) || mpfr_sgn (p[k]) <= 0 || inexact != 0)
     162          {
     163            printf ("Error in (mpfr_abs)(-Inf) for k = %d\n", k);
     164            exit (1);
     165          }
     166  
     167        mpfr_set_zero (p[k], 1);
     168        MPFR_SET_NAN (x);
     169        MPFR_SET_POS (x);
     170        mpfr_clear_nanflag ();
     171        inexact = mpfr_abs (p[k], x, MPFR_RNDN);
     172        if (! MPFR_IS_NAN (p[k]) || ! mpfr_nanflag_p () ||
     173            mpfr_signbit (p[k]) || inexact != 0)
     174          {
     175            printf ("Error in mpfr_abs(+NaN).\n");
     176            exit (1);
     177          }
     178  
     179        mpfr_set_zero (p[k], 1);
     180        MPFR_SET_NAN (x);
     181        MPFR_SET_POS (x);
     182        mpfr_clear_nanflag ();
     183        inexact = (mpfr_abs) (p[k], x, MPFR_RNDN);
     184        if (! MPFR_IS_NAN (p[k]) || ! mpfr_nanflag_p () ||
     185            mpfr_signbit (p[k]) || inexact != 0)
     186          {
     187            printf ("Error in (mpfr_abs)(+NaN).\n");
     188            exit (1);
     189          }
     190  
     191        mpfr_set_zero (p[k], 1);
     192        MPFR_SET_NAN (x);
     193        MPFR_SET_NEG (x);
     194        mpfr_clear_nanflag ();
     195        inexact = mpfr_abs (p[k], x, MPFR_RNDN);
     196        if (! MPFR_IS_NAN (p[k]) || ! mpfr_nanflag_p () ||
     197            mpfr_signbit (p[k]) || inexact != 0)
     198          {
     199            printf ("Error in mpfr_abs(-NaN).\n");
     200            exit (1);
     201          }
     202  
     203        mpfr_set_zero (p[k], 1);
     204        MPFR_SET_NAN (x);
     205        MPFR_SET_NEG (x);
     206        mpfr_clear_nanflag ();
     207        inexact = (mpfr_abs) (p[k], x, MPFR_RNDN);
     208        if (! MPFR_IS_NAN (p[k]) || ! mpfr_nanflag_p () ||
     209            mpfr_signbit (p[k]) || inexact != 0)
     210          {
     211            printf ("Error in (mpfr_abs)(-NaN).\n");
     212            exit (1);
     213          }
     214      }
     215  
     216    n = (argc==1) ? 25000 : atoi(argv[1]);
     217    for (k = 1; k <= n; k++)
     218      {
     219        mpfr_rnd_t rnd;
     220        int sign = RAND_SIGN ();
     221  
     222        mpfr_urandomb (x, RANDS);
     223        MPFR_SET_SIGN (x, sign);
     224        rnd = RND_RAND ();
     225        mpfr_abs (y, x, rnd);
     226        MPFR_SET_POS (x);
     227        if (mpfr_cmp (x, y))
     228          {
     229            printf ("Mismatch for sign=%d and x=", sign);
     230            mpfr_dump (x);
     231            printf ("Results=");
     232            mpfr_dump (y);
     233            exit (1);
     234          }
     235      }
     236  
     237    mpfr_clears (x, y, (mpfr_ptr) 0);
     238  }
     239  
     240  #define TEST_FUNCTION mpfr_abs
     241  #include "tgeneric.c"
     242  
     243  int
     244  main (int argc, char *argv[])
     245  {
     246    tests_start_mpfr ();
     247  
     248    check_inexact ();
     249    check_cmp (argc, argv);
     250  
     251    test_generic (MPFR_PREC_MIN, 1000, 10);
     252  
     253    tests_end_mpfr ();
     254    return 0;
     255  }