Blame tests/mpf/t-get_d.c

Packit 5c3484
/* Test mpf_get_d and mpf_set_d.
Packit 5c3484
Packit 5c3484
Copyright 1996, 1999-2001, 2009 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
#include "gmp.h"
Packit 5c3484
#include "tests.h"
Packit 5c3484
Packit 5c3484
#if defined (__vax) || defined (__vax__)
Packit 5c3484
#define LOW_BOUND 1e-38
Packit 5c3484
#define HIGH_BOUND 8e37
Packit 5c3484
#endif
Packit 5c3484
Packit 5c3484
#if defined (_CRAY) && ! defined (_CRAYIEEE)
Packit 5c3484
/* The range varies mysteriously between Cray version.  On an SV1,
Packit 5c3484
   the range seem to be 1e-600..1e603, but a cfp (non-ieee) T90
Packit 5c3484
   has a much smaller range of 1e-240..1e240.  */
Packit 5c3484
#define LOW_BOUND 1e-240
Packit 5c3484
#define HIGH_BOUND 1e240
Packit 5c3484
#endif
Packit 5c3484
Packit 5c3484
#if ! defined (LOW_BOUND)
Packit 5c3484
#define LOW_BOUND 1e-300
Packit 5c3484
#define HIGH_BOUND 1e300
Packit 5c3484
#endif
Packit 5c3484
Packit 5c3484
void
Packit 5c3484
test_denorms (int prc)
Packit 5c3484
{
Packit 5c3484
#ifdef _GMP_IEEE_FLOATS
Packit 5c3484
  double d1, d2;
Packit 5c3484
  mpf_t f;
Packit 5c3484
  int i;
Packit 5c3484
Packit 5c3484
  mpf_set_default_prec (prc);
Packit 5c3484
Packit 5c3484
  mpf_init (f);
Packit 5c3484
Packit 5c3484
  d1 = 1.9;
Packit 5c3484
  for (i = 0; i < 820; i++)
Packit 5c3484
    {
Packit 5c3484
      mpf_set_d (f, d1);
Packit 5c3484
      d2 = mpf_get_d (f);
Packit 5c3484
      if (d1 != d2)
Packit 5c3484
        abort ();
Packit 5c3484
      d1 *= 0.4;
Packit 5c3484
    }
Packit 5c3484
Packit 5c3484
  mpf_clear (f);
Packit 5c3484
#endif
Packit 5c3484
}
Packit 5c3484
Packit 5c3484
int
Packit 5c3484
main (int argc, char **argv)
Packit 5c3484
{
Packit 5c3484
  double d, e, r;
Packit 5c3484
  mpf_t u, v;
Packit 5c3484
Packit 5c3484
  tests_start ();
Packit 5c3484
  mpf_init (u);
Packit 5c3484
  mpf_init (v);
Packit 5c3484
Packit 5c3484
  mpf_set_d (u, LOW_BOUND);
Packit 5c3484
  for (d = 2.0 * LOW_BOUND; d < HIGH_BOUND; d *= 1.01)
Packit 5c3484
    {
Packit 5c3484
      mpf_set_d (v, d);
Packit 5c3484
      if (mpf_cmp (u, v) >= 0)
Packit 5c3484
	abort ();
Packit 5c3484
      e = mpf_get_d (v);
Packit 5c3484
      r = e/d;
Packit 5c3484
      if (r < 0.99999999999999 || r > 1.00000000000001)
Packit 5c3484
	{
Packit 5c3484
	  fprintf (stderr, "should be one ulp from 1: %.16f\n", r);
Packit 5c3484
	  abort ();
Packit 5c3484
	}
Packit 5c3484
      mpf_set (u, v);
Packit 5c3484
    }
Packit 5c3484
Packit 5c3484
  mpf_clear (u);
Packit 5c3484
  mpf_clear (v);
Packit 5c3484
Packit 5c3484
  test_denorms (10);
Packit 5c3484
  test_denorms (32);
Packit 5c3484
  test_denorms (64);
Packit 5c3484
  test_denorms (100);
Packit 5c3484
  test_denorms (200);
Packit 5c3484
Packit 5c3484
  tests_end ();
Packit 5c3484
  exit (0);
Packit 5c3484
}