GAP 4.8.9 installation with standard packages -- copy to your CoCalc project to get it
/* mpf expression evaluation12Copyright 2000, 2001, 2002, 2004 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 the GNU Lesser General Public License as published by8the Free Software Foundation; either version 2.1 of the License, or (at your9option) any later version.1011The GNU MP Library is distributed in the hope that it will be useful, but12WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY13or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public14License for more details.1516You should have received a copy of the GNU Lesser General Public License17along with the GNU MP Library; see the file COPYING.LIB. If not, write to18the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,19MA 02110-1301, USA. */202122/* Future: Bitwise "&", "|" and "&" could be done, if desired. Not sure23those functions would be much value though. */242526#include <ctype.h>27#include <stdio.h>28#include <string.h>2930#include "gmp.h"31#include "expr-impl.h"323334/* Change this to "#define TRACE(x) x" to get some traces. */35#define TRACE(x)363738static size_t39e_mpf_number (mpf_ptr res, __gmp_const char *e, size_t elen, int base)40{41char *edup;42size_t i, ret, extra=0;43int mant_base, exp_base;44void *(*allocate_func) (size_t);45void (*free_func) (void *, size_t);4647TRACE (printf ("mpf_number base=%d \"%.*s\"\n", base, (int) elen, e));4849/* mpf_set_str doesn't currently accept 0x for hex in base==0, so do it50here instead. FIXME: Would prefer to let mpf_set_str handle this. */51if (base == 0 && elen >= 2 && e[0] == '0' && (e[1] == 'x' || e[1] == 'X'))52{53base = 16;54extra = 2;55e += extra;56elen -= extra;57}5859if (base == 0)60mant_base = 10;61else if (base < 0)62mant_base = -base;63else64mant_base = base;6566/* exponent in decimal if base is negative */67if (base < 0)68exp_base = 10;69else if (base == 0)70exp_base = 10;71else72exp_base = base;7374#define IS_EXPONENT(c) \75(c == '@' || (base <= 10 && base >= -10 && (e[i] == 'e' || e[i] == 'E')))7677i = 0;78for (;;)79{80if (i >= elen)81goto parsed;82if (e[i] == '.')83break;84if (IS_EXPONENT (e[i]))85goto exponent;86if (! isasciidigit_in_base (e[i], mant_base))87goto parsed;88i++;89}9091/* fraction */92i++;93for (;;)94{95if (i >= elen)96goto parsed;97if (IS_EXPONENT (e[i]))98goto exponent;99if (! isasciidigit_in_base (e[i], mant_base))100goto parsed;101i++;102}103104exponent:105i++;106if (i >= elen)107goto parsed;108if (e[i] == '-')109i++;110for (;;)111{112if (i >= elen)113goto parsed;114if (! isasciidigit_in_base (e[i], exp_base))115break;116i++;117}118119parsed:120TRACE (printf (" parsed i=%u \"%.*s\"\n", i, (int) i, e));121122mp_get_memory_functions (&allocate_func, NULL, &free_func);123edup = (*allocate_func) (i+1);124memcpy (edup, e, i);125edup[i] = '\0';126127if (mpf_set_str (res, edup, base) == 0)128ret = i + extra;129else130ret = 0;131132(*free_func) (edup, i+1);133return ret;134}135136static int137e_mpf_ulong_p (mpf_srcptr f)138{139return mpf_integer_p (f) && mpf_fits_ulong_p (f);140}141142/* Don't want to change the precision of w, can only do an actual swap when143w and x have the same precision. */144static void145e_mpf_set_or_swap (mpf_ptr w, mpf_ptr x)146{147if (mpf_get_prec (w) == mpf_get_prec (x))148mpf_swap (w, x);149else150mpf_set (w, x);151}152153154int155mpf_expr_a (__gmp_const struct mpexpr_operator_t *table,156mpf_ptr res, int base, unsigned long prec,157__gmp_const char *e, size_t elen,158mpf_srcptr var[26])159{160struct mpexpr_parse_t p;161162p.table = table;163p.res = (mpX_ptr) res;164p.base = base;165p.prec = prec;166p.e = e;167p.elen = elen;168p.var = (mpX_srcptr *) var;169170p.mpX_clear = (mpexpr_fun_one_t) mpf_clear;171p.mpX_ulong_p = (mpexpr_fun_i_unary_t) e_mpf_ulong_p;172p.mpX_get_ui = (mpexpr_fun_get_ui_t) mpf_get_ui;173p.mpX_init = (mpexpr_fun_unary_ui_t) mpf_init2;174p.mpX_number = (mpexpr_fun_number_t) e_mpf_number;175p.mpX_set = (mpexpr_fun_unary_t) mpf_set;176p.mpX_set_or_swap = (mpexpr_fun_unary_t) e_mpf_set_or_swap;177p.mpX_set_si = (mpexpr_fun_set_si_t) mpf_set_si;178p.mpX_swap = (mpexpr_fun_swap_t) mpf_swap;179180return mpexpr_evaluate (&p);181}182183184