~ubuntu-branches/debian/sid/genius/sid

« back to all changes in this revision

Viewing changes to mpfr/tests/tset_z.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Holbach
  • Date: 2006-08-21 12:57:45 UTC
  • Revision ID: james.westby@ubuntu.com-20060821125745-sl9ks8v7fq324bdf
Tags: upstream-0.7.6.1
ImportĀ upstreamĀ versionĀ 0.7.6.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Test file for mpfr_set_z.
 
2
 
 
3
Copyright 1999, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
 
4
 
 
5
This file is part of the MPFR Library.
 
6
 
 
7
The MPFR 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 2.1 of the License, or (at your
 
10
option) any later version.
 
11
 
 
12
The MPFR 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 MPFR Library; see the file COPYING.LIB.  If not, write to
 
19
the Free Software Foundation, Inc., 51 Franklin Place, Fifth Floor, Boston,
 
20
MA 02110-1301, USA. */
 
21
 
 
22
#include <stdio.h>
 
23
#include <stdlib.h>
 
24
#include <limits.h>
 
25
#include <time.h>
 
26
 
 
27
#include "mpfr-test.h"
 
28
 
 
29
static void check0(void)
 
30
{
 
31
  mpz_t y;
 
32
  mpfr_t x;
 
33
  int inexact, r;
 
34
 
 
35
  /* Check for +0 */
 
36
  mpfr_init (x);
 
37
  mpz_init (y);
 
38
  mpz_set_si (y, 0);
 
39
  for(r = 0; r < GMP_RND_MAX; r++)
 
40
    {
 
41
      inexact = mpfr_set_z (x, y, (mp_rnd_t) r);
 
42
      if (!MPFR_IS_ZERO(x) || !MPFR_IS_POS(x) || inexact)
 
43
        {
 
44
          printf("mpfr_set_z(x,0) failed for %s\n",
 
45
                 mpfr_print_rnd_mode ((mp_rnd_t) r));
 
46
          exit(1);
 
47
        }
 
48
    }
 
49
  mpfr_clear(x);
 
50
  mpz_clear(y);
 
51
}
 
52
 
 
53
/* FIXME: It'd be better to examine the actual data in an mpfr_t to see that
 
54
   it's as expected.  Comparing mpfr_set_z with mpfr_cmp or against
 
55
   mpfr_get_si is a rather indirect test of a low level routine.  */
 
56
 
 
57
static void
 
58
check (long i, mp_rnd_t rnd)
 
59
{
 
60
  mpfr_t f;
 
61
  mpz_t z;
 
62
 
 
63
  mpfr_init2 (f, 8 * sizeof(long));
 
64
  mpz_init (z);
 
65
  mpz_set_ui (z, i);
 
66
  mpfr_set_z (f, z, rnd);
 
67
  if (mpfr_get_si (f, GMP_RNDZ) != i)
 
68
    {
 
69
      printf ("Error in mpfr_set_z for i=%ld rnd_mode=%d\n", i, rnd);
 
70
      exit (1);
 
71
    }
 
72
  mpfr_clear (f);
 
73
  mpz_clear (z);
 
74
}
 
75
 
 
76
static void
 
77
check_large (void)
 
78
{
 
79
  mpz_t z;
 
80
  mpfr_t x, y;
 
81
  mp_exp_t emax, emin;
 
82
 
 
83
  mpz_init (z);
 
84
  mpfr_init2 (x, 160);
 
85
  mpfr_init2 (y, 160);
 
86
 
 
87
  mpz_set_str (z, "77031627725494291259359895954016675357279104942148788042", 10);
 
88
  mpfr_set_z (x, z, GMP_RNDN);
 
89
  mpfr_set_str_binary (y, "0.1100100100001111110110101010001000100001011010001100001000110100110001001100011001100010100010111000000011011100000111001101000100101001000000100100111000001001E186");
 
90
  if (mpfr_cmp (x, y))
 
91
    {
 
92
      printf ("Error in mpfr_set_z on large input\n");
 
93
      exit (1);
 
94
    }
 
95
 
 
96
  /* check overflow */
 
97
  emax = mpfr_get_emax ();
 
98
  set_emax (2);
 
99
  mpz_set_str (z, "7", 10);
 
100
  mpfr_set_z (x, z, GMP_RNDU);
 
101
  MPFR_ASSERTN(mpfr_inf_p (x) && mpfr_sgn (x) > 0);
 
102
  set_emax (3);
 
103
  mpfr_set_prec (x, 2);
 
104
  mpz_set_str (z, "7", 10);
 
105
  mpfr_set_z (x, z, GMP_RNDU);
 
106
  MPFR_ASSERTN(mpfr_inf_p (x) && mpfr_sgn (x) > 0);
 
107
  set_emax (emax);
 
108
 
 
109
  /* check underflow */
 
110
  emin = mpfr_get_emin ();
 
111
  set_emin (3);
 
112
  mpz_set_str (z, "1", 10);
 
113
  mpfr_set_z (x, z, GMP_RNDZ);
 
114
  MPFR_ASSERTN(mpfr_cmp_ui (x, 0) == 0 && MPFR_IS_POS(x));
 
115
  set_emin (2);
 
116
  mpfr_set_z (x, z, GMP_RNDN);
 
117
  MPFR_ASSERTN(mpfr_cmp_ui (x, 0) == 0 && MPFR_IS_POS(x));
 
118
  set_emin (emin);
 
119
 
 
120
  mpz_clear (z);
 
121
 
 
122
  mpfr_clear (x);
 
123
  mpfr_clear (y);
 
124
}
 
125
 
 
126
int
 
127
main (int argc, char *argv[])
 
128
{
 
129
  long j;
 
130
 
 
131
  tests_start_mpfr ();
 
132
 
 
133
  check_large ();
 
134
  check (0, GMP_RNDN);
 
135
  for (j = 0; j < 200000; j++)
 
136
    check (randlimb () & LONG_MAX, (mp_rnd_t) RND_RAND ());
 
137
  check0 ();
 
138
 
 
139
  tests_end_mpfr ();
 
140
 
 
141
  return 0;
 
142
}