(root)/
gmp-6.3.0/
tests/
mpz/
t-get_d.c
       1  /* Test mpz_get_d.
       2  
       3  Copyright 2002, 2012, 2020 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  #include "gmp-impl.h"
      23  #include "tests.h"
      24  
      25  
      26  void
      27  check_onebit (void)
      28  {
      29    int     i;
      30    mpz_t   z;
      31    double  got, want;
      32    /* FIXME: It'd be better to base this on the float format. */
      33  #if defined (__vax) || defined (__vax__)
      34    int     limit = 127 - 1;  /* vax fp numbers have limited range */
      35  #else
      36    int     limit = 512;
      37  #endif
      38  
      39    mpz_init (z);
      40  
      41    got = mpz_get_d (z);
      42    if (got != 0)
      43      {
      44        printf    ("mpz_get_d wrong on zero\n");
      45        abort();
      46      }
      47  
      48    mpz_set_ui (z, 1L);
      49    want = 1.0;
      50  
      51    for (i = 0; i < limit; i++)
      52      {
      53        got = mpz_get_d (z);
      54  
      55        if (got != want)
      56          {
      57            printf    ("mpz_get_d wrong on 2**%d\n", i);
      58            mpz_trace ("   z    ", z);
      59            printf    ("   want  %.20g\n", want);
      60            printf    ("   got   %.20g\n", got);
      61            abort();
      62          }
      63  
      64        mpz_mul_2exp (z, z, 1L);
      65        want *= 2.0;
      66      }
      67    mpz_clear (z);
      68  }
      69  
      70  
      71  int
      72  main (void)
      73  {
      74    tests_start ();
      75  
      76    check_onebit ();
      77  
      78    tests_end ();
      79    exit (0);
      80  }