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