[BACK]Return to exprq.c CVS log [TXT][DIR] Up to [local] / OpenXM_contrib / gmp / demos / expr

Annotation of OpenXM_contrib/gmp/demos/expr/exprq.c, Revision 1.1.1.1

1.1       ohara       1: /* mpq 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: #include <stdio.h>
                     26: #include "gmp.h"
                     27: #include "expr-impl.h"
                     28:
                     29:
                     30: /* Change this to "#define TRACE(x) x" to get some traces. */
                     31: #define TRACE(x)
                     32:
                     33:
                     34: static void
                     35: e_mpq_pow_ui (mpq_ptr r, mpq_srcptr b, unsigned long e)
                     36: {
                     37:   mpz_pow_ui (mpq_numref(r), mpq_numref(b), e);
                     38:   mpz_pow_ui (mpq_denref(r), mpq_denref(b), e);
                     39: }
                     40:
                     41: /* Wrapped because mpq_sgn is a macro. */
                     42: static int
                     43: e_mpq_sgn (mpq_srcptr x)
                     44: {
                     45:   return mpq_sgn (x);
                     46: }
                     47:
                     48: /* Wrapped because mpq_equal only guarantees a non-zero return, whereas we
                     49:    want 1 or 0 for == and !=. */
                     50: static int
                     51: e_mpq_equal (mpq_srcptr x, mpq_srcptr y)
                     52: {
                     53:   return mpq_equal (x, y) != 0;
                     54: }
                     55: static int
                     56: e_mpq_notequal (mpq_srcptr x, mpq_srcptr y)
                     57: {
                     58:   return ! mpq_equal (x, y);
                     59: }
                     60:
                     61: static void
                     62: e_mpq_num (mpq_ptr w, mpq_srcptr x)
                     63: {
                     64:   if (w != x)
                     65:     mpz_set (mpq_numref(w), mpq_numref(x));
                     66:   mpz_set_ui (mpq_denref(w), 1L);
                     67: }
                     68: static void
                     69: e_mpq_den (mpq_ptr w, mpq_srcptr x)
                     70: {
                     71:   if (w == x)
                     72:     mpz_swap (mpq_numref(w), mpq_denref(w));
                     73:   else
                     74:     mpz_set (mpq_numref(w), mpq_denref(x));
                     75:   mpz_set_ui (mpq_denref(w), 1L);
                     76: }
                     77:
                     78:
                     79: static __gmp_const struct mpexpr_operator_t  _mpq_expr_standard_table[] = {
                     80:
                     81:   { "**",  (mpexpr_fun_t) e_mpq_pow_ui,
                     82:     MPEXPR_TYPE_BINARY_UI | MPEXPR_TYPE_RIGHTASSOC,                   220 },
                     83:
                     84:   { "!",   (mpexpr_fun_t) e_mpq_sgn,
                     85:     MPEXPR_TYPE_LOGICAL_NOT | MPEXPR_TYPE_PREFIX,                     210 },
                     86:   { "-",   (mpexpr_fun_t) mpq_neg,
                     87:     MPEXPR_TYPE_UNARY | MPEXPR_TYPE_PREFIX,                           210 },
                     88:
                     89:   { "*",   (mpexpr_fun_t) mpq_mul,           MPEXPR_TYPE_BINARY,      200 },
                     90:   { "/",   (mpexpr_fun_t) mpq_div,           MPEXPR_TYPE_BINARY,      200 },
                     91:
                     92:   { "+",   (mpexpr_fun_t) mpq_add,           MPEXPR_TYPE_BINARY,      190 },
                     93:   { "-",   (mpexpr_fun_t) mpq_sub,           MPEXPR_TYPE_BINARY,      190 },
                     94:
                     95:   { "<<",  (mpexpr_fun_t) mpq_mul_2exp,      MPEXPR_TYPE_BINARY_UI,   180 },
                     96:   { ">>",  (mpexpr_fun_t) mpq_div_2exp,      MPEXPR_TYPE_BINARY_UI,   180 },
                     97:
                     98:   { "<=",  (mpexpr_fun_t) mpq_cmp,           MPEXPR_TYPE_CMP_LE,      170 },
                     99:   { "<",   (mpexpr_fun_t) mpq_cmp,           MPEXPR_TYPE_CMP_LT,      170 },
                    100:   { ">=",  (mpexpr_fun_t) mpq_cmp,           MPEXPR_TYPE_CMP_GE,      170 },
                    101:   { ">",   (mpexpr_fun_t) mpq_cmp,           MPEXPR_TYPE_CMP_GT,      170 },
                    102:
                    103:   { "==",  (mpexpr_fun_t) e_mpq_equal,       MPEXPR_TYPE_I_BINARY,    160 },
                    104:   { "!=",  (mpexpr_fun_t) e_mpq_notequal,    MPEXPR_TYPE_I_BINARY,    160 },
                    105:
                    106:   { "&&",  (mpexpr_fun_t) e_mpq_sgn,         MPEXPR_TYPE_LOGICAL_AND, 120 },
                    107:   { "||",  (mpexpr_fun_t) e_mpq_sgn,         MPEXPR_TYPE_LOGICAL_OR,  110 },
                    108:
                    109:   { ":",   NULL,                             MPEXPR_TYPE_COLON,       101 },
                    110:   { "?",   (mpexpr_fun_t) e_mpq_sgn,         MPEXPR_TYPE_QUESTION,    100 },
                    111:
                    112:   { ")",   (mpexpr_fun_t) e_mpq_sgn,         MPEXPR_TYPE_CLOSEPAREN,    4 },
                    113:   { "(",   (mpexpr_fun_t) e_mpq_sgn,         MPEXPR_TYPE_OPENPAREN,     3 },
                    114:   { ",",   (mpexpr_fun_t) e_mpq_sgn,         MPEXPR_TYPE_ARGSEP,        2 },
                    115:   { "$",   NULL,                             MPEXPR_TYPE_VARIABLE,      1 },
                    116:
                    117:   { "abs",  (mpexpr_fun_t) mpq_abs,          MPEXPR_TYPE_UNARY            },
                    118:   { "cmp",  (mpexpr_fun_t) mpq_cmp,          MPEXPR_TYPE_I_BINARY         },
                    119:   { "den",  (mpexpr_fun_t) e_mpq_den,        MPEXPR_TYPE_UNARY            },
                    120:   { "max",  (mpexpr_fun_t) mpq_cmp,  MPEXPR_TYPE_MAX | MPEXPR_TYPE_PAIRWISE },
                    121:   { "min",  (mpexpr_fun_t) mpq_cmp,  MPEXPR_TYPE_MIN | MPEXPR_TYPE_PAIRWISE },
                    122:   { "num",  (mpexpr_fun_t) e_mpq_num,        MPEXPR_TYPE_UNARY            },
                    123:   { "sgn",  (mpexpr_fun_t) e_mpq_sgn,        MPEXPR_TYPE_I_UNARY          },
                    124:
                    125:   { NULL }
                    126: };
                    127:
                    128: __gmp_const struct mpexpr_operator_t * __gmp_const mpq_expr_standard_table
                    129: = _mpq_expr_standard_table;
                    130:
                    131:
                    132: int
                    133: #if HAVE_STDARG
                    134: mpq_expr (mpq_ptr res, int base, __gmp_const char *e, ...)
                    135: #else
                    136: mpq_expr (va_alist)
                    137:      va_dcl
                    138: #endif
                    139: {
                    140:   mpq_srcptr  var[MPEXPR_VARIABLES];
                    141:   va_list     ap;
                    142:   int         ret;
                    143: #if HAVE_STDARG
                    144:   va_start (ap, e);
                    145: #else
                    146:   mpq_ptr           res;
                    147:   int               base;
                    148:   __gmp_const char  *e;
                    149:   va_start (ap);
                    150:   res  = va_arg (ap, mpq_ptr);
                    151:   base = va_arg (ap, int);
                    152:   e    = va_arg (ap, __gmp_const char *);
                    153: #endif
                    154:
                    155:   TRACE (printf ("mpq_expr(): base %d, %s\n", base, e));
                    156:   ret = mpexpr_va_to_var ((void **) var, ap);
                    157:   va_end (ap);
                    158:
                    159:   if (ret != MPEXPR_RESULT_OK)
                    160:     return ret;
                    161:
                    162:   return mpq_expr_a (mpq_expr_standard_table, res, base, e, strlen(e), var);
                    163: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>