aboutsummaryrefslogtreecommitdiff
path: root/gmp-6.3.0/tests/mpz/t-set_d.c
blob: 1fdfc02e1605918dcf0e59674d18209717e8078e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/* Test mpz_set_d and mpz_init_set_d.

Copyright 2000-2003, 2006 Free Software Foundation, Inc.

This file is part of the GNU MP Library test suite.

The GNU MP Library test suite is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 3 of the License,
or (at your option) any later version.

The GNU MP Library test suite is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
Public License for more details.

You should have received a copy of the GNU General Public License along with
the GNU MP Library test suite.  If not, see https://www.gnu.org/licenses/.  */

#include <stdio.h>
#include <stdlib.h>
#include "gmp-impl.h"
#include "tests.h"


void
check_data (void)
{
  static const struct {
    double     d;
    mp_size_t  want_size;
    mp_limb_t  want_data[2];
  } data[] = {

    {  0.0,  0 },
    {  1.0,  1, { 1 } },
    { -1.0, -1, { 1 } },

    {  123.0,  1, { 123 } },
    { -123.0, -1, { 123 } },

    { 1e-1, 0, { 0 } },
    { -1e-1, 0, { 0 } },
    { 2.328306436538696e-10, 0, { 0 } },
    { -2.328306436538696e-10, 0, { 0 } },
    { 5.421010862427522e-20, 0, { 0 } },
    { -5.421010862427522e-20, 0, { 0 } },
    { 2.938735877055719e-39, 0, { 0 } },
    { -2.938735877055719e-39, 0, { 0 } },
  };

  mpz_t  z;
  int    i;

  for (i = 0; i < numberof (data); i++)
    {
      mpz_init (z);
      mpz_set_d (z, data[i].d);
      MPZ_CHECK_FORMAT (z);
      if (z->_mp_size != data[i].want_size
          || refmpn_cmp_allowzero (z->_mp_d, data[i].want_data,
                                   ABS (data[i].want_size)) != 0)
        {
          printf ("mpz_set_d wrong on data[%d]\n", i);
        bad:
          d_trace   ("  d  ", data[i].d);
          printf    ("  got  size %ld\n", (long) z->_mp_size);
          printf    ("  want size %ld\n", (long) data[i].want_size);
          mpn_trace ("  got  z", z->_mp_d, z->_mp_size);
          mpn_trace ("  want z", data[i].want_data, data[i].want_size);
          abort();
        }
      mpz_clear (z);

      mpz_init_set_d (z, data[i].d);
      MPZ_CHECK_FORMAT (z);
      if (z->_mp_size != data[i].want_size
          || refmpn_cmp_allowzero (z->_mp_d, data[i].want_data,
                                   ABS (data[i].want_size)) != 0)
        {
          printf ("mpz_init_set_d wrong on data[%d]\n", i);
          goto bad;
        }
      mpz_clear (z);
    }
}

/* Try mpz_set_d on values 2^i+1, while such a value fits a double. */
void
check_2n_plus_1 (void)
{
  volatile double  p, d, diff;
  mpz_t  want, got;
  int    i;

  mpz_init (want);
  mpz_init (got);

  p = 1.0;
  mpz_set_ui (want, 2L);  /* gives 3 on first step */

  for (i = 1; i < 500; i++)
    {
      mpz_mul_2exp (want, want, 1L);
      mpz_sub_ui (want, want, 1L);   /* want = 2^i+1 */

      p *= 2.0;  /* p = 2^i */
      d = p + 1.0;
      diff = d - p;
      if (diff != 1.0)
        break;   /* rounding occurred, stop now */

      mpz_set_d (got, d);
      MPZ_CHECK_FORMAT (got);
      if (mpz_cmp (got, want) != 0)
        {
          printf ("mpz_set_d wrong on 2^%d+1\n", i);
          d_trace   ("  d ", d);
          mpz_trace ("  got  ", got);
          mpz_trace ("  want ", want);
          abort ();
        }
    }

  mpz_clear (want);
  mpz_clear (got);
}

int
main (void)
{
  tests_start ();

  check_data ();
  check_2n_plus_1 ();

  tests_end ();
  exit (0);
}