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

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