~ubuntu-branches/ubuntu/quantal/gclcvs/quantal

« back to all changes in this revision

Viewing changes to gmp3/demos/expr/run-expr.c

  • Committer: Bazaar Package Importer
  • Author(s): Camm Maguire
  • Date: 2004-06-24 15:13:46 UTC
  • Revision ID: james.westby@ubuntu.com-20040624151346-xh0xaaktyyp7aorc
Tags: 2.7.0-26
C_GC_OFFSET is 2 on m68k-linux

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Demo program to run expression evaluation. */
 
2
 
 
3
/*
 
4
Copyright 2000, 2001 Free Software Foundation, Inc.
 
5
 
 
6
This file is part of the GNU MP Library.
 
7
 
 
8
The GNU MP Library is free software; you can redistribute it and/or modify
 
9
it under the terms of the GNU Lesser General Public License as published by
 
10
the Free Software Foundation; either version 2.1 of the License, or (at your
 
11
option) any later version.
 
12
 
 
13
The GNU MP Library is distributed in the hope that it will be useful, but
 
14
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 
15
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
 
16
License for more details.
 
17
 
 
18
You should have received a copy of the GNU Lesser General Public License
 
19
along with the GNU MP Library; see the file COPYING.LIB.  If not, write to
 
20
the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
 
21
MA 02111-1307, USA.
 
22
*/
 
23
 
 
24
 
 
25
/* Usage: ./run-expr [-z] [-q] [-f] [-r] [-p prec] [-b base] expression...
 
26
 
 
27
   Evaluate each argument as a simple expression.  By default this is in mpz
 
28
   integers, but -q selects mpq, -f selects mpf or -r selects mpfr (if
 
29
   available).  For mpf the float precision can be set with -p.  In all
 
30
   cases the input base can be set with -b, or the default is "0" meaning
 
31
   decimal with "0x" allowed.
 
32
 
 
33
   This is a pretty trivial program, it's just an easy way to experiment
 
34
   with the evaluation functions.  */
 
35
 
 
36
 
 
37
#include <stdio.h>
 
38
#include <stdlib.h>
 
39
#include <unistd.h>
 
40
 
 
41
#include "gmp.h"
 
42
 
 
43
/* only to get HAVE_MPFR, not necessary for normal use */
 
44
#include "expr-impl.h"
 
45
 
 
46
#include "expr.h"
 
47
 
 
48
 
 
49
 
 
50
#define TRY(exprfun, outfun, str)                       \
 
51
  {                                                     \
 
52
    ret = exprfun (res, base, str, foo, bar, NULL);     \
 
53
    printf ("\"%s\" base %d: ", str, base);             \
 
54
    if (ret == MPEXPR_RESULT_OK)                        \
 
55
      {                                                 \
 
56
        printf ("result ");                             \
 
57
        outfun;                                         \
 
58
        printf ("\n");                                  \
 
59
      }                                                 \
 
60
    else                                                \
 
61
      printf ("invalid (return code %d)\n", ret);       \
 
62
  }
 
63
 
 
64
  
 
65
int
 
66
main (int argc, char *argv[])
 
67
{
 
68
  int        type = 'z';
 
69
  int        base = 0;
 
70
  mp_size_t  prec = 64;
 
71
  int        obase, opt, i, ret;
 
72
 
 
73
  while ((opt = getopt (argc, argv, "b:fp:qrz")) != EOF)
 
74
    {
 
75
      switch (opt) {
 
76
      case 'f':
 
77
      case 'q':
 
78
      case 'r':
 
79
      case 'z':
 
80
        type = opt;
 
81
        break;
 
82
      case 'b':
 
83
        base = atoi (optarg);
 
84
        break;
 
85
      case 'p':
 
86
        prec = atoi (optarg);
 
87
        break;
 
88
      case '?':
 
89
      default:
 
90
        abort ();
 
91
      }
 
92
    }
 
93
 
 
94
  obase = (base == 0 ? 10 : base);
 
95
 
 
96
  if (optind >= argc)
 
97
    {
 
98
      printf ("Usage: %s [-z] [-q] [-f] [-r] [-p prec] [-b base] expression...\n", argv[0]);
 
99
      exit (1);
 
100
    }
 
101
 
 
102
  switch (type) {
 
103
  case 'z':
 
104
  default:
 
105
    {
 
106
      mpz_t  res, foo, bar;
 
107
 
 
108
      mpz_init (res);
 
109
      mpz_init_set_ui (foo, 55L);
 
110
      mpz_init_set_ui (bar, 99L);
 
111
 
 
112
      for (i = optind; i < argc; i++)
 
113
        TRY (mpz_expr, mpz_out_str (stdout, obase, res), argv[i]);
 
114
 
 
115
      mpz_clear (res);
 
116
      mpz_clear (foo);
 
117
      mpz_clear (bar);
 
118
    }
 
119
    break;
 
120
 
 
121
  case 'q':
 
122
    {
 
123
      mpq_t  res, foo, bar;
 
124
 
 
125
      mpq_init (res);
 
126
      mpq_init (foo);
 
127
      mpq_init (bar);
 
128
 
 
129
      mpq_set_ui (foo, 55L, 1);
 
130
      mpq_set_ui (bar, 99L, 1);
 
131
 
 
132
      for (i = optind; i < argc; i++)
 
133
        TRY (mpq_expr, mpq_out_str (stdout, obase, res), argv[i]);
 
134
 
 
135
      mpq_clear (res);
 
136
      mpq_clear (foo);
 
137
      mpq_clear (bar);
 
138
    }
 
139
    break;
 
140
 
 
141
  case 'f':
 
142
    {
 
143
      mpf_t  res, foo, bar;
 
144
 
 
145
      mpf_init2 (res, prec);
 
146
      mpf_init_set_ui (foo, 55L);
 
147
      mpf_init_set_ui (bar, 99L);
 
148
 
 
149
      for (i = optind; i < argc; i++)
 
150
        TRY (mpf_expr, mpf_out_str (stdout, obase, 0, res), argv[i]);
 
151
 
 
152
      mpf_clear (res);
 
153
      mpf_clear (foo);
 
154
      mpf_clear (bar);
 
155
    }
 
156
    break;
 
157
 
 
158
  case 'r':
 
159
#if HAVE_MPFR
 
160
    {
 
161
      mpfr_t  res, foo, bar;
 
162
 
 
163
      mpfr_init2 (res, prec);
 
164
      mpfr_init_set_ui (foo, 55L, GMP_RNDZ);
 
165
      mpfr_init_set_ui (bar, 99L, GMP_RNDZ);
 
166
 
 
167
      for (i = optind; i < argc; i++)
 
168
        TRY (mpfr_expr,
 
169
             mpfr_out_str (stdout, obase, 0, res, GMP_RNDZ),
 
170
             argv[i]);
 
171
 
 
172
      mpfr_clear (res);
 
173
      mpfr_clear (foo);
 
174
      mpfr_clear (bar);
 
175
    }
 
176
#else
 
177
    printf ("mpfr not compiled in\n");
 
178
    exit (1);
 
179
#endif
 
180
    break;
 
181
  }
 
182
 
 
183
  return 0;
 
184
}