(root)/
mpfr-4.2.1/
tests/
mpf_compat.h
       1  /* Test compatibility mpf-mpfr.
       2  
       3  Copyright 2003-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  #ifdef MPFR
      24  #include "mpf2mpfr.h"
      25  #endif
      26  
      27  int
      28  main (void)
      29  {
      30  
      31  #if defined (MPFR) && _MPFR_EXP_FORMAT != 3 /* because exp is a long below */
      32  
      33    return 77;
      34  
      35  #else
      36  
      37    unsigned long int prec, old_prec;
      38    unsigned long int prec2;
      39    mpf_t x, y;
      40    mpz_t z;
      41    mpq_t q;
      42    double d;
      43    signed long int exp;
      44    long l;
      45    unsigned long u;
      46    char *s;
      47    int i;
      48    FILE *f;
      49    gmp_randstate_t state;
      50  
      51    tests_start_mpfr ();
      52  
      53    /* Initialization Functions */
      54    prec = 53;
      55    mpf_set_default_prec (prec);
      56    prec2 = mpf_get_default_prec ();
      57    if (prec2 < prec)
      58      {
      59        printf ("Error in get_default_prec: %lu < %lu\n", prec2, prec);
      60        exit (1);
      61      }
      62  
      63    mpf_init (y);
      64  
      65    mpf_init2 (x, prec);
      66    prec2 = mpf_get_prec (x);
      67    if (prec2 < prec)
      68      {
      69        printf ("Error in get_prec: %lu < %lu\n", prec2, prec);
      70        mpf_clear (y);
      71        mpf_clear (x);
      72        exit (1);
      73      }
      74  
      75    mpf_set_prec (x, 2 * prec);
      76    prec2 = mpf_get_prec (x);
      77    if (prec2 < 2 * prec)
      78      {
      79        printf ("Error in set_prec: %lu < %lu\n", prec2, 2 * prec);
      80        mpf_clear (y);
      81        mpf_clear (x);
      82        exit (1);
      83      }
      84  
      85    old_prec = mpf_get_prec (x);
      86    mpf_set_prec_raw (x, prec);
      87    prec2 = mpf_get_prec (x);
      88    if (prec2 < prec)
      89      {
      90        printf ("Error in set_prec_raw: %lu < %lu\n", prec2, prec);
      91        mpf_clear (y);
      92        mpf_clear (x);
      93        exit (1);
      94      }
      95    mpf_set_prec_raw (x, old_prec);  /* needed with MPF (see GMP manual) */
      96  
      97    /* Assignment Functions */
      98  
      99    mpf_set (y, x);
     100    mpf_set_ui (x, 1);
     101    mpf_set_si (x, -1);
     102    mpf_set_d (x, 1.0);
     103  
     104    mpz_init_set_ui (z, 17);
     105    mpf_set_z (x, z);
     106    mpz_clear (z);
     107  
     108    mpq_init (q);
     109    mpq_set_ui (q, 2, 3);
     110    mpf_set_q (x, q);
     111    mpq_clear (q);
     112  
     113    mpf_set_str (x, "31415e-3", 10);
     114    mpf_swap (x, y);
     115  
     116    /* Combined Initialization and Assignment Functions */
     117  
     118    mpf_clear (x);
     119    mpf_init_set (x, y);
     120    mpf_clear (x);
     121    mpf_init_set_ui (x, 17);
     122    mpf_clear (x);
     123    mpf_init_set_si (x, -17);
     124    mpf_clear (x);
     125    mpf_init_set_d (x, 17.0);
     126    mpf_clear (x);
     127    mpf_init_set_str (x, "31415e-3", 10);
     128  
     129    /* Conversion Functions */
     130  
     131    d = mpf_get_d (x);
     132    d = mpf_get_d_2exp (&exp, x);
     133    l = mpf_get_si (x);
     134    u = mpf_get_ui (x);
     135    s = mpf_get_str (NULL, &exp, 10, 10, x);
     136    /* MPF doesn't have mpf_free_str */
     137    mpfr_free_str (s);
     138  
     139    /* Use d, l and u to avoid a warning with -Wunused-but-set-variable
     140       from GCC 4.6. The variables above were mainly used for prototype
     141       checking. */
     142    (void) d;  (void) l;  (void)  u;
     143  
     144    /* Arithmetic Functions */
     145  
     146    mpf_add (y, x, x);
     147    mpf_add_ui (y, x, 1);
     148    mpf_sub (y, x, x);
     149    mpf_ui_sub (y, 1, x);
     150    mpf_sub_ui (y, x, 1);
     151    mpf_mul (y, x, x);
     152    mpf_mul_ui (y, x, 17);
     153    mpf_div (y, x, x);
     154    mpf_ui_div (y, 17, x);
     155    mpf_div_ui (y, x, 17);
     156    mpf_sqrt (y, x);
     157    mpf_sqrt_ui (y, 17);
     158    mpf_pow_ui (y, x, 2);
     159    mpf_neg (y, x);
     160    mpf_abs (y, x);
     161    mpf_mul_2exp (y, x, 17);
     162    mpf_div_2exp (y, x, 17);
     163  
     164    /* Comparison Functions */
     165  
     166    i = mpf_cmp (y, x);
     167    i = mpf_cmp_d (y, 1.7);
     168    i = mpf_cmp_ui (y, 17);
     169    i = mpf_cmp_si (y, -17);
     170    i = mpf_eq (y, x, 17);
     171    mpf_reldiff (y, y, x);
     172    i = mpf_sgn (x);
     173  
     174    /* Input and Output Functions */
     175  
     176    f = fopen ("/dev/null", "w");
     177    if (f != NULL)
     178      {
     179        mpf_out_str (f, 10, 10, x);
     180        fclose (f);
     181      }
     182  
     183    mpf_set_prec (x, 15);
     184    mpf_set_prec (y, 15);
     185  
     186    f = src_fopen ("inp_str.dat", "r");
     187    if (f == NULL)
     188      {
     189        printf ("cannot open file \"inp_str.dat\"\n");
     190        exit (1);
     191      }
     192    i = mpf_inp_str (x, f, 10);
     193    if (i == 0 || mpf_cmp_si (x, -1700))
     194      {
     195        printf ("Error in reading 1st line from file \"inp_str.dat\"\n");
     196        exit (1);
     197      }
     198    fclose (f);
     199  
     200    /* Miscellaneous Functions */
     201  
     202    mpf_ceil (y, x);
     203    mpf_floor (y, x);
     204    mpf_trunc (y, x);
     205  
     206    i = mpf_integer_p (x);
     207  
     208    i = mpf_fits_ulong_p (x);
     209    i = mpf_fits_slong_p (x);
     210    i = mpf_fits_uint_p (x);
     211    i = mpf_fits_sint_p (x);
     212    i = mpf_fits_ushort_p (x);
     213    i = mpf_fits_sshort_p (x);
     214  
     215    gmp_randinit_lc_2exp_size (state, 128);
     216    mpf_urandomb (x, state, 10);
     217    gmp_randclear (state);
     218  
     219    /* Conversion to mpz */
     220    mpz_init (z);
     221    mpf_set_ui (x, 17);
     222    mpz_set_f (z, x);
     223    mpf_set_z (x, z);
     224    mpz_clear (z);
     225    if (mpf_cmp_ui (x, 17) != 0)
     226      {
     227        printf ("Error in conversion to/from mpz\n");
     228        printf ("expected 17, got %1.16e\n", mpf_get_d (x));
     229        exit (1);
     230      }
     231  
     232    /* non-regression tests for bugs fixed in revision 11565 */
     233    mpf_set_si (x, -1);
     234    MPFR_ASSERTN(mpf_fits_ulong_p (x) == 0);
     235    MPFR_ASSERTN(mpf_fits_slong_p (x) != 0);
     236    MPFR_ASSERTN(mpf_fits_uint_p (x) == 0);
     237    MPFR_ASSERTN(mpf_fits_sint_p (x) != 0);
     238    MPFR_ASSERTN(mpf_fits_ushort_p (x) == 0);
     239    MPFR_ASSERTN(mpf_fits_sshort_p (x) != 0);
     240    MPFR_ASSERTN(mpf_get_si (x) == -1);
     241  
     242    /* clear all variables */
     243    mpf_clear (y);
     244    mpf_clear (x);
     245  
     246    tests_end_mpfr ();
     247    return 0;
     248  
     249  #endif
     250  
     251  }