Home | History | Annotate | Line # | Download | only in tests
tlog_ui.c revision 1.1.1.1.4.2
      1  1.1.1.1.4.2  christos /* Test file for mpfr_log_ui.
      2  1.1.1.1.4.2  christos 
      3  1.1.1.1.4.2  christos Copyright 2016-2018 Free Software Foundation, Inc.
      4  1.1.1.1.4.2  christos Contributed by the AriC and Caramba projects, INRIA.
      5  1.1.1.1.4.2  christos 
      6  1.1.1.1.4.2  christos This file is part of the GNU MPFR Library.
      7  1.1.1.1.4.2  christos 
      8  1.1.1.1.4.2  christos The GNU MPFR Library is free software; you can redistribute it and/or modify
      9  1.1.1.1.4.2  christos it under the terms of the GNU Lesser General Public License as published by
     10  1.1.1.1.4.2  christos the Free Software Foundation; either version 3 of the License, or (at your
     11  1.1.1.1.4.2  christos option) any later version.
     12  1.1.1.1.4.2  christos 
     13  1.1.1.1.4.2  christos The GNU MPFR Library is distributed in the hope that it will be useful, but
     14  1.1.1.1.4.2  christos WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
     15  1.1.1.1.4.2  christos or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
     16  1.1.1.1.4.2  christos License for more details.
     17  1.1.1.1.4.2  christos 
     18  1.1.1.1.4.2  christos You should have received a copy of the GNU Lesser General Public License
     19  1.1.1.1.4.2  christos along with the GNU MPFR Library; see the file COPYING.LESSER.  If not, see
     20  1.1.1.1.4.2  christos http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
     21  1.1.1.1.4.2  christos 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
     22  1.1.1.1.4.2  christos 
     23  1.1.1.1.4.2  christos #include "mpfr-test.h"
     24  1.1.1.1.4.2  christos 
     25  1.1.1.1.4.2  christos static void
     26  1.1.1.1.4.2  christos compare_with_log (unsigned long n, mpfr_prec_t p)
     27  1.1.1.1.4.2  christos {
     28  1.1.1.1.4.2  christos   mpfr_t x, y;
     29  1.1.1.1.4.2  christos   int inex1, inex2;
     30  1.1.1.1.4.2  christos   mpfr_flags_t flags1;
     31  1.1.1.1.4.2  christos 
     32  1.1.1.1.4.2  christos   mpfr_init2 (x, sizeof (unsigned long) * CHAR_BIT);
     33  1.1.1.1.4.2  christos   mpfr_init2 (y, p);
     34  1.1.1.1.4.2  christos   inex1 = mpfr_set_ui (x, n, MPFR_RNDN);
     35  1.1.1.1.4.2  christos   MPFR_ASSERTN(inex1 == 0);
     36  1.1.1.1.4.2  christos   inex1 = mpfr_log (y, x, MPFR_RNDN);
     37  1.1.1.1.4.2  christos   flags1 = __gmpfr_flags;
     38  1.1.1.1.4.2  christos   mpfr_set_prec (x, p);
     39  1.1.1.1.4.2  christos   inex2 = mpfr_log_ui (x, n, MPFR_RNDN);
     40  1.1.1.1.4.2  christos   MPFR_ASSERTN(inex1 == inex2);
     41  1.1.1.1.4.2  christos   MPFR_ASSERTN(flags1 == __gmpfr_flags);
     42  1.1.1.1.4.2  christos   MPFR_ASSERTN(mpfr_equal_p (x, y));
     43  1.1.1.1.4.2  christos   mpfr_clears (x, y, (mpfr_ptr) 0);
     44  1.1.1.1.4.2  christos }
     45  1.1.1.1.4.2  christos 
     46  1.1.1.1.4.2  christos #define TEST_FUNCTION mpfr_log_ui
     47  1.1.1.1.4.2  christos #define ONE_ARG
     48  1.1.1.1.4.2  christos #define ULONG_ARG1
     49  1.1.1.1.4.2  christos #define RAND_FUNCTION(x) mpfr_set_ui (x, randlimb (), MPFR_RNDN)
     50  1.1.1.1.4.2  christos #include "tgeneric.c"
     51  1.1.1.1.4.2  christos 
     52  1.1.1.1.4.2  christos #define TEST_FUNCTION mpfr_log_ui
     53  1.1.1.1.4.2  christos 
     54  1.1.1.1.4.2  christos int
     55  1.1.1.1.4.2  christos main (int argc, char *argv[])
     56  1.1.1.1.4.2  christos {
     57  1.1.1.1.4.2  christos   unsigned int prec, yprec;
     58  1.1.1.1.4.2  christos   int rnd;
     59  1.1.1.1.4.2  christos   mpfr_t x, y, z, t, v;
     60  1.1.1.1.4.2  christos   unsigned long m, n;
     61  1.1.1.1.4.2  christos   int inex;
     62  1.1.1.1.4.2  christos   mpfr_exp_t emin, emax;
     63  1.1.1.1.4.2  christos   int i;
     64  1.1.1.1.4.2  christos 
     65  1.1.1.1.4.2  christos   tests_start_mpfr ();
     66  1.1.1.1.4.2  christos 
     67  1.1.1.1.4.2  christos   emin = mpfr_get_emin ();
     68  1.1.1.1.4.2  christos   emax = mpfr_get_emax ();
     69  1.1.1.1.4.2  christos 
     70  1.1.1.1.4.2  christos   mpfr_inits2 (53, x, y, z, t, (mpfr_ptr) 0);
     71  1.1.1.1.4.2  christos   mpfr_init2 (v, sizeof (unsigned long) * CHAR_BIT);
     72  1.1.1.1.4.2  christos 
     73  1.1.1.1.4.2  christos   if (argc >= 3) /* tlog_ui n prec [rnd] */
     74  1.1.1.1.4.2  christos     {
     75  1.1.1.1.4.2  christos       mpfr_set_prec (x, strtoul (argv[2], NULL, 10));
     76  1.1.1.1.4.2  christos       TEST_FUNCTION (x, strtoul (argv[1], NULL, 10),
     77  1.1.1.1.4.2  christos                     argc > 3 ? (mpfr_rnd_t) atoi (argv[3]) : MPFR_RNDN);
     78  1.1.1.1.4.2  christos       mpfr_out_str (stdout, 10, 0, x, MPFR_RNDN);
     79  1.1.1.1.4.2  christos       printf ("\n");
     80  1.1.1.1.4.2  christos       goto clear_and_exit;
     81  1.1.1.1.4.2  christos     }
     82  1.1.1.1.4.2  christos 
     83  1.1.1.1.4.2  christos   mpfr_set_prec (x, 33);
     84  1.1.1.1.4.2  christos   mpfr_set_prec (y, 33);
     85  1.1.1.1.4.2  christos   mpfr_log_ui (x, 3, MPFR_RNDZ);
     86  1.1.1.1.4.2  christos   mpfr_set_str_binary (y, "1.0001100100111110101001111010101");
     87  1.1.1.1.4.2  christos   if (mpfr_cmp (x, y))
     88  1.1.1.1.4.2  christos     {
     89  1.1.1.1.4.2  christos       printf ("Error for log(3), prec=33, MPFR_RNDZ\n");
     90  1.1.1.1.4.2  christos       printf ("expected "); mpfr_dump (y);
     91  1.1.1.1.4.2  christos       printf ("got      "); mpfr_dump (x);
     92  1.1.1.1.4.2  christos       exit (1);
     93  1.1.1.1.4.2  christos     }
     94  1.1.1.1.4.2  christos 
     95  1.1.1.1.4.2  christos   mpfr_set_prec (x, 60);
     96  1.1.1.1.4.2  christos   mpfr_set_prec (y, 60);
     97  1.1.1.1.4.2  christos   mpfr_log_ui (x, 19, MPFR_RNDU);
     98  1.1.1.1.4.2  christos   mpfr_set_str_binary (y, "10.1111000111000110110000001100000010010110011001011000111010");
     99  1.1.1.1.4.2  christos   if (mpfr_cmp (x, y))
    100  1.1.1.1.4.2  christos     {
    101  1.1.1.1.4.2  christos       printf ("Error for log(19), prec=60, MPFR_RNDU\n");
    102  1.1.1.1.4.2  christos       printf ("expected "); mpfr_dump (y);
    103  1.1.1.1.4.2  christos       printf ("got      "); mpfr_dump (x);
    104  1.1.1.1.4.2  christos       exit (1);
    105  1.1.1.1.4.2  christos     }
    106  1.1.1.1.4.2  christos 
    107  1.1.1.1.4.2  christos   mpfr_clear_flags ();
    108  1.1.1.1.4.2  christos   inex = mpfr_log_ui (x, 0, MPFR_RNDN);
    109  1.1.1.1.4.2  christos   MPFR_ASSERTN (inex == 0);
    110  1.1.1.1.4.2  christos   MPFR_ASSERTN (mpfr_inf_p (x));
    111  1.1.1.1.4.2  christos   MPFR_ASSERTN (mpfr_sgn (x) < 0);
    112  1.1.1.1.4.2  christos   MPFR_ASSERTN (__gmpfr_flags == MPFR_FLAGS_DIVBY0);
    113  1.1.1.1.4.2  christos 
    114  1.1.1.1.4.2  christos   mpfr_clear_flags ();
    115  1.1.1.1.4.2  christos   inex = mpfr_log_ui (x, 1, MPFR_RNDN);
    116  1.1.1.1.4.2  christos   MPFR_ASSERTN (inex == 0);
    117  1.1.1.1.4.2  christos   MPFR_ASSERTN (mpfr_zero_p (x));
    118  1.1.1.1.4.2  christos   MPFR_ASSERTN (mpfr_signbit (x) == 0);
    119  1.1.1.1.4.2  christos   MPFR_ASSERTN (__gmpfr_flags == 0);
    120  1.1.1.1.4.2  christos 
    121  1.1.1.1.4.2  christos   for (prec = MPFR_PREC_MIN; prec <= 100; prec++)
    122  1.1.1.1.4.2  christos     {
    123  1.1.1.1.4.2  christos       mpfr_set_prec (x, prec);
    124  1.1.1.1.4.2  christos       mpfr_set_prec (z, prec);
    125  1.1.1.1.4.2  christos       mpfr_set_prec (t, prec);
    126  1.1.1.1.4.2  christos       yprec = prec + 20;
    127  1.1.1.1.4.2  christos       mpfr_set_prec (y, yprec);
    128  1.1.1.1.4.2  christos 
    129  1.1.1.1.4.2  christos       for (m = 2; m < 130; m++)
    130  1.1.1.1.4.2  christos         RND_LOOP (rnd)
    131  1.1.1.1.4.2  christos           {
    132  1.1.1.1.4.2  christos             /* Start with n = 2 to 49 (mpfr_can_round would fail for n < 2),
    133  1.1.1.1.4.2  christos                then around ULONG_MAX/3, then around LONG_MAX, then
    134  1.1.1.1.4.2  christos                ULONG_MAX down to ULONG_MAX-19. */
    135  1.1.1.1.4.2  christos             n = (m < 50 ? m :
    136  1.1.1.1.4.2  christos                  m < 80 ? ULONG_MAX/3 + m - 65 :
    137  1.1.1.1.4.2  christos                  m < 110 ? (unsigned long) LONG_MAX + m - 95 :
    138  1.1.1.1.4.2  christos                  ULONG_MAX - (m - 110));
    139  1.1.1.1.4.2  christos             inex = mpfr_set_ui (v, n, MPFR_RNDN);
    140  1.1.1.1.4.2  christos             MPFR_ASSERTN (inex == 0);
    141  1.1.1.1.4.2  christos             mpfr_log (y, v, MPFR_RNDN);
    142  1.1.1.1.4.2  christos             if (mpfr_can_round (y, yprec, MPFR_RNDN, MPFR_RNDZ, prec
    143  1.1.1.1.4.2  christos                                 + (rnd == MPFR_RNDN)))
    144  1.1.1.1.4.2  christos               {
    145  1.1.1.1.4.2  christos                 mpfr_set (t, y, (mpfr_rnd_t) rnd);
    146  1.1.1.1.4.2  christos                 for (i = 0; i <= 1; i++)
    147  1.1.1.1.4.2  christos                   {
    148  1.1.1.1.4.2  christos                     if (i)
    149  1.1.1.1.4.2  christos                       {
    150  1.1.1.1.4.2  christos                         mpfr_exp_t e;
    151  1.1.1.1.4.2  christos 
    152  1.1.1.1.4.2  christos                         if (MPFR_IS_SINGULAR (t))
    153  1.1.1.1.4.2  christos                           break;
    154  1.1.1.1.4.2  christos                         e = mpfr_get_exp (t);
    155  1.1.1.1.4.2  christos                         set_emin (e);
    156  1.1.1.1.4.2  christos                         set_emax (e);
    157  1.1.1.1.4.2  christos                       }
    158  1.1.1.1.4.2  christos                     TEST_FUNCTION (z, n, (mpfr_rnd_t) rnd);
    159  1.1.1.1.4.2  christos                     if (i)
    160  1.1.1.1.4.2  christos                       {
    161  1.1.1.1.4.2  christos                         set_emin (emin);
    162  1.1.1.1.4.2  christos                         set_emax (emax);
    163  1.1.1.1.4.2  christos                       }
    164  1.1.1.1.4.2  christos                     if (mpfr_cmp (t, z))
    165  1.1.1.1.4.2  christos                       {
    166  1.1.1.1.4.2  christos                         printf ("results differ for n = %lu, prec = %u,"
    167  1.1.1.1.4.2  christos                                 " %s%s\n", n, prec,
    168  1.1.1.1.4.2  christos                                 mpfr_print_rnd_mode ((mpfr_rnd_t) rnd),
    169  1.1.1.1.4.2  christos                                 i ? ", reduced exponent range" : "");
    170  1.1.1.1.4.2  christos                         printf ("  got      ");
    171  1.1.1.1.4.2  christos                         mpfr_dump (z);
    172  1.1.1.1.4.2  christos                         printf ("  expected ");
    173  1.1.1.1.4.2  christos                         mpfr_dump (t);
    174  1.1.1.1.4.2  christos                         printf ("  approx   ");
    175  1.1.1.1.4.2  christos                         mpfr_dump (y);
    176  1.1.1.1.4.2  christos                         exit (1);
    177  1.1.1.1.4.2  christos                       }
    178  1.1.1.1.4.2  christos                   }
    179  1.1.1.1.4.2  christos               }
    180  1.1.1.1.4.2  christos             else
    181  1.1.1.1.4.2  christos               {
    182  1.1.1.1.4.2  christos                 /* We are not doing random tests. The precision increase
    183  1.1.1.1.4.2  christos                    must have be chosen so that this case never occurs. */
    184  1.1.1.1.4.2  christos                 printf ("mpfr_can_round failed for n = %lu, prec = %u, %s\n",
    185  1.1.1.1.4.2  christos                         n, prec, mpfr_print_rnd_mode ((mpfr_rnd_t) rnd));
    186  1.1.1.1.4.2  christos                 exit (1);
    187  1.1.1.1.4.2  christos               }
    188  1.1.1.1.4.2  christos           }
    189  1.1.1.1.4.2  christos     }
    190  1.1.1.1.4.2  christos 
    191  1.1.1.1.4.2  christos 
    192  1.1.1.1.4.2  christos   test_generic (MPFR_PREC_MIN, 1000, 1);
    193  1.1.1.1.4.2  christos 
    194  1.1.1.1.4.2  christos   for (n = 1; n < ULONG_MAX / 3; n *= 3)
    195  1.1.1.1.4.2  christos     compare_with_log (n, 10);
    196  1.1.1.1.4.2  christos 
    197  1.1.1.1.4.2  christos  clear_and_exit:
    198  1.1.1.1.4.2  christos   mpfr_clears (x, y, z, t, v, (mpfr_ptr) 0);
    199  1.1.1.1.4.2  christos 
    200  1.1.1.1.4.2  christos   tests_end_mpfr ();
    201  1.1.1.1.4.2  christos   return 0;
    202  1.1.1.1.4.2  christos }
    203