1 /* mpf expression evaluation
3 Copyright 2000, 2001, 2002 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/. */
23 #include "expr-impl.h"
26 /* Change this to "#define TRACE(x) x" to get some traces. */
31 e_mpf_sgn (mpf_srcptr x)
37 static __gmp_const struct mpexpr_operator_t _mpf_expr_standard_table[] = {
39 { "**", (mpexpr_fun_t) mpf_pow_ui,
40 MPEXPR_TYPE_BINARY_UI | MPEXPR_TYPE_RIGHTASSOC, 220 },
42 { "!", (mpexpr_fun_t) e_mpf_sgn,
43 MPEXPR_TYPE_LOGICAL_NOT | MPEXPR_TYPE_PREFIX, 210 },
44 { "-", (mpexpr_fun_t) mpf_neg,
45 MPEXPR_TYPE_UNARY | MPEXPR_TYPE_PREFIX, 210 },
47 { "*", (mpexpr_fun_t) mpf_mul, MPEXPR_TYPE_BINARY, 200 },
48 { "/", (mpexpr_fun_t) mpf_div, MPEXPR_TYPE_BINARY, 200 },
50 { "+", (mpexpr_fun_t) mpf_add, MPEXPR_TYPE_BINARY, 190 },
51 { "-", (mpexpr_fun_t) mpf_sub, MPEXPR_TYPE_BINARY, 190 },
53 { "<<", (mpexpr_fun_t) mpf_mul_2exp, MPEXPR_TYPE_BINARY_UI, 180 },
54 { ">>", (mpexpr_fun_t) mpf_div_2exp, MPEXPR_TYPE_BINARY_UI, 180 },
56 { "<=", (mpexpr_fun_t) mpf_cmp, MPEXPR_TYPE_CMP_LE, 170 },
57 { "<", (mpexpr_fun_t) mpf_cmp, MPEXPR_TYPE_CMP_LT, 170 },
58 { ">=", (mpexpr_fun_t) mpf_cmp, MPEXPR_TYPE_CMP_GE, 170 },
59 { ">", (mpexpr_fun_t) mpf_cmp, MPEXPR_TYPE_CMP_GT, 170 },
61 { "==", (mpexpr_fun_t) mpf_cmp, MPEXPR_TYPE_CMP_EQ, 160 },
62 { "!=", (mpexpr_fun_t) mpf_cmp, MPEXPR_TYPE_CMP_NE, 160 },
64 { "&&", (mpexpr_fun_t) e_mpf_sgn, MPEXPR_TYPE_LOGICAL_AND, 120 },
65 { "||", (mpexpr_fun_t) e_mpf_sgn, MPEXPR_TYPE_LOGICAL_OR, 110 },
67 { ":", NULL, MPEXPR_TYPE_COLON, 101 },
68 { "?", (mpexpr_fun_t) e_mpf_sgn, MPEXPR_TYPE_QUESTION, 100 },
70 { ")", NULL, MPEXPR_TYPE_CLOSEPAREN, 4 },
71 { "(", NULL, MPEXPR_TYPE_OPENPAREN, 3 },
72 { ",", NULL, MPEXPR_TYPE_ARGSEP, 2 },
73 { "$", NULL, MPEXPR_TYPE_VARIABLE, 1 },
75 { "abs", (mpexpr_fun_t) mpf_abs, MPEXPR_TYPE_UNARY },
76 { "ceil", (mpexpr_fun_t) mpf_ceil, MPEXPR_TYPE_UNARY },
77 { "cmp", (mpexpr_fun_t) mpf_cmp, MPEXPR_TYPE_I_BINARY },
78 { "eq", (mpexpr_fun_t) mpf_eq, MPEXPR_TYPE_I_TERNARY_UI },
79 { "floor", (mpexpr_fun_t) mpf_floor, MPEXPR_TYPE_UNARY },
80 { "integer_p",(mpexpr_fun_t) mpf_integer_p, MPEXPR_TYPE_I_UNARY },
81 { "max", (mpexpr_fun_t) mpf_cmp, MPEXPR_TYPE_MAX | MPEXPR_TYPE_PAIRWISE },
82 { "min", (mpexpr_fun_t) mpf_cmp, MPEXPR_TYPE_MIN | MPEXPR_TYPE_PAIRWISE },
83 { "reldiff", (mpexpr_fun_t) mpf_reldiff, MPEXPR_TYPE_BINARY },
84 { "sgn", (mpexpr_fun_t) e_mpf_sgn, MPEXPR_TYPE_I_UNARY },
85 { "sqrt", (mpexpr_fun_t) mpf_sqrt, MPEXPR_TYPE_UNARY },
86 { "trunc", (mpexpr_fun_t) mpf_trunc, MPEXPR_TYPE_UNARY },
91 __gmp_const struct mpexpr_operator_t * __gmp_const mpf_expr_standard_table
92 = _mpf_expr_standard_table;
97 mpf_expr (mpf_ptr res, int base, __gmp_const char *e, ...)
103 mpf_srcptr var[MPEXPR_VARIABLES];
113 res = va_arg (ap, mpf_ptr);
114 base = va_arg (ap, int);
115 e = va_arg (ap, __gmp_const char *);
118 TRACE (printf ("mpf_expr(): base %d, %s\n", base, e));
119 ret = mpexpr_va_to_var ((void **) var, ap);
122 if (ret != MPEXPR_RESULT_OK)
125 return mpf_expr_a (mpf_expr_standard_table, res, base,
126 mpf_get_prec (res), e, strlen(e), var);