Upload Tizen:Base source
[external/gmp.git] / tests / mpz / t-get_d.c
1 /* Test mpz_get_d.
2
3 Copyright 2002 Free Software Foundation, Inc.
4
5 This file is part of the GNU MP Library.
6
7 The GNU MP Library is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11
12 The GNU MP Library is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
15 License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with the GNU MP Library.  If not, see http://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 #ifdef __vax
35   int     limit = 127;  /* 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 }