(root)/
mpfr-4.2.1/
tests/
tset_exp.c
       1  /* Test file for mpfr_get_exp and mpfr_set_exp.
       2  
       3  Copyright 2004, 2006-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  int
      26  main (int argc, char *argv[])
      27  {
      28    mpfr_t x;
      29    int ret;
      30    mpfr_exp_t emin, emax, e;
      31    int i = 0;
      32  
      33    tests_start_mpfr ();
      34  
      35    emin = mpfr_get_emin ();
      36    emax = mpfr_get_emax ();
      37  
      38    mpfr_init2 (x, 53);
      39  
      40    mpfr_set_ui (x, 1, MPFR_RNDN);
      41    ret = mpfr_set_exp (x, 2);
      42    MPFR_ASSERTN (ret == 0 && mpfr_cmp_ui (x, 2) == 0);
      43    e = mpfr_get_exp (x);
      44    MPFR_ASSERTN (e == 2);
      45    e = (mpfr_get_exp) (x);
      46    MPFR_ASSERTN (e == 2);
      47  
      48    ret = mpfr_set_exp (x, emax);
      49    e = mpfr_get_exp (x);
      50    MPFR_ASSERTN (e == emax);
      51    e = (mpfr_get_exp) (x);
      52    MPFR_ASSERTN (e == emax);
      53  
      54    ret = mpfr_set_exp (x, emin);
      55    e = mpfr_get_exp (x);
      56    MPFR_ASSERTN (e == emin);
      57    e = (mpfr_get_exp) (x);
      58    MPFR_ASSERTN (e == emin);
      59  
      60    set_emin (-1);
      61  
      62    e = mpfr_get_exp (x);
      63    MPFR_ASSERTN (e == emin);
      64    e = (mpfr_get_exp) (x);
      65    MPFR_ASSERTN (e == emin);
      66  
      67  #ifdef IGNORE_CPP_COMPAT
      68  #pragma GCC diagnostic push
      69  #pragma GCC diagnostic ignored "-Wc++-compat"
      70  #endif
      71    e = mpfr_get_exp ((i++, VOIDP_CAST(x)));
      72  #ifdef IGNORE_CPP_COMPAT
      73  #pragma GCC diagnostic pop
      74  #endif
      75    MPFR_ASSERTN (e == emin);
      76    MPFR_ASSERTN (i == 1);
      77  
      78    ret = mpfr_set_exp (x, -1);
      79    MPFR_ASSERTN (ret == 0 && mpfr_cmp_ui_2exp (x, 1, -2) == 0);
      80  
      81    set_emax (1);
      82    ret = mpfr_set_exp (x, 1);
      83    MPFR_ASSERTN (ret == 0 && mpfr_cmp_ui (x, 1) == 0);
      84  
      85    ret = mpfr_set_exp (x, -2);
      86    MPFR_ASSERTN (ret != 0 && mpfr_cmp_ui (x, 1) == 0);
      87  
      88    ret = mpfr_set_exp (x, 2);
      89    MPFR_ASSERTN (ret != 0 && mpfr_cmp_ui (x, 1) == 0);
      90  
      91    mpfr_clear (x);
      92  
      93    set_emin (emin);
      94    set_emax (emax);
      95  
      96    tests_end_mpfr ();
      97    return 0;
      98  }