(root)/
mpfr-4.2.1/
tests/
tisnan.c
       1  /* Test file for mpfr_nan_p, mpfr_inf_p, mpfr_number_p, mpfr_zero_p and
       2     mpfr_regular_p.
       3  
       4  Copyright 2001-2004, 2006-2023 Free Software Foundation, Inc.
       5  Contributed by the AriC and Caramba projects, INRIA.
       6  
       7  This file is part of the GNU MPFR Library.
       8  
       9  The GNU MPFR Library is free software; you can redistribute it and/or modify
      10  it under the terms of the GNU Lesser General Public License as published by
      11  the Free Software Foundation; either version 3 of the License, or (at your
      12  option) any later version.
      13  
      14  The GNU MPFR Library is distributed in the hope that it will be useful, but
      15  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
      16  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
      17  License for more details.
      18  
      19  You should have received a copy of the GNU Lesser General Public License
      20  along with the GNU MPFR Library; see the file COPYING.LESSER.  If not, see
      21  https://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
      22  51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
      23  
      24  #include "mpfr-test.h"
      25  
      26  int
      27  main (void)
      28  {
      29    mpfr_t  x;
      30    int i = 0, j = 0;
      31  
      32    /* We need to check that when the function is implemented by a macro,
      33       it behaves correctly. */
      34  #define ARG     (i++, VOIDP_CAST(x))
      35  #define CHECK   MPFR_ASSERTN (i == ++j)
      36  
      37    tests_start_mpfr ();
      38  
      39    mpfr_init (x);
      40  
      41  #if 0
      42    /* The following should yield a compilation error when the functions
      43       are implemented as macros. Change 0 to 1 above in order to test. */
      44    (void) (mpfr_nan_p (1L));
      45    (void) (mpfr_inf_p (1L));
      46    (void) (mpfr_number_p (1L));
      47    (void) (mpfr_zero_p (1L));
      48    (void) (mpfr_regular_p (1L));
      49  #endif
      50  
      51  #ifdef IGNORE_CPP_COMPAT
      52  #pragma GCC diagnostic push
      53  #pragma GCC diagnostic ignored "-Wc++-compat"
      54  #endif
      55  
      56    /* check +infinity gives non-zero for mpfr_inf_p only */
      57    mpfr_set_ui (x, 1L, MPFR_RNDZ);
      58    mpfr_div_ui (x, x, 0L, MPFR_RNDZ);
      59    if (mpfr_nan_p (x) || (mpfr_nan_p) (x) || mpfr_nan_p (ARG))
      60      {
      61        printf ("Error: mpfr_nan_p(+Inf) gives non-zero\n");
      62        exit (1);
      63      }
      64    CHECK;
      65    if (!mpfr_inf_p (x) || !(mpfr_inf_p) (x) || !mpfr_inf_p (ARG))
      66      {
      67        printf ("Error: mpfr_inf_p(+Inf) gives zero\n");
      68        exit (1);
      69      }
      70    CHECK;
      71    if (mpfr_number_p (x) || (mpfr_number_p) (x) || mpfr_number_p (ARG))
      72      {
      73        printf ("Error: mpfr_number_p(+Inf) gives non-zero\n");
      74        exit (1);
      75      }
      76    CHECK;
      77    if (mpfr_zero_p (x) || (mpfr_zero_p) (x) || mpfr_zero_p (ARG))
      78      {
      79        printf ("Error: mpfr_zero_p(+Inf) gives non-zero\n");
      80        exit (1);
      81      }
      82    CHECK;
      83    if (mpfr_regular_p (x) || (mpfr_regular_p) (x) || mpfr_regular_p (ARG))
      84      {
      85        printf ("Error: mpfr_regular_p(+Inf) gives non-zero\n");
      86        exit (1);
      87      }
      88    CHECK;
      89  
      90    /* same for -Inf */
      91    mpfr_neg (x, x, MPFR_RNDN);
      92    if (mpfr_nan_p (x) || (mpfr_nan_p) (x) || mpfr_nan_p (ARG))
      93      {
      94        printf ("Error: mpfr_nan_p(-Inf) gives non-zero\n");
      95        exit (1);
      96      }
      97    CHECK;
      98    if (!mpfr_inf_p (x) || !(mpfr_inf_p) (x) || !mpfr_inf_p (ARG))
      99      {
     100        printf ("Error: mpfr_inf_p(-Inf) gives zero\n");
     101        exit (1);
     102      }
     103    CHECK;
     104    if (mpfr_number_p (x) || (mpfr_number_p) (x) || mpfr_number_p (ARG))
     105      {
     106        printf ("Error: mpfr_number_p(-Inf) gives non-zero\n");
     107        exit (1);
     108      }
     109    CHECK;
     110    if (mpfr_zero_p (x) || (mpfr_zero_p) (x) || mpfr_zero_p (ARG))
     111      {
     112        printf ("Error: mpfr_zero_p(-Inf) gives non-zero\n");
     113        exit (1);
     114      }
     115    CHECK;
     116    if (mpfr_regular_p (x) || (mpfr_regular_p) (x) || mpfr_regular_p (ARG))
     117      {
     118        printf ("Error: mpfr_regular_p(-Inf) gives non-zero\n");
     119        exit (1);
     120      }
     121    CHECK;
     122  
     123    /* same for NaN */
     124    mpfr_sub (x, x, x, MPFR_RNDN);
     125    if (!mpfr_nan_p (x) || !(mpfr_nan_p) (x) || !mpfr_nan_p (ARG))
     126      {
     127        printf ("Error: mpfr_nan_p(NaN) gives zero\n");
     128        exit (1);
     129      }
     130    CHECK;
     131    if (mpfr_inf_p (x) || (mpfr_inf_p) (x) || mpfr_inf_p (ARG))
     132      {
     133        printf ("Error: mpfr_inf_p(NaN) gives non-zero\n");
     134        exit (1);
     135      }
     136    CHECK;
     137    if (mpfr_number_p (x) || (mpfr_number_p) (x) || mpfr_number_p (ARG))
     138      {
     139        printf ("Error: mpfr_number_p(NaN) gives non-zero\n");
     140        exit (1);
     141      }
     142    CHECK;
     143    if (mpfr_zero_p (x) || (mpfr_zero_p) (x) || mpfr_zero_p (ARG))
     144      {
     145        printf ("Error: mpfr_number_p(NaN) gives non-zero\n");
     146        exit (1);
     147      }
     148    CHECK;
     149    if (mpfr_regular_p (x) || (mpfr_regular_p) (x) || mpfr_regular_p (ARG))
     150      {
     151        printf ("Error: mpfr_regular_p(NaN) gives non-zero\n");
     152        exit (1);
     153      }
     154    CHECK;
     155  
     156    /* same for a regular number */
     157    mpfr_set_ui (x, 1, MPFR_RNDN);
     158    if (mpfr_nan_p (x) || (mpfr_nan_p) (x) || mpfr_nan_p (ARG))
     159      {
     160        printf ("Error: mpfr_nan_p(1) gives non-zero\n");
     161        exit (1);
     162      }
     163    CHECK;
     164    if (mpfr_inf_p (x) || (mpfr_inf_p) (x) || mpfr_inf_p (ARG))
     165      {
     166        printf ("Error: mpfr_inf_p(1) gives non-zero\n");
     167        exit (1);
     168      }
     169    CHECK;
     170    if (!mpfr_number_p (x) || !(mpfr_number_p) (x) || !mpfr_number_p (ARG))
     171      {
     172        printf ("Error: mpfr_number_p(1) gives zero\n");
     173        exit (1);
     174      }
     175    CHECK;
     176    if (mpfr_zero_p (x) || (mpfr_zero_p) (x) || mpfr_zero_p (ARG))
     177      {
     178        printf ("Error: mpfr_zero_p(1) gives non-zero\n");
     179        exit (1);
     180      }
     181    CHECK;
     182    if (!mpfr_regular_p (x) || !(mpfr_regular_p) (x) || !mpfr_regular_p (ARG))
     183      {
     184        printf ("Error: mpfr_regular_p(1) gives zero\n");
     185        exit (1);
     186      }
     187    CHECK;
     188  
     189    /* Same for +0 */
     190    mpfr_set_ui (x, 0, MPFR_RNDN);
     191    if (mpfr_nan_p (x) || (mpfr_nan_p) (x) || mpfr_nan_p (ARG))
     192      {
     193        printf ("Error: mpfr_nan_p(+0) gives non-zero\n");
     194        exit (1);
     195      }
     196    CHECK;
     197    if (mpfr_inf_p (x) || (mpfr_inf_p) (x) || mpfr_inf_p (ARG))
     198      {
     199        printf ("Error: mpfr_inf_p(+0) gives non-zero\n");
     200        exit (1);
     201      }
     202    CHECK;
     203    if (!mpfr_number_p (x) || !(mpfr_number_p) (x) || !mpfr_number_p (ARG))
     204      {
     205        printf ("Error: mpfr_number_p(+0) gives zero\n");
     206        exit (1);
     207      }
     208    CHECK;
     209    if (!mpfr_zero_p (x) || !(mpfr_zero_p) (x) || !mpfr_zero_p (ARG))
     210      {
     211        printf ("Error: mpfr_zero_p(+0) gives zero\n");
     212        exit (1);
     213      }
     214    CHECK;
     215    if (mpfr_regular_p (x) || (mpfr_regular_p) (x) || mpfr_regular_p (ARG))
     216      {
     217        printf ("Error: mpfr_regular_p(+0) gives non-zero\n");
     218        exit (1);
     219      }
     220    CHECK;
     221  
     222    /* Same for -0 */
     223    mpfr_set_ui (x, 0, MPFR_RNDN);
     224    mpfr_neg (x, x, MPFR_RNDN);
     225    if (mpfr_nan_p (x) || (mpfr_nan_p) (x) || mpfr_nan_p (ARG))
     226      {
     227        printf ("Error: mpfr_nan_p(-0) gives non-zero\n");
     228        exit (1);
     229      }
     230    CHECK;
     231    if (mpfr_inf_p (x) || (mpfr_inf_p) (x) || mpfr_inf_p (ARG))
     232      {
     233        printf ("Error: mpfr_inf_p(-0) gives non-zero\n");
     234        exit (1);
     235      }
     236    CHECK;
     237    if (!mpfr_number_p (x) || !(mpfr_number_p) (x) || !mpfr_number_p (ARG))
     238      {
     239        printf ("Error: mpfr_number_p(-0) gives zero\n");
     240        exit (1);
     241      }
     242    CHECK;
     243    if (!mpfr_zero_p (x) || !(mpfr_zero_p) (x) || !mpfr_zero_p (ARG))
     244      {
     245        printf ("Error: mpfr_zero_p(-0) gives zero\n");
     246        exit (1);
     247      }
     248    CHECK;
     249    if (mpfr_regular_p (x) || (mpfr_regular_p) (x) || mpfr_regular_p (ARG))
     250      {
     251        printf ("Error: mpfr_regular_p(-0) gives non-zero\n");
     252        exit (1);
     253      }
     254    CHECK;
     255  
     256  #ifdef IGNORE_CPP_COMPAT
     257  #pragma GCC diagnostic pop
     258  #endif
     259  
     260    mpfr_clear (x);
     261  
     262    tests_end_mpfr ();
     263    return 0;
     264  }