1
/* mpf expression evaluation */
4
Copyright 2000, 2001 Free Software Foundation, Inc.
6
This file is part of the GNU MP Library.
8
The GNU MP Library is free software; you can redistribute it and/or modify
9
it under the terms of the GNU Lesser General Public License as published by
10
the Free Software Foundation; either version 2.1 of the License, or (at your
11
option) any later version.
13
The GNU MP Library is distributed in the hope that it will be useful, but
14
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16
License for more details.
18
You should have received a copy of the GNU Lesser General Public License
19
along with the GNU MP Library; see the file COPYING.LIB. If not, write to
20
the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
24
/* Future: Bitwise "&", "|" and "&" could be done, if desired. Not sure
25
those functions would be much value though. */
32
#include "expr-impl.h"
35
/* Change this to "#define TRACE(x) x" to get some traces. */
40
e_mpf_number (mpf_ptr res, __gmp_const char *e, size_t elen, int base)
43
size_t i, ret, extra=0;
44
int mant_base, exp_base;
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
edup = (*__gmp_allocate_func) (i+1);
125
if (mpf_set_str (res, edup, base) == 0)
130
(*__gmp_free_func) (edup, i+1);
135
e_mpf_ulong_p (mpf_srcptr f)
137
return mpf_integer_p (f) && mpf_fits_ulong_p (f);
140
/* Don't want to change the precision of w, can only do an actual swap when
141
w and x have the same precision. */
143
e_mpf_set_or_swap (mpf_ptr w, mpf_ptr x)
145
if (mpf_get_prec (w) == mpf_get_prec (x))
153
mpf_expr_a (__gmp_const struct mpexpr_operator_t *table,
154
mpf_ptr res, int base, unsigned long prec,
155
__gmp_const char *e, size_t elen,
158
struct mpexpr_parse_t p;
161
p.res = (mpX_ptr) res;
166
p.var = (mpX_srcptr *) var;
168
p.mpX_clear = (mpexpr_fun_one_t) mpf_clear;
169
p.mpX_ulong_p = (mpexpr_fun_i_unary_t) e_mpf_ulong_p;
170
p.mpX_get_ui = (mpexpr_fun_get_ui_t) mpf_get_ui;
171
p.mpX_init = (mpexpr_fun_unary_ui_t) mpf_init;
172
p.mpX_number = (mpexpr_fun_number_t) e_mpf_number;
173
p.mpX_set = (mpexpr_fun_unary_t) mpf_set;
174
p.mpX_set_or_swap = (mpexpr_fun_unary_t) e_mpf_set_or_swap;
175
p.mpX_set_si = (mpexpr_fun_set_si_t) mpf_set_si;
176
p.mpX_swap = (mpexpr_fun_swap_t) mpf_swap;
178
return mpexpr_evaluate (&p);