Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
| Download
GAP 4.8.9 installation with standard packages -- copy to your CoCalc project to get it
Project: cocalc-sagemath-dev-slelievre
Views: 418386/* mpf expression evaluation12Copyright 2000-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 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/. */293031/* Future: Bitwise "&", "|" and "&" could be done, if desired. Not sure32those functions would be much value though. */333435#include <ctype.h>36#include <stdio.h>37#include <string.h>3839#include "gmp.h"40#include "expr-impl.h"414243/* Change this to "#define TRACE(x) x" to get some traces. */44#define TRACE(x)454647static size_t48e_mpf_number (mpf_ptr res, const char *e, size_t elen, int base)49{50char *edup;51size_t i, ret, extra=0;52int mant_base, exp_base;53void *(*allocate_func) (size_t);54void (*free_func) (void *, size_t);5556TRACE (printf ("mpf_number base=%d \"%.*s\"\n", base, (int) elen, e));5758/* mpf_set_str doesn't currently accept 0x for hex in base==0, so do it59here instead. FIXME: Would prefer to let mpf_set_str handle this. */60if (base == 0 && elen >= 2 && e[0] == '0' && (e[1] == 'x' || e[1] == 'X'))61{62base = 16;63extra = 2;64e += extra;65elen -= extra;66}6768if (base == 0)69mant_base = 10;70else if (base < 0)71mant_base = -base;72else73mant_base = base;7475/* exponent in decimal if base is negative */76if (base < 0)77exp_base = 10;78else if (base == 0)79exp_base = 10;80else81exp_base = base;8283#define IS_EXPONENT(c) \84(c == '@' || (base <= 10 && base >= -10 && (e[i] == 'e' || e[i] == 'E')))8586i = 0;87for (;;)88{89if (i >= elen)90goto parsed;91if (e[i] == '.')92break;93if (IS_EXPONENT (e[i]))94goto exponent;95if (! isasciidigit_in_base (e[i], mant_base))96goto parsed;97i++;98}99100/* fraction */101i++;102for (;;)103{104if (i >= elen)105goto parsed;106if (IS_EXPONENT (e[i]))107goto exponent;108if (! isasciidigit_in_base (e[i], mant_base))109goto parsed;110i++;111}112113exponent:114i++;115if (i >= elen)116goto parsed;117if (e[i] == '-')118i++;119for (;;)120{121if (i >= elen)122goto parsed;123if (! isasciidigit_in_base (e[i], exp_base))124break;125i++;126}127128parsed:129TRACE (printf (" parsed i=%u \"%.*s\"\n", i, (int) i, e));130131mp_get_memory_functions (&allocate_func, NULL, &free_func);132edup = (*allocate_func) (i+1);133memcpy (edup, e, i);134edup[i] = '\0';135136if (mpf_set_str (res, edup, base) == 0)137ret = i + extra;138else139ret = 0;140141(*free_func) (edup, i+1);142return ret;143}144145static int146e_mpf_ulong_p (mpf_srcptr f)147{148return mpf_integer_p (f) && mpf_fits_ulong_p (f);149}150151/* Don't want to change the precision of w, can only do an actual swap when152w and x have the same precision. */153static void154e_mpf_set_or_swap (mpf_ptr w, mpf_ptr x)155{156if (mpf_get_prec (w) == mpf_get_prec (x))157mpf_swap (w, x);158else159mpf_set (w, x);160}161162163int164mpf_expr_a (const struct mpexpr_operator_t *table,165mpf_ptr res, int base, unsigned long prec,166const char *e, size_t elen,167mpf_srcptr var[26])168{169struct mpexpr_parse_t p;170171p.table = table;172p.res = (mpX_ptr) res;173p.base = base;174p.prec = prec;175p.e = e;176p.elen = elen;177p.var = (mpX_srcptr *) var;178179p.mpX_clear = (mpexpr_fun_one_t) mpf_clear;180p.mpX_ulong_p = (mpexpr_fun_i_unary_t) e_mpf_ulong_p;181p.mpX_get_ui = (mpexpr_fun_get_ui_t) mpf_get_ui;182p.mpX_init = (mpexpr_fun_unary_ui_t) mpf_init2;183p.mpX_number = (mpexpr_fun_number_t) e_mpf_number;184p.mpX_set = (mpexpr_fun_unary_t) mpf_set;185p.mpX_set_or_swap = (mpexpr_fun_unary_t) e_mpf_set_or_swap;186p.mpX_set_si = (mpexpr_fun_set_si_t) mpf_set_si;187p.mpX_swap = (mpexpr_fun_swap_t) mpf_swap;188189return mpexpr_evaluate (&p);190}191192193