GAP 4.8.9 installation with standard packages -- copy to your CoCalc project to get it
/* mpq expression evaluation12Copyright 2000-2002 Free Software Foundation, Inc.34This file is part of the GNU MP Library.56The GNU MP Library is free software; you can redistribute it and/or modify7it under the terms of either:89* the GNU Lesser General Public License as published by the Free10Software Foundation; either version 3 of the License, or (at your11option) any later version.1213or1415* the GNU General Public License as published by the Free Software16Foundation; either version 2 of the License, or (at your option) any17later version.1819or both in parallel, as here.2021The GNU MP Library is distributed in the hope that it will be useful, but22WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY23or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License24for more details.2526You should have received copies of the GNU General Public License and the27GNU Lesser General Public License along with the GNU MP Library. If not,28see https://www.gnu.org/licenses/. */2930#include <stdio.h>31#include <string.h>32#include "gmp.h"33#include "expr-impl.h"343536/* Change this to "#define TRACE(x) x" to get some traces. */37#define TRACE(x)383940static void41e_mpq_pow_ui (mpq_ptr r, mpq_srcptr b, unsigned long e)42{43mpz_pow_ui (mpq_numref(r), mpq_numref(b), e);44mpz_pow_ui (mpq_denref(r), mpq_denref(b), e);45}4647/* Wrapped because mpq_sgn is a macro. */48static int49e_mpq_sgn (mpq_srcptr x)50{51return mpq_sgn (x);52}5354/* Wrapped because mpq_equal only guarantees a non-zero return, whereas we55want 1 or 0 for == and !=. */56static int57e_mpq_equal (mpq_srcptr x, mpq_srcptr y)58{59return mpq_equal (x, y) != 0;60}61static int62e_mpq_notequal (mpq_srcptr x, mpq_srcptr y)63{64return ! mpq_equal (x, y);65}6667static void68e_mpq_num (mpq_ptr w, mpq_srcptr x)69{70if (w != x)71mpz_set (mpq_numref(w), mpq_numref(x));72mpz_set_ui (mpq_denref(w), 1L);73}74static void75e_mpq_den (mpq_ptr w, mpq_srcptr x)76{77if (w == x)78mpz_swap (mpq_numref(w), mpq_denref(w));79else80mpz_set (mpq_numref(w), mpq_denref(x));81mpz_set_ui (mpq_denref(w), 1L);82}838485static const struct mpexpr_operator_t _mpq_expr_standard_table[] = {8687{ "**", (mpexpr_fun_t) e_mpq_pow_ui,88MPEXPR_TYPE_BINARY_UI | MPEXPR_TYPE_RIGHTASSOC, 220 },8990{ "!", (mpexpr_fun_t) e_mpq_sgn,91MPEXPR_TYPE_LOGICAL_NOT | MPEXPR_TYPE_PREFIX, 210 },92{ "-", (mpexpr_fun_t) mpq_neg,93MPEXPR_TYPE_UNARY | MPEXPR_TYPE_PREFIX, 210 },9495{ "*", (mpexpr_fun_t) mpq_mul, MPEXPR_TYPE_BINARY, 200 },96{ "/", (mpexpr_fun_t) mpq_div, MPEXPR_TYPE_BINARY, 200 },9798{ "+", (mpexpr_fun_t) mpq_add, MPEXPR_TYPE_BINARY, 190 },99{ "-", (mpexpr_fun_t) mpq_sub, MPEXPR_TYPE_BINARY, 190 },100101{ "<<", (mpexpr_fun_t) mpq_mul_2exp, MPEXPR_TYPE_BINARY_UI, 180 },102{ ">>", (mpexpr_fun_t) mpq_div_2exp, MPEXPR_TYPE_BINARY_UI, 180 },103104{ "<=", (mpexpr_fun_t) mpq_cmp, MPEXPR_TYPE_CMP_LE, 170 },105{ "<", (mpexpr_fun_t) mpq_cmp, MPEXPR_TYPE_CMP_LT, 170 },106{ ">=", (mpexpr_fun_t) mpq_cmp, MPEXPR_TYPE_CMP_GE, 170 },107{ ">", (mpexpr_fun_t) mpq_cmp, MPEXPR_TYPE_CMP_GT, 170 },108109{ "==", (mpexpr_fun_t) e_mpq_equal, MPEXPR_TYPE_I_BINARY, 160 },110{ "!=", (mpexpr_fun_t) e_mpq_notequal, MPEXPR_TYPE_I_BINARY, 160 },111112{ "&&", (mpexpr_fun_t) e_mpq_sgn, MPEXPR_TYPE_LOGICAL_AND, 120 },113{ "||", (mpexpr_fun_t) e_mpq_sgn, MPEXPR_TYPE_LOGICAL_OR, 110 },114115{ ":", NULL, MPEXPR_TYPE_COLON, 101 },116{ "?", (mpexpr_fun_t) e_mpq_sgn, MPEXPR_TYPE_QUESTION, 100 },117118{ ")", (mpexpr_fun_t) e_mpq_sgn, MPEXPR_TYPE_CLOSEPAREN, 4 },119{ "(", (mpexpr_fun_t) e_mpq_sgn, MPEXPR_TYPE_OPENPAREN, 3 },120{ ",", (mpexpr_fun_t) e_mpq_sgn, MPEXPR_TYPE_ARGSEP, 2 },121{ "$", NULL, MPEXPR_TYPE_VARIABLE, 1 },122123{ "abs", (mpexpr_fun_t) mpq_abs, MPEXPR_TYPE_UNARY },124{ "cmp", (mpexpr_fun_t) mpq_cmp, MPEXPR_TYPE_I_BINARY },125{ "den", (mpexpr_fun_t) e_mpq_den, MPEXPR_TYPE_UNARY },126{ "max", (mpexpr_fun_t) mpq_cmp, MPEXPR_TYPE_MAX | MPEXPR_TYPE_PAIRWISE },127{ "min", (mpexpr_fun_t) mpq_cmp, MPEXPR_TYPE_MIN | MPEXPR_TYPE_PAIRWISE },128{ "num", (mpexpr_fun_t) e_mpq_num, MPEXPR_TYPE_UNARY },129{ "sgn", (mpexpr_fun_t) e_mpq_sgn, MPEXPR_TYPE_I_UNARY },130131{ NULL }132};133134const struct mpexpr_operator_t * const mpq_expr_standard_table135= _mpq_expr_standard_table;136137138int139mpq_expr (mpq_ptr res, int base, const char *e, ...)140{141mpq_srcptr var[MPEXPR_VARIABLES];142va_list ap;143int ret;144va_start (ap, e);145146TRACE (printf ("mpq_expr(): base %d, %s\n", base, e));147ret = mpexpr_va_to_var ((void **) var, ap);148va_end (ap);149150if (ret != MPEXPR_RESULT_OK)151return ret;152153return mpq_expr_a (mpq_expr_standard_table, res, base, e, strlen(e), var);154}155156157