source: trunk/third/gmp/tests/mpz/t-set_f.c @ 18191

Revision 18191, 2.9 KB checked in by ghudson, 22 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r18190, which included commits to RCS files with non-trunk default branches.
Line 
1/* Test mpz_set_f.
2
3Copyright 2001 Free Software Foundation, Inc.
4
5This file is part of the GNU MP Library.
6
7The GNU MP Library is free software; you can redistribute it and/or modify
8it under the terms of the GNU Lesser General Public License as published by
9the Free Software Foundation; either version 2.1 of the License, or (at your
10option) any later version.
11
12The GNU MP Library is distributed in the hope that it will be useful, but
13WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
15License for more details.
16
17You should have received a copy of the GNU Lesser General Public License
18along with the GNU MP Library; see the file COPYING.LIB.  If not, write to
19the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20MA 02111-1307, USA. */
21
22#include <stdio.h>
23#include <stdlib.h>
24#include "gmp.h"
25#include "gmp-impl.h"
26#include "tests.h"
27
28
29void
30check_one (mpz_srcptr z)
31{
32  static const int shift[] = {
33    0, 1, BITS_PER_MP_LIMB, 2*BITS_PER_MP_LIMB, 5*BITS_PER_MP_LIMB
34  };
35
36  int    sh, shneg, neg;
37  mpf_t  f;
38  mpz_t  got, want;
39
40  mpf_init2 (f, mpz_sizeinbase(z,2));
41  mpz_init (got);
42  mpz_init (want);
43
44  for (sh = 0; sh < numberof(shift); sh++)
45    {
46      for (shneg = 0; shneg <= 1; shneg++)
47        {
48          for (neg = 0; neg <= 1; neg++)
49            {
50              mpf_set_z (f, z);
51              mpz_set (want, z);
52           
53              if (neg)
54                {
55                  mpf_neg (f, f);
56                  mpz_neg (want, want);
57                }
58
59              if (shneg)
60                {
61                  mpz_tdiv_q_2exp (want, want, shift[sh]);
62                  mpf_div_2exp (f, f, shift[sh]);
63                }
64              else
65                {
66                  mpz_mul_2exp (want, want, shift[sh]);
67                  mpf_mul_2exp (f, f, shift[sh]);
68                }
69
70              mpz_set_f (got, f);
71              MPZ_CHECK_FORMAT (got);
72
73              if (mpz_cmp (got, want) != 0)
74                {
75                  printf ("wrong result\n");
76                  printf ("  shift  %d\n", shneg ? -shift[sh] : shift[sh]);
77                  printf ("  neg    %d\n", neg);
78                  mpf_trace ("     f", f);
79                  mpz_trace ("   got", got);
80                  mpz_trace ("  want", want);
81                  abort ();
82                }
83            }
84        }
85    }
86
87  mpf_clear (f);
88  mpz_clear (got);
89  mpz_clear (want);
90}
91
92
93void
94check_various (void)
95{
96  mpz_t  z;
97
98  mpz_init (z);
99
100  mpz_set_ui (z, 0L);
101  check_one (z);
102
103  mpz_set_si (z, 123L);
104  check_one (z);
105
106  mpz_rrandomb (z, RANDS, 2*BITS_PER_MP_LIMB);
107  check_one (z);
108
109  mpz_rrandomb (z, RANDS, 5*BITS_PER_MP_LIMB);
110  check_one (z);
111
112  mpz_clear (z);
113}
114
115
116int
117main (int argc, char *argv[])
118{
119#if GMP_NAIL_BITS == 0
120  tests_start ();
121  mp_trace_base = 16;
122
123  check_various ();
124
125  tests_end ();
126#endif
127  exit (0);
128}
Note: See TracBrowser for help on using the repository browser.