1 /* mpf expression evaluation
3 Copyright 2000, 2001, 2002, 2004 Free Software Foundation, Inc.
5 This file is part of the GNU MP Library.
7 The GNU MP Library is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
12 The GNU MP Library is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */
21 /* Future: Bitwise "&", "|" and "&" could be done, if desired. Not sure
22 those functions would be much value though. */
30 #include "expr-impl.h"
33 /* Change this to "#define TRACE(x) x" to get some traces. */
38 e_mpf_number (mpf_ptr res, __gmp_const char *e, size_t elen, int base)
41 size_t i, ret, extra=0;
42 int mant_base, exp_base;
43 void *(*allocate_func) (size_t);
44 void (*free_func) (void *, size_t);
46 TRACE (printf ("mpf_number base=%d \"%.*s\"\n", base, (int) elen, e));
48 /* mpf_set_str doesn't currently accept 0x for hex in base==0, so do it
49 here instead. FIXME: Would prefer to let mpf_set_str handle this. */
50 if (base == 0 && elen >= 2 && e[0] == '0' && (e[1] == 'x' || e[1] == 'X'))
65 /* exponent in decimal if base is negative */
73 #define IS_EXPONENT(c) \
74 (c == '@' || (base <= 10 && base >= -10 && (e[i] == 'e' || e[i] == 'E')))
83 if (IS_EXPONENT (e[i]))
85 if (! isasciidigit_in_base (e[i], mant_base))
96 if (IS_EXPONENT (e[i]))
98 if (! isasciidigit_in_base (e[i], mant_base))
113 if (! isasciidigit_in_base (e[i], exp_base))
119 TRACE (printf (" parsed i=%u \"%.*s\"\n", i, (int) i, e));
121 mp_get_memory_functions (&allocate_func, NULL, &free_func);
122 edup = (*allocate_func) (i+1);
126 if (mpf_set_str (res, edup, base) == 0)
131 (*free_func) (edup, i+1);
136 e_mpf_ulong_p (mpf_srcptr f)
138 return mpf_integer_p (f) && mpf_fits_ulong_p (f);
141 /* Don't want to change the precision of w, can only do an actual swap when
142 w and x have the same precision. */
144 e_mpf_set_or_swap (mpf_ptr w, mpf_ptr x)
146 if (mpf_get_prec (w) == mpf_get_prec (x))
154 mpf_expr_a (__gmp_const struct mpexpr_operator_t *table,
155 mpf_ptr res, int base, unsigned long prec,
156 __gmp_const char *e, size_t elen,
159 struct mpexpr_parse_t p;
162 p.res = (mpX_ptr) res;
167 p.var = (mpX_srcptr *) var;
169 p.mpX_clear = (mpexpr_fun_one_t) mpf_clear;
170 p.mpX_ulong_p = (mpexpr_fun_i_unary_t) e_mpf_ulong_p;
171 p.mpX_get_ui = (mpexpr_fun_get_ui_t) mpf_get_ui;
172 p.mpX_init = (mpexpr_fun_unary_ui_t) mpf_init2;
173 p.mpX_number = (mpexpr_fun_number_t) e_mpf_number;
174 p.mpX_set = (mpexpr_fun_unary_t) mpf_set;
175 p.mpX_set_or_swap = (mpexpr_fun_unary_t) e_mpf_set_or_swap;
176 p.mpX_set_si = (mpexpr_fun_set_si_t) mpf_set_si;
177 p.mpX_swap = (mpexpr_fun_swap_t) mpf_swap;
179 return mpexpr_evaluate (&p);