1
/* Demo program to run expression evaluation. */
4
Copyright 2000, 2001 Free Software Foundation, Inc.
6
This file is part of the GNU MP Library.
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.
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.
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,
25
/* Usage: ./run-expr [-z] [-q] [-f] [-r] [-p prec] [-b base] expression...
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.
33
This is a pretty trivial program, it's just an easy way to experiment
34
with the evaluation functions. */
43
/* only to get HAVE_MPFR, not necessary for normal use */
44
#include "expr-impl.h"
50
#define TRY(exprfun, outfun, str) \
52
ret = exprfun (res, base, str, foo, bar, NULL); \
53
printf ("\"%s\" base %d: ", str, base); \
54
if (ret == MPEXPR_RESULT_OK) \
61
printf ("invalid (return code %d)\n", ret); \
66
main (int argc, char *argv[])
71
int obase, opt, i, ret;
73
while ((opt = getopt (argc, argv, "b:fp:qrz")) != EOF)
94
obase = (base == 0 ? 10 : base);
98
printf ("Usage: %s [-z] [-q] [-f] [-r] [-p prec] [-b base] expression...\n", argv[0]);
109
mpz_init_set_ui (foo, 55L);
110
mpz_init_set_ui (bar, 99L);
112
for (i = optind; i < argc; i++)
113
TRY (mpz_expr, mpz_out_str (stdout, obase, res), argv[i]);
129
mpq_set_ui (foo, 55L, 1);
130
mpq_set_ui (bar, 99L, 1);
132
for (i = optind; i < argc; i++)
133
TRY (mpq_expr, mpq_out_str (stdout, obase, res), argv[i]);
145
mpf_init2 (res, prec);
146
mpf_init_set_ui (foo, 55L);
147
mpf_init_set_ui (bar, 99L);
149
for (i = optind; i < argc; i++)
150
TRY (mpf_expr, mpf_out_str (stdout, obase, 0, res), argv[i]);
161
mpfr_t res, foo, bar;
163
mpfr_init2 (res, prec);
164
mpfr_init_set_ui (foo, 55L, GMP_RNDZ);
165
mpfr_init_set_ui (bar, 99L, GMP_RNDZ);
167
for (i = optind; i < argc; i++)
169
mpfr_out_str (stdout, obase, 0, res, GMP_RNDZ),
177
printf ("mpfr not compiled in\n");