summaryrefslogtreecommitdiff
path: root/tests/tget_str.c
blob: 7eceb19c0fce0cf7863282056867d2958160cad5 (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
/* Test file for mpfr_get_str.

Copyright 1999, 2001, 2002 Free Software Foundation, Inc.

This file is part of the MPFR Library.

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

The MPFR Library 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 Lesser General Public
License for more details.

You should have received a copy of the GNU Lesser General Public License
along with the MPFR Library; see the file COPYING.  If not, write to
the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA. */

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "gmp.h"
#include "mpfr.h"
#ifdef HAVE_FENV_H
#include "mpfr-test.h"
#endif

void check _PROTO((double, mp_rnd_t)); 
void check3 _PROTO((double, mp_rnd_t, char *)); 
void check_small _PROTO((void)); 

void
check (double d, mp_rnd_t rnd)
{
  mpfr_t x; char *str; mp_exp_t e;

  mpfr_init2(x, 53);
  mpfr_set_d(x, d, rnd);
  str = mpfr_get_str(NULL, &e, 10, 5, x, rnd);
  mpfr_clear(x);
  free(str);
}

void
check3 (double d, mp_rnd_t rnd, char *res)
{
  mpfr_t x; char *str; mp_exp_t e;

  mpfr_init2 (x, 53);
  mpfr_set_d (x, d, rnd);
  str = mpfr_get_str (NULL, &e, 10, 5, x, rnd);
  if (strcmp(str, res))
    {
      fprintf (stderr, "Error in mpfr_get_str for x=%1.20e\n", d);
      fprintf (stderr, "got %s instead of %s\n", str, res);
    }
  mpfr_clear (x);
  free (str);
}

void
check_small (void)
{
  mpfr_t x;
  char *s;
  mp_exp_t e;
  
  mpfr_init(x);

  /* problem found by Fabrice Rouillier */
  mpfr_set_prec(x, 63);
  mpfr_set_d(x, 5e14, GMP_RNDN);
  s = mpfr_get_str(NULL, &e, 10, 18, x, GMP_RNDU);
  free(s);

  /* bug found by Johan Vervloet */
  mpfr_set_prec(x, 6);
  mpfr_set_d(x, 688.0, GMP_RNDN);
  s = mpfr_get_str(NULL, &e, 2, 4, x, GMP_RNDU);
  if (strcmp(s, "1011") || (e!=10)) {
    fprintf(stderr, "Error in mpfr_get_str: 688 printed up to 4 bits should give 1.011e9\ninstead of ");
    mpfr_out_str(stderr, 2, 4, x, GMP_RNDU); putchar('\n');
    exit(1);
  }
  free(s);

  mpfr_set_prec (x, 38);
  mpfr_set_str_raw (x, "1.0001110111110100011010100010010100110e-6");
  s = mpfr_get_str (NULL, &e, 8, 10, x, GMP_RNDU);
  if (strcmp (s, "1073721522") || (e != -1))
    {
      fprintf (stderr, "Error in mpfr_get_str (3): s=%s e=%d\n", s, (int) e);
      exit (1);
    }
  free (s);

  mpfr_clear(x);
}

int
main (int argc, char *argv[])
{
#ifdef HAVE_FENV_H
  int i;
  double d;

  SEED_RAND (time(NULL));
  for (i=0;i<100000;i++) {
    do { d = drand(); } while (isnan(d));
    check(d, GMP_RNDN);
  }
#endif
  check_small();
  check3(4.059650008e-83, GMP_RNDN, "40597");
  check3(-6.606499965302424244461355e233, GMP_RNDN, "-66065");
  check3(-7.4, GMP_RNDN, "-74000");
  check3(0.997, GMP_RNDN, "99700");
  check3(-4.53063926135729747564e-308, GMP_RNDN, "-45306");
  check3(2.14478198760196000000e+16, GMP_RNDN, "21448");
  check3(7.02293374921793516813e-84, GMP_RNDN, "70229");
  check3(-6.7274500420134077e-87, GMP_RNDN, "-67275"); 

  return 0;
}