Blame tests/mpf/t-sqrt_ui.c

Packit 5c3484
/* Test mpf_sqrt_ui.
Packit 5c3484
Packit 5c3484
Copyright 2004, 2015 Free Software Foundation, Inc.
Packit 5c3484
Packit 5c3484
This file is part of the GNU MP Library test suite.
Packit 5c3484
Packit 5c3484
The GNU MP Library test suite is free software; you can redistribute it
Packit 5c3484
and/or modify it under the terms of the GNU General Public License as
Packit 5c3484
published by the Free Software Foundation; either version 3 of the License,
Packit 5c3484
or (at your option) any later version.
Packit 5c3484
Packit 5c3484
The GNU MP Library test suite is distributed in the hope that it will be
Packit 5c3484
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 5c3484
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
Packit 5c3484
Public License for more details.
Packit 5c3484
Packit 5c3484
You should have received a copy of the GNU General Public License along with
Packit 5c3484
the GNU MP Library test suite.  If not, see https://www.gnu.org/licenses/.  */
Packit 5c3484
Packit 5c3484
#include <stdio.h>
Packit 5c3484
#include <stdlib.h>
Packit 5c3484
Packit 5c3484
#include "gmp.h"
Packit 5c3484
#include "gmp-impl.h"
Packit 5c3484
#include "tests.h"
Packit 5c3484
Packit 5c3484
Packit 5c3484
void
Packit 5c3484
check_rand (void)
Packit 5c3484
{
Packit 5c3484
  unsigned long      max_prec = 15;
Packit 5c3484
  unsigned long      min_prec = __GMPF_BITS_TO_PREC (1);
Packit 5c3484
  gmp_randstate_ptr  rands = RANDS;
Packit 5c3484
  unsigned long      x, prec;
Packit 5c3484
  mpf_t              r, s;
Packit 5c3484
  int                i;
Packit 5c3484
Packit 5c3484
  mpf_init (r);
Packit 5c3484
  mpf_init (s);
Packit 5c3484
  refmpf_set_prec_limbs (s, 2*max_prec+10);
Packit 5c3484
Packit 5c3484
  for (x = 0; x < 2; x++)
Packit 5c3484
    {
Packit 5c3484
      mpf_sqrt_ui (r, x);
Packit 5c3484
      MPF_CHECK_FORMAT (r);
Packit 5c3484
      if (mpf_cmp_ui (r, x) != 0)
Packit 5c3484
	{
Packit 5c3484
	  printf    ("mpf_sqrt_ui wrong for special case:\n");
Packit 5c3484
          printf    ("  x=%lu\n", x);
Packit 5c3484
          mpf_trace ("  r", r);
Packit 5c3484
	  abort ();
Packit 5c3484
	}
Packit 5c3484
    }
Packit 5c3484
Packit 5c3484
  for (i = 0; i < 50; i++)
Packit 5c3484
    {
Packit 5c3484
      /* input, a random non-zero ulong, exponentially distributed */
Packit 5c3484
      do {
Packit 5c3484
        x = gmp_urandomb_ui (rands,
Packit 5c3484
                             gmp_urandomm_ui (rands, BITS_PER_ULONG) + 1);
Packit 5c3484
      } while (x <= 1);
Packit 5c3484
Packit 5c3484
      /* result precision */
Packit 5c3484
      prec = gmp_urandomm_ui (rands, max_prec-min_prec) + min_prec;
Packit 5c3484
      refmpf_set_prec_limbs (r, prec);
Packit 5c3484
Packit 5c3484
      mpf_sqrt_ui (r, x);
Packit 5c3484
      MPF_CHECK_FORMAT (r);
Packit 5c3484
Packit 5c3484
      /* Expect to prec limbs of result.
Packit 5c3484
         In the current implementation there's no stripping of low zero
Packit 5c3484
         limbs in mpf_sqrt_ui, not even on perfect squares, so size should
Packit 5c3484
         be exactly prec.  */
Packit 5c3484
      if (SIZ(r) != prec)
Packit 5c3484
        {
Packit 5c3484
          printf ("mpf_sqrt_ui result not enough result limbs\n");
Packit 5c3484
          printf    ("  x=%lu\n", x);
Packit 5c3484
          printf    ("  want prec=%lu\n", prec);
Packit 5c3484
          mpf_trace ("  r", r);
Packit 5c3484
          printf    ("  r size %ld\n", (long) SIZ(r));
Packit 5c3484
          printf    ("  r prec %ld\n", (long) PREC(r));
Packit 5c3484
          abort ();
Packit 5c3484
        }
Packit 5c3484
Packit 5c3484
      /* Must have r^2 <= x, since r has been truncated. */
Packit 5c3484
      mpf_mul (s, r, r);
Packit 5c3484
      if (! (mpf_cmp_ui (s, x) <= 0))
Packit 5c3484
        {
Packit 5c3484
          printf    ("mpf_sqrt_ui result too big\n");
Packit 5c3484
          printf    ("  x=%lu\n", x);
Packit 5c3484
          printf    ("  want prec=%lu\n", prec);
Packit 5c3484
          mpf_trace ("  r", r);
Packit 5c3484
          mpf_trace ("  s", s);
Packit 5c3484
          abort ();
Packit 5c3484
        }
Packit 5c3484
Packit 5c3484
      /* Must have (r+ulp)^2 > x.
Packit 5c3484
         No overflow from refmpf_add_ulp since r is only prec limbs. */
Packit 5c3484
      refmpf_add_ulp (r);
Packit 5c3484
      mpf_mul (s, r, r);
Packit 5c3484
      if (! (mpf_cmp_ui (s, x) > 0))
Packit 5c3484
        {
Packit 5c3484
          printf    ("mpf_sqrt_ui result too small\n");
Packit 5c3484
          printf    ("  x=%lu\n", x);
Packit 5c3484
          printf    ("  want prec=%lu\n", prec);
Packit 5c3484
          mpf_trace ("  r+ulp", r);
Packit 5c3484
          mpf_trace ("  s", s);
Packit 5c3484
          abort ();
Packit 5c3484
        }
Packit 5c3484
    }
Packit 5c3484
Packit 5c3484
  mpf_clear (r);
Packit 5c3484
  mpf_clear (s);
Packit 5c3484
}
Packit 5c3484
Packit 5c3484
int
Packit 5c3484
main (int argc, char **argv)
Packit 5c3484
{
Packit 5c3484
  tests_start ();
Packit 5c3484
  mp_trace_base = -16;
Packit 5c3484
Packit 5c3484
  check_rand ();
Packit 5c3484
Packit 5c3484
  tests_end ();
Packit 5c3484
  exit (0);
Packit 5c3484
}