Home | History | Annotate | Line # | Download | only in tests
      1      1.1  mrg /* Test file for mpfr_div_d
      2      1.1  mrg 
      3  1.1.1.6  mrg Copyright 2007-2023 Free Software Foundation, Inc.
      4  1.1.1.3  mrg Contributed by the AriC and Caramba projects, INRIA.
      5      1.1  mrg 
      6      1.1  mrg This file is part of the GNU MPFR Library.
      7      1.1  mrg 
      8      1.1  mrg The GNU MPFR Library is free software; you can redistribute it and/or modify
      9      1.1  mrg it under the terms of the GNU Lesser General Public License as published by
     10      1.1  mrg the Free Software Foundation; either version 3 of the License, or (at your
     11      1.1  mrg option) any later version.
     12      1.1  mrg 
     13      1.1  mrg The GNU MPFR Library is distributed in the hope that it will be useful, but
     14      1.1  mrg WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
     15      1.1  mrg or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
     16      1.1  mrg License for more details.
     17      1.1  mrg 
     18      1.1  mrg You should have received a copy of the GNU Lesser General Public License
     19      1.1  mrg along with the GNU MPFR Library; see the file COPYING.LESSER.  If not, see
     20  1.1.1.5  mrg https://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
     21      1.1  mrg 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
     22      1.1  mrg 
     23      1.1  mrg #include <float.h>
     24      1.1  mrg 
     25      1.1  mrg #include "mpfr-test.h"
     26      1.1  mrg 
     27      1.1  mrg static void
     28      1.1  mrg check_nans (void)
     29      1.1  mrg {
     30      1.1  mrg   mpfr_t  x, y;
     31      1.1  mrg   int inexact;
     32      1.1  mrg 
     33      1.1  mrg   mpfr_init2 (x, 123);
     34      1.1  mrg   mpfr_init2 (y, 123);
     35      1.1  mrg 
     36      1.1  mrg   /* nan / 1.0 is nan */
     37      1.1  mrg   mpfr_set_nan (x);
     38      1.1  mrg   mpfr_clear_flags ();
     39      1.1  mrg   inexact = mpfr_div_d (y, x, 1.0, MPFR_RNDN);
     40      1.1  mrg   MPFR_ASSERTN (inexact == 0);
     41  1.1.1.4  mrg   MPFR_ASSERTN (__gmpfr_flags == MPFR_FLAGS_NAN);
     42      1.1  mrg   MPFR_ASSERTN (mpfr_nan_p (y));
     43      1.1  mrg 
     44      1.1  mrg   /* +inf / 1.0 == +inf */
     45      1.1  mrg   mpfr_set_inf (x, 1);
     46      1.1  mrg   mpfr_clear_flags ();
     47      1.1  mrg   inexact = mpfr_div_d (y, x, 1.0, MPFR_RNDN);
     48      1.1  mrg   MPFR_ASSERTN (inexact == 0);
     49      1.1  mrg   MPFR_ASSERTN (__gmpfr_flags == 0);
     50      1.1  mrg   MPFR_ASSERTN (mpfr_inf_p (y));
     51      1.1  mrg   MPFR_ASSERTN (MPFR_IS_POS (y));
     52      1.1  mrg 
     53      1.1  mrg   /* -inf / 1.0 == -inf */
     54      1.1  mrg   mpfr_set_inf (x, -1);
     55      1.1  mrg   mpfr_clear_flags ();
     56      1.1  mrg   inexact = mpfr_div_d (y, x, 1.0, MPFR_RNDN);
     57      1.1  mrg   MPFR_ASSERTN (inexact == 0);
     58      1.1  mrg   MPFR_ASSERTN (__gmpfr_flags == 0);
     59      1.1  mrg   MPFR_ASSERTN (mpfr_inf_p (y));
     60      1.1  mrg   MPFR_ASSERTN (MPFR_IS_NEG (y));
     61      1.1  mrg 
     62      1.1  mrg   /* 0.0 / 0.0 is nan */
     63  1.1.1.2  mrg   mpfr_set_ui (x, 0, MPFR_RNDN);
     64      1.1  mrg   mpfr_clear_flags ();
     65      1.1  mrg   inexact = mpfr_div_d (y, x, 0.0, MPFR_RNDN);
     66      1.1  mrg   MPFR_ASSERTN (inexact == 0);
     67  1.1.1.2  mrg   MPFR_ASSERTN (__gmpfr_flags == MPFR_FLAGS_NAN);
     68      1.1  mrg   MPFR_ASSERTN (mpfr_nan_p (y));
     69      1.1  mrg 
     70  1.1.1.2  mrg   /* 1.0 / 0.0 == +inf */
     71  1.1.1.2  mrg   mpfr_set_ui (x, 1, MPFR_RNDN);
     72  1.1.1.2  mrg   mpfr_clear_flags ();
     73  1.1.1.2  mrg   inexact = mpfr_div_d (y, x, 0.0, MPFR_RNDN);
     74  1.1.1.2  mrg   MPFR_ASSERTN (inexact == 0);
     75  1.1.1.2  mrg   MPFR_ASSERTN (__gmpfr_flags == MPFR_FLAGS_DIVBY0);
     76  1.1.1.2  mrg   MPFR_ASSERTN (mpfr_inf_p (y));
     77  1.1.1.2  mrg   MPFR_ASSERTN (MPFR_IS_POS (y));
     78  1.1.1.2  mrg 
     79  1.1.1.2  mrg   /* -1.0 / 0.0 == -inf */
     80  1.1.1.2  mrg   mpfr_set_si (x, -1, MPFR_RNDN);
     81  1.1.1.2  mrg   mpfr_clear_flags ();
     82  1.1.1.2  mrg   inexact = mpfr_div_d (y, x, 0.0, MPFR_RNDN);
     83  1.1.1.2  mrg   MPFR_ASSERTN (inexact == 0);
     84  1.1.1.2  mrg   MPFR_ASSERTN (__gmpfr_flags == MPFR_FLAGS_DIVBY0);
     85  1.1.1.2  mrg   MPFR_ASSERTN (mpfr_inf_p (y));
     86  1.1.1.2  mrg   MPFR_ASSERTN (MPFR_IS_NEG (y));
     87  1.1.1.2  mrg 
     88      1.1  mrg   mpfr_clear (x);
     89      1.1  mrg   mpfr_clear (y);
     90      1.1  mrg }
     91      1.1  mrg 
     92  1.1.1.3  mrg #define TEST_FUNCTION mpfr_div_d
     93      1.1  mrg #define DOUBLE_ARG2
     94      1.1  mrg #define RAND_FUNCTION(x) mpfr_random2(x, MPFR_LIMB_SIZE (x), 1, RANDS)
     95      1.1  mrg #include "tgeneric.c"
     96      1.1  mrg 
     97      1.1  mrg int
     98      1.1  mrg main (void)
     99      1.1  mrg {
    100      1.1  mrg   mpfr_t x, y, z;
    101      1.1  mrg   double d;
    102      1.1  mrg   int inexact;
    103      1.1  mrg 
    104      1.1  mrg   tests_start_mpfr ();
    105      1.1  mrg 
    106      1.1  mrg   /* check with enough precision */
    107      1.1  mrg   mpfr_init2 (x, IEEE_DBL_MANT_DIG);
    108      1.1  mrg   mpfr_init2 (y, IEEE_DBL_MANT_DIG);
    109      1.1  mrg   mpfr_init2 (z, IEEE_DBL_MANT_DIG);
    110      1.1  mrg 
    111      1.1  mrg   mpfr_set_str (y, "4096", 10, MPFR_RNDN);
    112      1.1  mrg   d = 0.125;
    113      1.1  mrg   mpfr_clear_flags ();
    114      1.1  mrg   inexact = mpfr_div_d (x, y, d, MPFR_RNDN);
    115      1.1  mrg   if (inexact != 0)
    116      1.1  mrg     {
    117      1.1  mrg       printf ("Inexact flag error in mpfr_div_d\n");
    118      1.1  mrg       exit (1);
    119      1.1  mrg     }
    120      1.1  mrg   mpfr_set_str (z, "32768", 10, MPFR_RNDN);
    121      1.1  mrg   if (mpfr_cmp (z, x))
    122      1.1  mrg     {
    123      1.1  mrg       printf ("Error in mpfr_div_d (");
    124      1.1  mrg       mpfr_out_str (stdout, 10, 0, y, MPFR_RNDN);
    125      1.1  mrg       printf (" + %.20g)\nexpected ", d);
    126      1.1  mrg       mpfr_out_str (stdout, 10, 0, z, MPFR_RNDN);
    127      1.1  mrg       printf ("\ngot     ");
    128      1.1  mrg       mpfr_out_str (stdout, 10, 0, x, MPFR_RNDN);
    129      1.1  mrg       printf ("\n");
    130      1.1  mrg       exit (1);
    131      1.1  mrg     }
    132      1.1  mrg   mpfr_clears (x, y, z, (mpfr_ptr) 0);
    133      1.1  mrg 
    134      1.1  mrg   check_nans ();
    135      1.1  mrg 
    136  1.1.1.4  mrg   test_generic (MPFR_PREC_MIN, 1000, 100);
    137      1.1  mrg 
    138      1.1  mrg   tests_end_mpfr ();
    139      1.1  mrg   return 0;
    140      1.1  mrg }
    141