(root)/
mpfr-4.2.1/
tests/
teint.c
       1  /* Test file for mpfr_eint.
       2  
       3  Copyright 2005-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  #define TEST_FUNCTION mpfr_eint
      26  #include "tgeneric.c"
      27  
      28  static void
      29  check_specials (void)
      30  {
      31    mpfr_t  x, y;
      32  
      33    mpfr_init2 (x, 123L);
      34    mpfr_init2 (y, 123L);
      35  
      36    mpfr_set_nan (x);
      37    mpfr_eint (y, x, MPFR_RNDN);
      38    if (! mpfr_nan_p (y))
      39      {
      40        printf ("Error: eint(NaN) != NaN\n");
      41        exit (1);
      42      }
      43  
      44    mpfr_set_inf (x, 1);
      45    mpfr_eint (y, x, MPFR_RNDN);
      46    if (! (mpfr_inf_p (y) && mpfr_sgn (y) > 0))
      47      {
      48        printf ("Error: eint(+Inf) != +Inf\n");
      49        exit (1);
      50      }
      51  
      52    mpfr_set_inf (x, -1);
      53    mpfr_eint (y, x, MPFR_RNDN);
      54    if (! (mpfr_zero_p (y) && MPFR_IS_NEG (y)))
      55      {
      56        printf ("Error: eint(-Inf) != -0\n");
      57        exit (1);
      58      }
      59  
      60    /* eint(+/-0) = -Inf */
      61    mpfr_set_ui (x, 0, MPFR_RNDN);
      62    mpfr_eint (y, x, MPFR_RNDN);
      63    if (! (mpfr_inf_p (y) && mpfr_sgn (y) < 0))
      64      {
      65        printf ("Error: eint(+0) != -Inf\n");
      66        exit (1);
      67      }
      68    mpfr_neg (x, x, MPFR_RNDN);
      69    mpfr_eint (y, x, MPFR_RNDN);
      70    if (! (mpfr_inf_p (y) && mpfr_sgn (y) < 0))
      71      {
      72        printf ("Error: eint(-0) != -Inf\n");
      73        exit (1);
      74      }
      75  
      76    mpfr_set_prec (x, 17);
      77    mpfr_set_prec (y, 17);
      78    mpfr_set_str_binary (x, "1.0111110100100110e-2");
      79    mpfr_set_str_binary (y, "-1.0010101001110100e-10");
      80    mpfr_eint (x, x, MPFR_RNDZ);
      81    if (mpfr_cmp (x, y))
      82      {
      83        printf ("Error for x=1.0111110100100110e-2, MPFR_RNDZ\n");
      84        printf ("expected "); mpfr_dump (y);
      85        printf ("got      "); mpfr_dump (x);
      86        exit (1);
      87      }
      88  
      89    mpfr_set_prec (x, 53);
      90    mpfr_set_prec (y, 53);
      91    mpfr_set_str_binary (x, "0.10E4");
      92    mpfr_eint (x, x, MPFR_RNDN);
      93    mpfr_set_str (y, "440.37989953483827", 10, MPFR_RNDN);
      94    if (mpfr_cmp (x, y) != 0)
      95      {
      96        printf ("Error for x=0.10E4, MPFR_RNDZ\n");
      97        printf ("expected "); mpfr_dump (y);
      98        printf ("got      "); mpfr_dump (x);
      99        exit (1);
     100      }
     101  
     102    mpfr_set_prec (x, 63);
     103    mpfr_set_prec (y, 63);
     104    mpfr_set_str_binary (x, "1.01111101011100111000011010001000101101011000011001111101011010e-2");
     105    mpfr_eint (x, x, MPFR_RNDZ);
     106    mpfr_set_str_binary (y, "1.11010110001101000001010010000100001111001000100100000001011100e-17");
     107    if (mpfr_cmp (x, y) != 0)
     108      {
     109        printf ("Error (1) for MPFR_RNDZ\n");
     110        printf ("expected "); mpfr_dump (y);
     111        printf ("got      "); mpfr_dump (x);
     112        exit (1);
     113      }
     114  
     115    mpfr_set_prec (x, 2);
     116    mpfr_set_prec (y, 2);
     117    mpfr_set_si (x, -1, MPFR_RNDN);
     118    mpfr_eint (x, x, MPFR_RNDN); /* eint1(1) = 0.219383934395520 */
     119    mpfr_set_str_binary (y, "-1e-2");
     120    if (mpfr_cmp (x, y) != 0)
     121      {
     122        printf ("Error for x=-1, MPFR_RNDN\n");
     123        printf ("expected "); mpfr_dump (y);
     124        printf ("got      "); mpfr_dump (x);
     125        exit (1);
     126      }
     127  
     128    mpfr_set_prec (x, 10);
     129    mpfr_set_prec (y, 10);
     130    mpfr_set_si (x, -2, MPFR_RNDN);
     131    mpfr_eint (y, x, MPFR_RNDN); /* eint1(2) = 0.0489005107080611 */
     132    if (mpfr_cmp_si_2exp (y, -801, -14) != 0)
     133      {
     134        printf ("Error for x=-2, MPFR_RNDN\n");
     135        printf ("expected -801/2^14\n");
     136        printf ("got      "); mpfr_dump (y);
     137        exit (1);
     138      }
     139  
     140    /* check large x */
     141    mpfr_set_prec (x, 53);
     142    mpfr_set_prec (y, 53);
     143    mpfr_set_str_binary (x, "1E6");
     144    mpfr_eint (x, x, MPFR_RNDN);
     145    mpfr_set_str_binary (y, "10100011110001101001110000110010111000100111010001E37");
     146    if (mpfr_cmp (x, y) != 0)
     147      {
     148        printf ("Error for x=2^6, MPFR_RNDN\n");
     149        printf ("expected "); mpfr_dump (y);
     150        printf ("got      "); mpfr_dump (x);
     151        exit (1);
     152      }
     153    mpfr_set_str_binary (x, "1E7");
     154    mpfr_eint (x, x, MPFR_RNDN);
     155    mpfr_set_str_binary (y, "11001100100011110000101001011010110111111011110011E128");
     156    if (mpfr_cmp (x, y) != 0)
     157      {
     158        printf ("Error for x=2^7, MPFR_RNDN\n");
     159        printf ("expected "); mpfr_dump (y);
     160        printf ("got      "); mpfr_dump (x);
     161        exit (1);
     162      }
     163    mpfr_set_str_binary (x, "1E8");
     164    mpfr_eint (x, x, MPFR_RNDN);
     165    mpfr_set_str_binary (y, "1010000110000101111111011011000101001000101011101001E310");
     166    if (mpfr_cmp (x, y) != 0)
     167      {
     168        printf ("Error for x=2^8, MPFR_RNDN\n");
     169        printf ("expected "); mpfr_dump (y);
     170        printf ("got      "); mpfr_dump (x);
     171        exit (1);
     172      }
     173    mpfr_set_str_binary (x, "1E9");
     174    mpfr_eint (x, x, MPFR_RNDN);
     175    mpfr_set_str_binary (y, "11001010101000001010101101110000010110011101110010101E677");
     176    if (mpfr_cmp (x, y) != 0)
     177      {
     178        printf ("Error for x=2^9, MPFR_RNDN\n");
     179        printf ("expected "); mpfr_dump (y);
     180        printf ("got      "); mpfr_dump (x);
     181        exit (1);
     182      }
     183    mpfr_set_str_binary (x, "1E10");
     184    mpfr_eint (x, x, MPFR_RNDN);
     185    mpfr_set_str_binary (y, "10011111111010010110110101101000101100101010101101101E1415");
     186    if (mpfr_cmp (x, y) != 0)
     187      {
     188        printf ("Error for x=2^10, MPFR_RNDN\n");
     189        printf ("expected "); mpfr_dump (y);
     190        printf ("got      "); mpfr_dump (x);
     191        exit (1);
     192      }
     193  
     194    /* Run-time error at si_op.c:42:31 with r9996 on an x86-64 Linux machine,
     195       using CFLAGS="-fsanitize=undefined -fno-sanitize-recover". */
     196    mpfr_set_prec (x, 32);
     197    mpfr_set_prec (y, 46);
     198    mpfr_set_si_2exp (x, -1, -1, MPFR_RNDN);
     199    mpfr_eint (y, x, MPFR_RNDN);
     200  
     201    mpfr_set_prec (x, 10);
     202    mpfr_set_prec (y, 6);
     203    mpfr_set_str (x, "-7.875", 10, MPFR_RNDN);
     204    mpfr_eint (y, x, MPFR_RNDN);
     205    if (mpfr_cmp_si_2exp (y, -45, -20) != 0)
     206      {
     207        printf ("Error for x=-7.875, MPFR_RNDN\n");
     208        printf ("expected -45/2^20\n");
     209        printf ("got      "); mpfr_dump (y);
     210        exit (1);
     211      }
     212  
     213    mpfr_clear (x);
     214    mpfr_clear (y);
     215  }
     216  
     217  int
     218  main (int argc, char *argv[])
     219  {
     220    tests_start_mpfr ();
     221  
     222    if (argc != 1) /* teint x [prec] */
     223      {
     224        mpfr_t x;
     225        mpfr_prec_t p;
     226        p = (argc < 3) ? 53 : atoi (argv[2]);
     227        mpfr_init2 (x, p);
     228        mpfr_set_str (x, argv[1], 10, MPFR_RNDN);
     229        printf ("eint(");
     230        mpfr_out_str (stdout, 10, 0, x, MPFR_RNDN);
     231        printf (")=");
     232        mpfr_eint (x, x, MPFR_RNDN);
     233        mpfr_out_str (stdout, 10, 0, x, MPFR_RNDN);
     234        printf ("\n");
     235        mpfr_clear (x);
     236      }
     237    else
     238      {
     239        check_specials ();
     240  
     241        test_generic (MPFR_PREC_MIN, 100, 100);
     242      }
     243  
     244    tests_end_mpfr ();
     245    return 0;
     246  }