Blame demos/expr/exprqa.c

Packit 5c3484
/* mpq expression evaluation
Packit 5c3484
Packit 5c3484
Copyright 2000, 2001, 2004 Free Software Foundation, Inc.
Packit 5c3484
Packit 5c3484
This file is part of the GNU MP Library.
Packit 5c3484
Packit 5c3484
The GNU MP Library is free software; you can redistribute it and/or modify
Packit 5c3484
it under the terms of either:
Packit 5c3484
Packit 5c3484
  * the GNU Lesser General Public License as published by the Free
Packit 5c3484
    Software Foundation; either version 3 of the License, or (at your
Packit 5c3484
    option) any later version.
Packit 5c3484
Packit 5c3484
or
Packit 5c3484
Packit 5c3484
  * the GNU General Public License as published by the Free Software
Packit 5c3484
    Foundation; either version 2 of the License, or (at your option) any
Packit 5c3484
    later version.
Packit 5c3484
Packit 5c3484
or both in parallel, as here.
Packit 5c3484
Packit 5c3484
The GNU MP Library is distributed in the hope that it will be useful, but
Packit 5c3484
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
Packit 5c3484
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
Packit 5c3484
for more details.
Packit 5c3484
Packit 5c3484
You should have received copies of the GNU General Public License and the
Packit 5c3484
GNU Lesser General Public License along with the GNU MP Library.  If not,
Packit 5c3484
see https://www.gnu.org/licenses/.  */
Packit 5c3484
Packit 5c3484
Packit 5c3484
#include <stdio.h>
Packit 5c3484
#include "gmp.h"
Packit 5c3484
#include "expr-impl.h"
Packit 5c3484
Packit 5c3484
Packit 5c3484
static int
Packit 5c3484
e_mpq_ulong_p (mpq_srcptr q)
Packit 5c3484
{
Packit 5c3484
  return mpz_fits_ulong_p (mpq_numref (q))
Packit 5c3484
    && mpz_cmp_ui (mpq_denref (q), 1L) == 0;
Packit 5c3484
}
Packit 5c3484
Packit 5c3484
/* get value as a ui, on the assumption it fits */
Packit 5c3484
static int
Packit 5c3484
e_mpq_get_ui_fits (mpq_srcptr q)
Packit 5c3484
{
Packit 5c3484
  return mpz_get_ui (mpq_numref (q));
Packit 5c3484
}
Packit 5c3484
Packit 5c3484
static void
Packit 5c3484
e_mpq_set_si1 (mpq_ptr q, long num)
Packit 5c3484
{
Packit 5c3484
  mpq_set_si (q, num, 1L);
Packit 5c3484
}
Packit 5c3484
Packit 5c3484
/* The same as mpz, but putting the result in the numerator.  Negatives and
Packit 5c3484
   fractions aren't parsed here because '-' and '/' are operators. */
Packit 5c3484
static size_t
Packit 5c3484
e_mpq_number (mpq_ptr res, const char *e, size_t elen, int base)
Packit 5c3484
{
Packit 5c3484
  mpz_set_ui (mpq_denref (res), 1L);
Packit 5c3484
  return mpexpr_mpz_number (mpq_numref (res), e, elen, base);
Packit 5c3484
}
Packit 5c3484
Packit 5c3484
Packit 5c3484
/* ignoring prec */
Packit 5c3484
static void
Packit 5c3484
e_mpq_init (mpq_ptr q, unsigned long prec)
Packit 5c3484
{
Packit 5c3484
  mpq_init (q);
Packit 5c3484
}
Packit 5c3484
Packit 5c3484
int
Packit 5c3484
mpq_expr_a (const struct mpexpr_operator_t *table,
Packit 5c3484
            mpq_ptr res, int base,
Packit 5c3484
            const char *e, size_t elen,
Packit 5c3484
            mpq_srcptr var[26])
Packit 5c3484
{
Packit 5c3484
  struct mpexpr_parse_t  p;
Packit 5c3484
Packit 5c3484
  p.table = table;
Packit 5c3484
  p.res = (mpX_ptr) res;
Packit 5c3484
  p.base = base;
Packit 5c3484
  p.e = e;
Packit 5c3484
  p.elen = elen;
Packit 5c3484
  p.var = (mpX_srcptr *) var;
Packit 5c3484
Packit 5c3484
  p.mpX_clear       = (mpexpr_fun_one_t)      mpq_clear;
Packit 5c3484
  p.mpX_ulong_p     = (mpexpr_fun_i_unary_t)  e_mpq_ulong_p;
Packit 5c3484
  p.mpX_get_ui      = (mpexpr_fun_get_ui_t)   e_mpq_get_ui_fits;
Packit 5c3484
  p.mpX_init        = (mpexpr_fun_unary_ui_t) e_mpq_init;
Packit 5c3484
  p.mpX_number      = (mpexpr_fun_number_t)   e_mpq_number;
Packit 5c3484
  p.mpX_set         = (mpexpr_fun_unary_t)    mpq_set;
Packit 5c3484
  p.mpX_set_or_swap = (mpexpr_fun_unary_t)    mpq_swap;
Packit 5c3484
  p.mpX_set_si      = (mpexpr_fun_set_si_t)   e_mpq_set_si1;
Packit 5c3484
  p.mpX_swap        = (mpexpr_fun_swap_t)     mpq_swap;
Packit 5c3484
Packit 5c3484
  return mpexpr_evaluate (&p);
Packit 5c3484
}