Home | History | Annotate | Line # | Download | only in tests
tset_z.c revision 1.1.1.5
      1      1.1  mrg /* Test file for mpfr_set_z.
      2      1.1  mrg 
      3  1.1.1.5  mrg Copyright 1999, 2001-2020 Free Software Foundation, Inc.
      4  1.1.1.3  mrg Contributed by the AriC and Caramba projects, INRIA.
      5      1.1  mrg 
      6      1.1  mrg This file is part of the GNU MPFR Library.
      7      1.1  mrg 
      8      1.1  mrg The GNU MPFR Library is free software; you can redistribute it and/or modify
      9      1.1  mrg it under the terms of the GNU Lesser General Public License as published by
     10      1.1  mrg the Free Software Foundation; either version 3 of the License, or (at your
     11      1.1  mrg option) any later version.
     12      1.1  mrg 
     13      1.1  mrg The GNU MPFR Library is distributed in the hope that it will be useful, but
     14      1.1  mrg WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
     15      1.1  mrg or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
     16      1.1  mrg License for more details.
     17      1.1  mrg 
     18      1.1  mrg You should have received a copy of the GNU Lesser General Public License
     19      1.1  mrg along with the GNU MPFR Library; see the file COPYING.LESSER.  If not, see
     20  1.1.1.5  mrg https://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
     21      1.1  mrg 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
     22      1.1  mrg 
     23      1.1  mrg #include "mpfr-test.h"
     24      1.1  mrg 
     25      1.1  mrg static void check0(void)
     26      1.1  mrg {
     27      1.1  mrg   mpz_t y;
     28      1.1  mrg   mpfr_t x;
     29      1.1  mrg   int inexact, r;
     30      1.1  mrg 
     31      1.1  mrg   /* Check for +0 */
     32      1.1  mrg   mpfr_init (x);
     33      1.1  mrg   mpz_init (y);
     34      1.1  mrg   mpz_set_si (y, 0);
     35      1.1  mrg   for(r = 0; r < MPFR_RND_MAX; r++)
     36      1.1  mrg     {
     37      1.1  mrg       inexact = mpfr_set_z (x, y, (mpfr_rnd_t) r);
     38      1.1  mrg       if (!MPFR_IS_ZERO(x) || !MPFR_IS_POS(x) || inexact)
     39      1.1  mrg         {
     40      1.1  mrg           printf("mpfr_set_z(x,0) failed for %s\n",
     41      1.1  mrg                  mpfr_print_rnd_mode ((mpfr_rnd_t) r));
     42      1.1  mrg           exit(1);
     43      1.1  mrg         }
     44      1.1  mrg     }
     45      1.1  mrg   mpfr_clear(x);
     46      1.1  mrg   mpz_clear(y);
     47      1.1  mrg }
     48      1.1  mrg 
     49      1.1  mrg /* FIXME: It'd be better to examine the actual data in an mpfr_t to see that
     50      1.1  mrg    it's as expected.  Comparing mpfr_set_z with mpfr_cmp or against
     51      1.1  mrg    mpfr_get_si is a rather indirect test of a low level routine.  */
     52      1.1  mrg 
     53      1.1  mrg static void
     54      1.1  mrg check (long i, mpfr_rnd_t rnd)
     55      1.1  mrg {
     56      1.1  mrg   mpfr_t f;
     57      1.1  mrg   mpz_t z;
     58      1.1  mrg 
     59      1.1  mrg   mpfr_init2 (f, 8 * sizeof(long));
     60      1.1  mrg   mpz_init (z);
     61      1.1  mrg   mpz_set_ui (z, i);
     62      1.1  mrg   mpfr_set_z (f, z, rnd);
     63      1.1  mrg   if (mpfr_get_si (f, MPFR_RNDZ) != i)
     64      1.1  mrg     {
     65      1.1  mrg       printf ("Error in mpfr_set_z for i=%ld rnd_mode=%d\n", i, rnd);
     66      1.1  mrg       exit (1);
     67      1.1  mrg     }
     68      1.1  mrg   mpfr_clear (f);
     69      1.1  mrg   mpz_clear (z);
     70      1.1  mrg }
     71      1.1  mrg 
     72      1.1  mrg static void
     73      1.1  mrg check_large (void)
     74      1.1  mrg {
     75      1.1  mrg   mpz_t z;
     76      1.1  mrg   mpfr_t x, y;
     77      1.1  mrg   mpfr_exp_t emax, emin;
     78      1.1  mrg 
     79      1.1  mrg   mpz_init (z);
     80      1.1  mrg   mpfr_init2 (x, 160);
     81      1.1  mrg   mpfr_init2 (y, 160);
     82      1.1  mrg 
     83      1.1  mrg   mpz_set_str (z, "77031627725494291259359895954016675357279104942148788042", 10);
     84      1.1  mrg   mpfr_set_z (x, z, MPFR_RNDN);
     85      1.1  mrg   mpfr_set_str_binary (y, "0.1100100100001111110110101010001000100001011010001100001000110100110001001100011001100010100010111000000011011100000111001101000100101001000000100100111000001001E186");
     86      1.1  mrg   if (mpfr_cmp (x, y))
     87      1.1  mrg     {
     88      1.1  mrg       printf ("Error in mpfr_set_z on large input\n");
     89      1.1  mrg       exit (1);
     90      1.1  mrg     }
     91      1.1  mrg 
     92      1.1  mrg   /* check overflow */
     93      1.1  mrg   emax = mpfr_get_emax ();
     94      1.1  mrg   set_emax (2);
     95      1.1  mrg   mpz_set_str (z, "7", 10);
     96      1.1  mrg   mpfr_set_z (x, z, MPFR_RNDU);
     97      1.1  mrg   MPFR_ASSERTN(mpfr_inf_p (x) && mpfr_sgn (x) > 0);
     98      1.1  mrg   set_emax (3);
     99      1.1  mrg   mpfr_set_prec (x, 2);
    100      1.1  mrg   mpz_set_str (z, "7", 10);
    101      1.1  mrg   mpfr_set_z (x, z, MPFR_RNDU);
    102      1.1  mrg   MPFR_ASSERTN(mpfr_inf_p (x) && mpfr_sgn (x) > 0);
    103      1.1  mrg   set_emax (emax);
    104      1.1  mrg 
    105      1.1  mrg   /* check underflow */
    106      1.1  mrg   emin = mpfr_get_emin ();
    107      1.1  mrg   set_emin (3);
    108      1.1  mrg   mpz_set_str (z, "1", 10);
    109      1.1  mrg   mpfr_set_z (x, z, MPFR_RNDZ);
    110      1.1  mrg   MPFR_ASSERTN(mpfr_cmp_ui (x, 0) == 0 && MPFR_IS_POS(x));
    111      1.1  mrg   set_emin (2);
    112      1.1  mrg   mpfr_set_z (x, z, MPFR_RNDN);
    113      1.1  mrg   MPFR_ASSERTN(mpfr_cmp_ui (x, 0) == 0 && MPFR_IS_POS(x));
    114      1.1  mrg   set_emin (emin);
    115      1.1  mrg 
    116      1.1  mrg   mpz_clear (z);
    117      1.1  mrg 
    118      1.1  mrg   mpfr_clear (x);
    119      1.1  mrg   mpfr_clear (y);
    120      1.1  mrg }
    121      1.1  mrg 
    122      1.1  mrg int
    123      1.1  mrg main (int argc, char *argv[])
    124      1.1  mrg {
    125      1.1  mrg   long j;
    126      1.1  mrg 
    127      1.1  mrg   tests_start_mpfr ();
    128      1.1  mrg 
    129      1.1  mrg   check_large ();
    130      1.1  mrg   check (0, MPFR_RNDN);
    131      1.1  mrg   for (j = 0; j < 200000; j++)
    132      1.1  mrg     check (randlimb () & LONG_MAX, RND_RAND ());
    133      1.1  mrg   check0 ();
    134      1.1  mrg 
    135      1.1  mrg   tests_end_mpfr ();
    136      1.1  mrg 
    137      1.1  mrg   return 0;
    138      1.1  mrg }
    139