github.com/aergoio/aergo@v1.3.1/libtool/src/gmp-6.1.2/tests/mpf/t-sqrt_ui.c (about)

     1  /* Test mpf_sqrt_ui.
     2  
     3  Copyright 2004, 2015 Free Software Foundation, Inc.
     4  
     5  This file is part of the GNU MP Library test suite.
     6  
     7  The GNU MP Library test suite is free software; you can redistribute it
     8  and/or modify it under the terms of the GNU General Public License as
     9  published by the Free Software Foundation; either version 3 of the License,
    10  or (at your option) any later version.
    11  
    12  The GNU MP Library test suite is distributed in the hope that it will be
    13  useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
    14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
    15  Public License for more details.
    16  
    17  You should have received a copy of the GNU General Public License along with
    18  the GNU MP Library test suite.  If not, see https://www.gnu.org/licenses/.  */
    19  
    20  #include <stdio.h>
    21  #include <stdlib.h>
    22  
    23  #include "gmp.h"
    24  #include "gmp-impl.h"
    25  #include "tests.h"
    26  
    27  
    28  void
    29  check_rand (void)
    30  {
    31    unsigned long      max_prec = 15;
    32    unsigned long      min_prec = __GMPF_BITS_TO_PREC (1);
    33    gmp_randstate_ptr  rands = RANDS;
    34    unsigned long      x, prec;
    35    mpf_t              r, s;
    36    int                i;
    37  
    38    mpf_init (r);
    39    mpf_init (s);
    40    refmpf_set_prec_limbs (s, 2*max_prec+10);
    41  
    42    for (x = 0; x < 2; x++)
    43      {
    44        mpf_sqrt_ui (r, x);
    45        MPF_CHECK_FORMAT (r);
    46        if (mpf_cmp_ui (r, x) != 0)
    47  	{
    48  	  printf    ("mpf_sqrt_ui wrong for special case:\n");
    49            printf    ("  x=%lu\n", x);
    50            mpf_trace ("  r", r);
    51  	  abort ();
    52  	}
    53      }
    54  
    55    for (i = 0; i < 50; i++)
    56      {
    57        /* input, a random non-zero ulong, exponentially distributed */
    58        do {
    59          x = gmp_urandomb_ui (rands,
    60                               gmp_urandomm_ui (rands, BITS_PER_ULONG) + 1);
    61        } while (x <= 1);
    62  
    63        /* result precision */
    64        prec = gmp_urandomm_ui (rands, max_prec-min_prec) + min_prec;
    65        refmpf_set_prec_limbs (r, prec);
    66  
    67        mpf_sqrt_ui (r, x);
    68        MPF_CHECK_FORMAT (r);
    69  
    70        /* Expect to prec limbs of result.
    71           In the current implementation there's no stripping of low zero
    72           limbs in mpf_sqrt_ui, not even on perfect squares, so size should
    73           be exactly prec.  */
    74        if (SIZ(r) != prec)
    75          {
    76            printf ("mpf_sqrt_ui result not enough result limbs\n");
    77            printf    ("  x=%lu\n", x);
    78            printf    ("  want prec=%lu\n", prec);
    79            mpf_trace ("  r", r);
    80            printf    ("  r size %ld\n", (long) SIZ(r));
    81            printf    ("  r prec %ld\n", (long) PREC(r));
    82            abort ();
    83          }
    84  
    85        /* Must have r^2 <= x, since r has been truncated. */
    86        mpf_mul (s, r, r);
    87        if (! (mpf_cmp_ui (s, x) <= 0))
    88          {
    89            printf    ("mpf_sqrt_ui result too big\n");
    90            printf    ("  x=%lu\n", x);
    91            printf    ("  want prec=%lu\n", prec);
    92            mpf_trace ("  r", r);
    93            mpf_trace ("  s", s);
    94            abort ();
    95          }
    96  
    97        /* Must have (r+ulp)^2 > x.
    98           No overflow from refmpf_add_ulp since r is only prec limbs. */
    99        refmpf_add_ulp (r);
   100        mpf_mul (s, r, r);
   101        if (! (mpf_cmp_ui (s, x) > 0))
   102          {
   103            printf    ("mpf_sqrt_ui result too small\n");
   104            printf    ("  x=%lu\n", x);
   105            printf    ("  want prec=%lu\n", prec);
   106            mpf_trace ("  r+ulp", r);
   107            mpf_trace ("  s", s);
   108            abort ();
   109          }
   110      }
   111  
   112    mpf_clear (r);
   113    mpf_clear (s);
   114  }
   115  
   116  int
   117  main (int argc, char **argv)
   118  {
   119    tests_start ();
   120    mp_trace_base = -16;
   121  
   122    check_rand ();
   123  
   124    tests_end ();
   125    exit (0);
   126  }