Home | History | Annotate | Line # | Download | only in tests
      1      1.1  mrg /* Test file for mpfr_set.
      2      1.1  mrg 
      3  1.1.1.6  mrg Copyright 2001-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 "mpfr-test.h"
     24      1.1  mrg 
     25  1.1.1.4  mrg static int error;
     26      1.1  mrg 
     27      1.1  mrg #define PRINT_ERROR_IF(condition, text)         \
     28      1.1  mrg   do {                                          \
     29      1.1  mrg     if (condition)                              \
     30      1.1  mrg       {                                         \
     31      1.1  mrg         printf ("%s", text);                    \
     32      1.1  mrg         error = 1;                              \
     33      1.1  mrg       }                                         \
     34      1.1  mrg   } while (0)
     35      1.1  mrg 
     36      1.1  mrg 
     37  1.1.1.4  mrg /* Maybe better create its own test file? */
     38      1.1  mrg static void
     39      1.1  mrg check_neg_special (void)
     40      1.1  mrg {
     41  1.1.1.4  mrg   mpfr_t x, y;
     42  1.1.1.4  mrg   int inexact;
     43  1.1.1.4  mrg   int s1, s2, s3;
     44  1.1.1.4  mrg 
     45  1.1.1.4  mrg   mpfr_inits2 (53, x, y, (mpfr_ptr) 0);
     46  1.1.1.4  mrg 
     47      1.1  mrg   MPFR_SET_NAN (x);
     48  1.1.1.4  mrg   s1 = mpfr_signbit (x) != 0;
     49  1.1.1.4  mrg 
     50      1.1  mrg   mpfr_clear_nanflag ();
     51  1.1.1.4  mrg   inexact = mpfr_neg (y, x, MPFR_RNDN);
     52  1.1.1.4  mrg   s2 = mpfr_signbit (y) != 0;
     53      1.1  mrg   PRINT_ERROR_IF (!mpfr_nanflag_p (),
     54  1.1.1.4  mrg                   "ERROR: neg (NaN) doesn't set Nan flag (1).\n");
     55  1.1.1.4  mrg   PRINT_ERROR_IF (!mpfr_nan_p (y) || inexact != 0,
     56  1.1.1.4  mrg                   "ERROR: neg (NaN) failed to set variable to NaN (1).\n");
     57  1.1.1.4  mrg   PRINT_ERROR_IF (s1 == s2,
     58  1.1.1.4  mrg                   "ERROR: neg (NaN) doesn't correctly flip sign bit (1).\n");
     59  1.1.1.4  mrg 
     60  1.1.1.4  mrg   mpfr_clear_nanflag ();
     61  1.1.1.4  mrg   inexact = mpfr_neg (x, x, MPFR_RNDN);
     62  1.1.1.4  mrg   s2 = mpfr_signbit (x) != 0;
     63  1.1.1.4  mrg   PRINT_ERROR_IF (!mpfr_nanflag_p (),
     64  1.1.1.4  mrg                   "ERROR: neg (NaN) doesn't set Nan flag (2).\n");
     65  1.1.1.4  mrg   PRINT_ERROR_IF (!mpfr_nan_p (x) || inexact != 0,
     66  1.1.1.4  mrg                   "ERROR: neg (NaN) failed to set variable to NaN (2).\n");
     67  1.1.1.4  mrg   /* check following "bug" is fixed:
     68  1.1.1.4  mrg      https://sympa.inria.fr/sympa/arc/mpfr/2017-11/msg00003.html */
     69  1.1.1.4  mrg   PRINT_ERROR_IF (s1 == s2,
     70  1.1.1.4  mrg                   "ERROR: neg (NaN) doesn't correctly flip sign bit (2).\n");
     71  1.1.1.4  mrg 
     72  1.1.1.4  mrg   mpfr_clear_nanflag ();
     73  1.1.1.4  mrg   inexact = mpfr_neg (y, x, MPFR_RNDN);
     74  1.1.1.4  mrg   s3 = mpfr_signbit (y) != 0;
     75  1.1.1.4  mrg   PRINT_ERROR_IF (!mpfr_nanflag_p (),
     76  1.1.1.4  mrg                   "ERROR: neg (NaN) doesn't set Nan flag (3).\n");
     77  1.1.1.4  mrg   PRINT_ERROR_IF (!mpfr_nan_p (y) || inexact != 0,
     78  1.1.1.4  mrg                   "ERROR: neg (NaN) failed to set variable to NaN (3).\n");
     79  1.1.1.4  mrg   PRINT_ERROR_IF (s2 == s3,
     80  1.1.1.4  mrg                   "ERROR: neg (NaN) doesn't correctly flip sign bit (3).\n");
     81  1.1.1.4  mrg 
     82  1.1.1.4  mrg   mpfr_clear_nanflag ();
     83  1.1.1.4  mrg   inexact = mpfr_neg (x, x, MPFR_RNDN);
     84  1.1.1.4  mrg   s3 = mpfr_signbit (x) != 0;
     85  1.1.1.4  mrg   PRINT_ERROR_IF (!mpfr_nanflag_p (),
     86  1.1.1.4  mrg                   "ERROR: neg (NaN) doesn't set Nan flag (4).\n");
     87  1.1.1.4  mrg   PRINT_ERROR_IF (!mpfr_nan_p (x) || inexact != 0,
     88  1.1.1.4  mrg                   "ERROR: neg (NaN) failed to set variable to NaN (4).\n");
     89  1.1.1.4  mrg   PRINT_ERROR_IF (s2 == s3,
     90  1.1.1.4  mrg                   "ERROR: neg (NaN) doesn't correctly flip sign bit (4).\n");
     91  1.1.1.4  mrg 
     92  1.1.1.4  mrg   mpfr_clears (x, y, (mpfr_ptr) 0);
     93      1.1  mrg }
     94      1.1  mrg 
     95      1.1  mrg static void
     96      1.1  mrg check_special (void)
     97      1.1  mrg {
     98      1.1  mrg   mpfr_t x, y;
     99      1.1  mrg   int inexact;
    100  1.1.1.4  mrg   int s1, s2;
    101      1.1  mrg 
    102  1.1.1.4  mrg   mpfr_inits2 (53, x, y, (mpfr_ptr) 0);
    103      1.1  mrg 
    104      1.1  mrg   mpfr_set_inf (x, 1);
    105      1.1  mrg   PRINT_ERROR_IF (!mpfr_inf_p (x) || mpfr_sgn (x) < 0,
    106  1.1.1.4  mrg                   "ERROR: mpfr_set_inf failed to set variable to +inf [1].\n");
    107  1.1.1.4  mrg   mpfr_set_inf (x, INT_MAX);
    108  1.1.1.4  mrg   PRINT_ERROR_IF (!mpfr_inf_p (x) || mpfr_sgn (x) < 0,
    109  1.1.1.4  mrg                   "ERROR: mpfr_set_inf failed to set variable to +inf [2].\n");
    110  1.1.1.4  mrg   mpfr_set_inf (x, 0);
    111  1.1.1.4  mrg   PRINT_ERROR_IF (!mpfr_inf_p (x) || mpfr_sgn (x) < 0,
    112  1.1.1.4  mrg                   "ERROR: mpfr_set_inf failed to set variable to +inf [3].\n");
    113      1.1  mrg   inexact = mpfr_set (y, x, MPFR_RNDN);
    114      1.1  mrg   PRINT_ERROR_IF (!mpfr_inf_p (y) || mpfr_sgn (y) < 0 || inexact != 0,
    115      1.1  mrg                   "ERROR: mpfr_set failed to set variable to +infinity.\n");
    116      1.1  mrg 
    117      1.1  mrg   inexact = mpfr_set_ui (y, 0, MPFR_RNDN);
    118  1.1.1.4  mrg   PRINT_ERROR_IF (MPFR_NOTZERO (y) || MPFR_IS_NEG (y) || inexact != 0,
    119      1.1  mrg                   "ERROR: mpfr_set_ui failed to set variable to +0.\n");
    120      1.1  mrg 
    121      1.1  mrg   mpfr_set_inf (x, -1);
    122      1.1  mrg   PRINT_ERROR_IF (!mpfr_inf_p (x) || mpfr_sgn (x) > 0,
    123  1.1.1.4  mrg                   "ERROR: mpfr_set_inf failed to set variable to -inf [1].\n");
    124  1.1.1.4  mrg   mpfr_set_inf (x, INT_MIN);
    125  1.1.1.4  mrg   PRINT_ERROR_IF (!mpfr_inf_p (x) || mpfr_sgn (x) > 0,
    126  1.1.1.4  mrg                   "ERROR: mpfr_set_inf failed to set variable to -inf [2].\n");
    127      1.1  mrg   inexact = mpfr_set (y, x, MPFR_RNDN);
    128      1.1  mrg   PRINT_ERROR_IF (!mpfr_inf_p (y) || mpfr_sgn (y) > 0 || inexact != 0,
    129      1.1  mrg                   "ERROR: mpfr_set failed to set variable to -infinity.\n");
    130      1.1  mrg 
    131      1.1  mrg   mpfr_set_zero (x, 1);
    132  1.1.1.4  mrg   PRINT_ERROR_IF (MPFR_NOTZERO (x) || MPFR_IS_NEG (x),
    133  1.1.1.4  mrg                   "ERROR: mpfr_set_zero failed to set variable to +0 [1].\n");
    134  1.1.1.4  mrg   mpfr_set_zero (x, INT_MAX);
    135  1.1.1.4  mrg   PRINT_ERROR_IF (MPFR_NOTZERO (x) || MPFR_IS_NEG (x),
    136  1.1.1.4  mrg                   "ERROR: mpfr_set_zero failed to set variable to +0 [2].\n");
    137  1.1.1.4  mrg   mpfr_set_zero (x, 0);
    138  1.1.1.4  mrg   PRINT_ERROR_IF (MPFR_NOTZERO (x) || MPFR_IS_NEG (x),
    139  1.1.1.4  mrg                   "ERROR: mpfr_set_zero failed to set variable to +0 [3].\n");
    140      1.1  mrg   inexact = mpfr_set (y, x, MPFR_RNDN);
    141  1.1.1.4  mrg   PRINT_ERROR_IF (MPFR_NOTZERO (y) || MPFR_IS_NEG (y) || inexact != 0,
    142      1.1  mrg                   "ERROR: mpfr_set failed to set variable to +0.\n");
    143      1.1  mrg 
    144      1.1  mrg   mpfr_set_zero (x, -1);
    145  1.1.1.4  mrg   PRINT_ERROR_IF (MPFR_NOTZERO (x) || MPFR_IS_POS (x),
    146  1.1.1.4  mrg                   "ERROR: mpfr_set_zero failed to set variable to -0 [1].\n");
    147  1.1.1.4  mrg   mpfr_set_zero (x, INT_MIN);
    148  1.1.1.4  mrg   PRINT_ERROR_IF (MPFR_NOTZERO (x) || MPFR_IS_POS (x),
    149  1.1.1.4  mrg                   "ERROR: mpfr_set_zero failed to set variable to -0 [2].\n");
    150      1.1  mrg   inexact = mpfr_set (y, x, MPFR_RNDN);
    151  1.1.1.4  mrg   PRINT_ERROR_IF (MPFR_NOTZERO (y) || MPFR_IS_POS (y) || inexact != 0,
    152      1.1  mrg                   "ERROR: mpfr_set failed to set variable to -0.\n");
    153      1.1  mrg 
    154  1.1.1.4  mrg   /* NaN tests */
    155  1.1.1.4  mrg 
    156      1.1  mrg   mpfr_set_nan (x);
    157      1.1  mrg   PRINT_ERROR_IF (!mpfr_nan_p (x),
    158      1.1  mrg                   "ERROR: mpfr_set_nan failed to set variable to NaN.\n");
    159  1.1.1.4  mrg   s1 = mpfr_signbit (x) != 0;
    160  1.1.1.4  mrg 
    161  1.1.1.4  mrg   mpfr_clear_nanflag ();
    162      1.1  mrg   inexact = mpfr_set (y, x, MPFR_RNDN);
    163  1.1.1.4  mrg   s2 = mpfr_signbit (y) != 0;
    164  1.1.1.4  mrg   PRINT_ERROR_IF (!mpfr_nanflag_p (),
    165  1.1.1.4  mrg                   "ERROR: mpfr_set doesn't set Nan flag (1).\n");
    166      1.1  mrg   PRINT_ERROR_IF (!mpfr_nan_p (y) || inexact != 0,
    167  1.1.1.4  mrg                   "ERROR: mpfr_set failed to set variable to NaN (1).\n");
    168  1.1.1.4  mrg   PRINT_ERROR_IF (s1 != s2,
    169  1.1.1.4  mrg                   "ERROR: mpfr_set doesn't preserve the sign bit (1).\n");
    170      1.1  mrg 
    171  1.1.1.4  mrg   mpfr_clear_nanflag ();
    172  1.1.1.4  mrg   inexact = mpfr_set (x, x, MPFR_RNDN);
    173  1.1.1.4  mrg   s2 = mpfr_signbit (x) != 0;
    174  1.1.1.4  mrg   PRINT_ERROR_IF (!mpfr_nanflag_p (),
    175  1.1.1.4  mrg                   "ERROR: mpfr_set doesn't set Nan flag (2).\n");
    176  1.1.1.4  mrg   PRINT_ERROR_IF (!mpfr_nan_p (x) || inexact != 0,
    177  1.1.1.4  mrg                   "ERROR: mpfr_set failed to set variable to NaN (2).\n");
    178  1.1.1.4  mrg   PRINT_ERROR_IF (s1 != s2,
    179  1.1.1.4  mrg                   "ERROR: mpfr_set doesn't preserve the sign bit (2).\n");
    180  1.1.1.4  mrg 
    181  1.1.1.4  mrg   MPFR_CHANGE_SIGN (x);
    182  1.1.1.4  mrg   s1 = !s1;
    183  1.1.1.4  mrg 
    184  1.1.1.4  mrg   mpfr_clear_nanflag ();
    185  1.1.1.4  mrg   inexact = mpfr_set (y, x, MPFR_RNDN);
    186  1.1.1.4  mrg   s2 = mpfr_signbit (y) != 0;
    187  1.1.1.4  mrg   PRINT_ERROR_IF (!mpfr_nanflag_p (),
    188  1.1.1.4  mrg                   "ERROR: mpfr_set doesn't set Nan flag (3).\n");
    189  1.1.1.4  mrg   PRINT_ERROR_IF (!mpfr_nan_p (y) || inexact != 0,
    190  1.1.1.4  mrg                   "ERROR: mpfr_set failed to set variable to NaN (3).\n");
    191  1.1.1.4  mrg   PRINT_ERROR_IF (s1 != s2,
    192  1.1.1.4  mrg                   "ERROR: mpfr_set doesn't preserve the sign bit (3).\n");
    193  1.1.1.4  mrg 
    194  1.1.1.4  mrg   mpfr_clear_nanflag ();
    195  1.1.1.4  mrg   inexact = mpfr_set (x, x, MPFR_RNDN);
    196  1.1.1.4  mrg   s2 = mpfr_signbit (x) != 0;
    197  1.1.1.4  mrg   PRINT_ERROR_IF (!mpfr_nanflag_p (),
    198  1.1.1.4  mrg                   "ERROR: mpfr_set doesn't set Nan flag (4).\n");
    199  1.1.1.4  mrg   PRINT_ERROR_IF (!mpfr_nan_p (x) || inexact != 0,
    200  1.1.1.4  mrg                   "ERROR: mpfr_set failed to set variable to NaN (4).\n");
    201  1.1.1.4  mrg   PRINT_ERROR_IF (s1 != s2,
    202  1.1.1.4  mrg                   "ERROR: mpfr_set doesn't preserve the sign bit (4).\n");
    203  1.1.1.4  mrg 
    204  1.1.1.4  mrg   mpfr_clears (x, y, (mpfr_ptr) 0);
    205      1.1  mrg }
    206      1.1  mrg 
    207      1.1  mrg static void
    208      1.1  mrg check_ternary_value (void)
    209      1.1  mrg {
    210  1.1.1.6  mrg   int k, p, q, rnd;
    211      1.1  mrg   int inexact, cmp;
    212      1.1  mrg   mpfr_t x, y;
    213      1.1  mrg 
    214      1.1  mrg   mpfr_init (x);
    215      1.1  mrg   mpfr_init (y);
    216      1.1  mrg   for (p=2; p<500; p++)
    217      1.1  mrg     {
    218      1.1  mrg       mpfr_set_prec (x, p);
    219      1.1  mrg       mpfr_urandomb (x, RANDS);
    220  1.1.1.6  mrg       if (RAND_BOOL ())
    221      1.1  mrg         mpfr_neg (x, x, MPFR_RNDN);
    222      1.1  mrg       for (q=2; q<2*p; q++)
    223      1.1  mrg         {
    224      1.1  mrg           mpfr_set_prec (y, q);
    225  1.1.1.6  mrg           RND_LOOP (rnd)
    226      1.1  mrg             {
    227  1.1.1.4  mrg               if (rnd == MPFR_RNDF) /* the test below makes no sense */
    228  1.1.1.4  mrg                 continue;
    229  1.1.1.6  mrg               for (k = 0; k < 3; k++)
    230      1.1  mrg                 {
    231  1.1.1.6  mrg                   int a = 0, b = 0, c = 0;
    232  1.1.1.6  mrg 
    233  1.1.1.6  mrg                   switch (k)
    234  1.1.1.6  mrg                     {
    235  1.1.1.6  mrg                     case 0:
    236  1.1.1.6  mrg                       inexact = mpfr_set (y, x, (mpfr_rnd_t) rnd);
    237  1.1.1.6  mrg                       break;
    238  1.1.1.6  mrg                     case 1:
    239  1.1.1.6  mrg                       inexact = (mpfr_set) (y, x, (mpfr_rnd_t) rnd);
    240  1.1.1.6  mrg                       break;
    241  1.1.1.6  mrg                     case 2:
    242  1.1.1.6  mrg #ifdef IGNORE_CPP_COMPAT
    243  1.1.1.6  mrg #pragma GCC diagnostic push
    244  1.1.1.6  mrg #pragma GCC diagnostic ignored "-Wc++-compat"
    245  1.1.1.6  mrg #endif
    246  1.1.1.6  mrg                       inexact = mpfr_set ((a++, VOIDP_CAST(y)),
    247  1.1.1.6  mrg                                           (b++, VOIDP_CAST(x)),
    248  1.1.1.6  mrg                                           (c++, (mpfr_rnd_t) rnd));
    249  1.1.1.6  mrg #ifdef IGNORE_CPP_COMPAT
    250  1.1.1.6  mrg #pragma GCC diagnostic pop
    251  1.1.1.6  mrg #endif
    252  1.1.1.6  mrg                       MPFR_ASSERTN (a == 1);
    253  1.1.1.6  mrg                       MPFR_ASSERTN (b == 1);
    254  1.1.1.6  mrg                       MPFR_ASSERTN (c == 1);
    255  1.1.1.6  mrg                       break;
    256  1.1.1.6  mrg                     }
    257  1.1.1.6  mrg                   cmp = mpfr_cmp (y, x);
    258  1.1.1.6  mrg                   if (((inexact == 0) && (cmp != 0)) ||
    259  1.1.1.6  mrg                       ((inexact > 0) && (cmp <= 0)) ||
    260  1.1.1.6  mrg                       ((inexact < 0) && (cmp >= 0)))
    261  1.1.1.6  mrg                     {
    262  1.1.1.6  mrg                       printf ("Wrong ternary value in mpfr_set for %s (%d):"
    263  1.1.1.6  mrg                               " expected %d, got %d\n",
    264  1.1.1.6  mrg                               mpfr_print_rnd_mode ((mpfr_rnd_t) rnd),
    265  1.1.1.6  mrg                               k, cmp, inexact);
    266  1.1.1.6  mrg                       exit (1);
    267  1.1.1.6  mrg                     }
    268      1.1  mrg                 }
    269      1.1  mrg             }
    270      1.1  mrg         }
    271      1.1  mrg     }
    272      1.1  mrg   mpfr_clear (x);
    273      1.1  mrg   mpfr_clear (y);
    274      1.1  mrg }
    275      1.1  mrg 
    276  1.1.1.5  mrg static void
    277  1.1.1.5  mrg test_set_1_2 (void)
    278  1.1.1.5  mrg {
    279  1.1.1.5  mrg   mpfr_t u, v, zz, z;
    280  1.1.1.5  mrg   int inex;
    281  1.1.1.5  mrg 
    282  1.1.1.5  mrg   /* (8,16)-bit test */
    283  1.1.1.5  mrg   mpfr_inits2 (16, u, v, zz, (mpfr_ptr) 0);
    284  1.1.1.5  mrg   mpfr_init2 (z, 8);
    285  1.1.1.5  mrg   mpfr_set_str_binary (u, "0.1100001100011010E-1");
    286  1.1.1.5  mrg   mpfr_set_str_binary (v, "0.1100010101110010E0");
    287  1.1.1.5  mrg   /* u + v = 1.0010011011111111 */
    288  1.1.1.5  mrg   inex = mpfr_add (zz, u, v, MPFR_RNDN);
    289  1.1.1.5  mrg   MPFR_ASSERTN(inex > 0);
    290  1.1.1.5  mrg   mpfr_set_str_binary (u, "1.001001110000000");
    291  1.1.1.5  mrg   MPFR_ASSERTN(mpfr_equal_p (zz, u));
    292  1.1.1.5  mrg   inex = mpfr_set_1_2 (z, zz, MPFR_RNDN, inex);
    293  1.1.1.5  mrg   /* we should have z = 1.0010011 and inex < 0 */
    294  1.1.1.5  mrg   MPFR_ASSERTN(inex < 0);
    295  1.1.1.5  mrg   mpfr_set_str_binary (u, "1.0010011");
    296  1.1.1.5  mrg   MPFR_ASSERTN(mpfr_equal_p (z, u));
    297  1.1.1.5  mrg   mpfr_clears (u, v, zz, z, (mpfr_ptr) 0);
    298  1.1.1.5  mrg 
    299  1.1.1.5  mrg   /* (16,32)-bit test:
    300  1.1.1.5  mrg    * take for v a random 32-bit number in [1/2,1), here 2859611790/2^32
    301  1.1.1.5  mrg    * take for z a random 16-bit number in [1,2), less than 2*v,
    302  1.1.1.5  mrg    with last bit 0, here we take z = 40900/2^15
    303  1.1.1.5  mrg    * take u = z-v-1/2^16-1/2^32 */
    304  1.1.1.5  mrg   mpfr_inits2 (32, u, v, zz, (mpfr_ptr) 0);
    305  1.1.1.5  mrg   mpfr_init2 (z, 16);
    306  1.1.1.5  mrg   mpfr_set_str_binary (u, "0.10010101000101001100100101110001");
    307  1.1.1.5  mrg   mpfr_set_str_binary (v, "0.10101010011100100011011010001110");
    308  1.1.1.5  mrg   /* u + v = 1.00111111100001101111111111111111 */
    309  1.1.1.5  mrg   inex = mpfr_add (zz, u, v, MPFR_RNDN);
    310  1.1.1.5  mrg   MPFR_ASSERTN(inex > 0);
    311  1.1.1.5  mrg   mpfr_set_str_binary (u, "1.0011111110000111");
    312  1.1.1.5  mrg   MPFR_ASSERTN(mpfr_equal_p (zz, u));
    313  1.1.1.5  mrg   inex = mpfr_set_1_2 (z, zz, MPFR_RNDN, inex);
    314  1.1.1.5  mrg   /* we should have z = 1.001111111000011 and inex < 0 */
    315  1.1.1.5  mrg   MPFR_ASSERTN(inex < 0);
    316  1.1.1.5  mrg   mpfr_set_str_binary (u, "1.001111111000011");
    317  1.1.1.5  mrg   MPFR_ASSERTN(mpfr_equal_p (z, u));
    318  1.1.1.5  mrg   mpfr_clears (u, v, zz, z, (mpfr_ptr) 0);
    319  1.1.1.5  mrg 
    320  1.1.1.5  mrg   /* (32,64)-bit test:
    321  1.1.1.5  mrg    * take for v a random 64-bit number in [1/2,1),
    322  1.1.1.5  mrg      here v = 13687985014345662879/2^64
    323  1.1.1.5  mrg    * take for z a random 32-bit number in [1,2), less than 2*v,
    324  1.1.1.5  mrg      with last bit 0, here we take z = 2871078774/2^31
    325  1.1.1.5  mrg    * take u = z-v-1/2^32-1/2^64 */
    326  1.1.1.5  mrg   mpfr_inits2 (64, u, v, zz, (mpfr_ptr) 0);
    327  1.1.1.5  mrg   mpfr_init2 (z, 32);
    328  1.1.1.5  mrg   mpfr_set_str_binary (u, "0.10011000010011001110000100010001110010010000111001111110011");
    329  1.1.1.5  mrg   mpfr_set_str_binary (v, "0.1011110111110101011111011101100100110110111100011000000110011111");
    330  1.1.1.5  mrg   /* u + v = 1.0101011001000010010111101110101011111111111111111111111111111111 */
    331  1.1.1.5  mrg   inex = mpfr_add (zz, u, v, MPFR_RNDN);
    332  1.1.1.5  mrg   MPFR_ASSERTN(inex > 0);
    333  1.1.1.5  mrg   mpfr_set_str_binary (u, "1.01010110010000100101111011101011");
    334  1.1.1.5  mrg   MPFR_ASSERTN(mpfr_equal_p (zz, u));
    335  1.1.1.5  mrg   inex = mpfr_set_1_2 (z, zz, MPFR_RNDN, inex);
    336  1.1.1.5  mrg   /* we should have z = 1.0101011001000010010111101110101 and inex < 0 */
    337  1.1.1.5  mrg   MPFR_ASSERTN(inex < 0);
    338  1.1.1.5  mrg   mpfr_set_str_binary (u, "1.0101011001000010010111101110101");
    339  1.1.1.5  mrg   MPFR_ASSERTN(mpfr_equal_p (z, u));
    340  1.1.1.5  mrg   mpfr_clears (u, v, zz, z, (mpfr_ptr) 0);
    341  1.1.1.5  mrg 
    342  1.1.1.5  mrg   /* (64,128)-bit test:
    343  1.1.1.5  mrg    * take for v a random 128-bit number in [1/2,1),
    344  1.1.1.5  mrg      here v = 322263811942091240216761391118876232409/2^128
    345  1.1.1.5  mrg    * take for z a random 64-bit number in [1,2), less than 2*v,
    346  1.1.1.5  mrg      with last bit 0, here we take z = 16440347967874738276/2^63
    347  1.1.1.5  mrg    * take u = z-v-1/2^64-1/2^128 */
    348  1.1.1.5  mrg   mpfr_inits2 (128, u, v, zz, (mpfr_ptr) 0);
    349  1.1.1.5  mrg   mpfr_init2 (z, 64);
    350  1.1.1.5  mrg   mpfr_set_str_binary (u, "0.1101010111011101111100100001011111111000010011011001000101111010110101101101011011100110101001010001101011011110101101010010011");
    351  1.1.1.5  mrg   mpfr_set_str_binary (v, "0.11110010011100011100000010100110100010011010110010111111010011000010100100101001000110010101101011100101001000010100101011011001");
    352  1.1.1.5  mrg   inex = mpfr_add (zz, u, v, MPFR_RNDN);
    353  1.1.1.5  mrg   MPFR_ASSERTN(inex > 0);
    354  1.1.1.5  mrg   mpfr_set_str_binary (u, "1.1100100001001111101100101011111010000001111110100101000011000111");
    355  1.1.1.5  mrg   MPFR_ASSERTN(mpfr_equal_p (zz, u));
    356  1.1.1.5  mrg   inex = mpfr_set_1_2 (z, zz, MPFR_RNDN, inex);
    357  1.1.1.5  mrg   MPFR_ASSERTN(inex < 0);
    358  1.1.1.5  mrg   mpfr_set_str_binary (u, "1.1100100001001111101100101011111010000001111110100101000011000110");
    359  1.1.1.5  mrg   MPFR_ASSERTN(mpfr_equal_p (z, u));
    360  1.1.1.5  mrg   mpfr_clears (u, v, zz, z, (mpfr_ptr) 0);
    361  1.1.1.5  mrg }
    362  1.1.1.5  mrg 
    363      1.1  mrg #define TEST_FUNCTION mpfr_set
    364      1.1  mrg #include "tgeneric.c"
    365      1.1  mrg 
    366      1.1  mrg int
    367      1.1  mrg main (void)
    368      1.1  mrg {
    369      1.1  mrg   mpfr_t x, y, z, u;
    370      1.1  mrg   int inexact;
    371      1.1  mrg   mpfr_exp_t emax;
    372      1.1  mrg 
    373      1.1  mrg   tests_start_mpfr ();
    374      1.1  mrg 
    375  1.1.1.5  mrg   test_set_1_2 ();
    376  1.1.1.5  mrg 
    377      1.1  mrg   /* Default : no error */
    378      1.1  mrg   error = 0;
    379      1.1  mrg 
    380      1.1  mrg   /* check prototypes of mpfr_init_set_* */
    381      1.1  mrg   inexact = mpfr_init_set_si (x, -1, MPFR_RNDN);
    382  1.1.1.2  mrg   MPFR_ASSERTN (inexact == 0);
    383      1.1  mrg   inexact = mpfr_init_set (y, x, MPFR_RNDN);
    384  1.1.1.2  mrg   MPFR_ASSERTN (inexact == 0);
    385      1.1  mrg   inexact = mpfr_init_set_ui (z, 1, MPFR_RNDN);
    386  1.1.1.2  mrg   MPFR_ASSERTN (inexact == 0);
    387      1.1  mrg   inexact = mpfr_init_set_d (u, 1.0, MPFR_RNDN);
    388  1.1.1.2  mrg   MPFR_ASSERTN (inexact == 0);
    389      1.1  mrg 
    390      1.1  mrg   emax = mpfr_get_emax ();
    391      1.1  mrg   set_emax (0);
    392      1.1  mrg   mpfr_set_prec (x, 3);
    393      1.1  mrg   mpfr_set_str_binary (x, "0.111");
    394      1.1  mrg   mpfr_set_prec (y, 2);
    395      1.1  mrg   mpfr_set (y, x, MPFR_RNDU);
    396  1.1.1.4  mrg   if (!(MPFR_IS_INF (y) && MPFR_IS_POS (y)))
    397      1.1  mrg     {
    398      1.1  mrg       printf ("Error for y=x=0.111 with px=3, py=2 and emax=0\nx=");
    399      1.1  mrg       mpfr_dump (x);
    400      1.1  mrg       printf ("y=");
    401      1.1  mrg       mpfr_dump (y);
    402      1.1  mrg       exit (1);
    403      1.1  mrg     }
    404      1.1  mrg 
    405      1.1  mrg   set_emax (emax);
    406      1.1  mrg 
    407      1.1  mrg   mpfr_set_prec (y, 11);
    408      1.1  mrg   mpfr_set_str_binary (y, "0.11111111100E-8");
    409      1.1  mrg   mpfr_set_prec (x, 2);
    410      1.1  mrg   mpfr_set (x, y, MPFR_RNDN);
    411      1.1  mrg   mpfr_set_str_binary (y, "1.0E-8");
    412      1.1  mrg   if (mpfr_cmp (x, y))
    413      1.1  mrg     {
    414      1.1  mrg       printf ("Error for y=0.11111111100E-8, prec=2, rnd=MPFR_RNDN\n");
    415      1.1  mrg       exit (1);
    416      1.1  mrg     }
    417      1.1  mrg 
    418      1.1  mrg   mpfr_clear (x);
    419      1.1  mrg   mpfr_clear (y);
    420      1.1  mrg   mpfr_clear (z);
    421      1.1  mrg   mpfr_clear (u);
    422      1.1  mrg 
    423      1.1  mrg   check_ternary_value ();
    424      1.1  mrg   check_special ();
    425      1.1  mrg   check_neg_special ();
    426      1.1  mrg 
    427  1.1.1.4  mrg   test_generic (MPFR_PREC_MIN, 1000, 10);
    428      1.1  mrg 
    429      1.1  mrg   tests_end_mpfr ();
    430      1.1  mrg   return error;
    431      1.1  mrg }
    432