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

Annotation of OpenXM_contrib/gmp/demos/expr/exprqa.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: static int
                     31: e_mpq_ulong_p (mpq_srcptr q)
                     32: {
                     33:   return mpz_fits_ulong_p (mpq_numref (q))
                     34:     && mpz_cmp_ui (mpq_denref (q), 1L) == 0;
                     35: }
                     36:
                     37: /* get value as a ui, on the assumption it fits */
                     38: static int
                     39: e_mpq_get_ui_fits (mpq_srcptr q)
                     40: {
                     41:   return mpz_get_ui (mpq_numref (q));
                     42: }
                     43:
                     44: static void
                     45: e_mpq_set_si1 (mpq_ptr q, long num)
                     46: {
                     47:   mpq_set_si (q, num, 1L);
                     48: }
                     49:
                     50: /* The same as mpz, but putting the result in the numerator.  Negatives and
                     51:    fractions aren't parsed here because '-' and '/' are operators. */
                     52: static size_t
                     53: e_mpq_number (mpq_ptr res, __gmp_const char *e, size_t elen, int base)
                     54: {
                     55:   mpz_set_ui (mpq_denref (res), 1L);
                     56:   return mpexpr_mpz_number (mpq_numref (res), e, elen, base);
                     57: }
                     58:
                     59:
                     60: int
                     61: mpq_expr_a (__gmp_const struct mpexpr_operator_t *table,
                     62:             mpq_ptr res, int base,
                     63:             __gmp_const char *e, size_t elen,
                     64:             mpq_srcptr var[26])
                     65: {
                     66:   struct mpexpr_parse_t  p;
                     67:
                     68:   p.table = table;
                     69:   p.res = (mpX_ptr) res;
                     70:   p.base = base;
                     71:   p.e = e;
                     72:   p.elen = elen;
                     73:   p.var = (mpX_srcptr *) var;
                     74:
                     75:   p.mpX_clear       = (mpexpr_fun_one_t)      mpq_clear;
                     76:   p.mpX_ulong_p     = (mpexpr_fun_i_unary_t)  e_mpq_ulong_p;
                     77:   p.mpX_get_ui      = (mpexpr_fun_get_ui_t)   e_mpq_get_ui_fits;
                     78:   p.mpX_init        = (mpexpr_fun_unary_ui_t) mpq_init;
                     79:   p.mpX_number      = (mpexpr_fun_number_t)   e_mpq_number;
                     80:   p.mpX_set         = (mpexpr_fun_unary_t)    mpq_set;
                     81:   p.mpX_set_or_swap = (mpexpr_fun_unary_t)    mpq_swap;
                     82:   p.mpX_set_si      = (mpexpr_fun_set_si_t)   e_mpq_set_si1;
                     83:   p.mpX_swap        = (mpexpr_fun_swap_t)     mpq_swap;
                     84:
                     85:   return mpexpr_evaluate (&p);
                     86: }

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