github.com/aergoio/aergo@v1.3.1/libtool/src/gmp-6.1.2/demos/expr/exprf.c (about) 1 /* mpf expression evaluation 2 3 Copyright 2000-2002 Free Software Foundation, Inc. 4 5 This file is part of the GNU MP Library. 6 7 The GNU MP Library is free software; you can redistribute it and/or modify 8 it under the terms of either: 9 10 * the GNU Lesser General Public License as published by the Free 11 Software Foundation; either version 3 of the License, or (at your 12 option) any later version. 13 14 or 15 16 * the GNU General Public License as published by the Free Software 17 Foundation; either version 2 of the License, or (at your option) any 18 later version. 19 20 or both in parallel, as here. 21 22 The GNU MP Library is distributed in the hope that it will be useful, but 23 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 24 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 25 for more details. 26 27 You should have received copies of the GNU General Public License and the 28 GNU Lesser General Public License along with the GNU MP Library. If not, 29 see https://www.gnu.org/licenses/. */ 30 31 #include <stdio.h> 32 #include <string.h> 33 #include "gmp.h" 34 #include "expr-impl.h" 35 36 37 /* Change this to "#define TRACE(x) x" to get some traces. */ 38 #define TRACE(x) 39 40 41 static int 42 e_mpf_sgn (mpf_srcptr x) 43 { 44 return mpf_sgn (x); 45 } 46 47 48 static const struct mpexpr_operator_t _mpf_expr_standard_table[] = { 49 50 { "**", (mpexpr_fun_t) mpf_pow_ui, 51 MPEXPR_TYPE_BINARY_UI | MPEXPR_TYPE_RIGHTASSOC, 220 }, 52 53 { "!", (mpexpr_fun_t) e_mpf_sgn, 54 MPEXPR_TYPE_LOGICAL_NOT | MPEXPR_TYPE_PREFIX, 210 }, 55 { "-", (mpexpr_fun_t) mpf_neg, 56 MPEXPR_TYPE_UNARY | MPEXPR_TYPE_PREFIX, 210 }, 57 58 { "*", (mpexpr_fun_t) mpf_mul, MPEXPR_TYPE_BINARY, 200 }, 59 { "/", (mpexpr_fun_t) mpf_div, MPEXPR_TYPE_BINARY, 200 }, 60 61 { "+", (mpexpr_fun_t) mpf_add, MPEXPR_TYPE_BINARY, 190 }, 62 { "-", (mpexpr_fun_t) mpf_sub, MPEXPR_TYPE_BINARY, 190 }, 63 64 { "<<", (mpexpr_fun_t) mpf_mul_2exp, MPEXPR_TYPE_BINARY_UI, 180 }, 65 { ">>", (mpexpr_fun_t) mpf_div_2exp, MPEXPR_TYPE_BINARY_UI, 180 }, 66 67 { "<=", (mpexpr_fun_t) mpf_cmp, MPEXPR_TYPE_CMP_LE, 170 }, 68 { "<", (mpexpr_fun_t) mpf_cmp, MPEXPR_TYPE_CMP_LT, 170 }, 69 { ">=", (mpexpr_fun_t) mpf_cmp, MPEXPR_TYPE_CMP_GE, 170 }, 70 { ">", (mpexpr_fun_t) mpf_cmp, MPEXPR_TYPE_CMP_GT, 170 }, 71 72 { "==", (mpexpr_fun_t) mpf_cmp, MPEXPR_TYPE_CMP_EQ, 160 }, 73 { "!=", (mpexpr_fun_t) mpf_cmp, MPEXPR_TYPE_CMP_NE, 160 }, 74 75 { "&&", (mpexpr_fun_t) e_mpf_sgn, MPEXPR_TYPE_LOGICAL_AND, 120 }, 76 { "||", (mpexpr_fun_t) e_mpf_sgn, MPEXPR_TYPE_LOGICAL_OR, 110 }, 77 78 { ":", NULL, MPEXPR_TYPE_COLON, 101 }, 79 { "?", (mpexpr_fun_t) e_mpf_sgn, MPEXPR_TYPE_QUESTION, 100 }, 80 81 { ")", NULL, MPEXPR_TYPE_CLOSEPAREN, 4 }, 82 { "(", NULL, MPEXPR_TYPE_OPENPAREN, 3 }, 83 { ",", NULL, MPEXPR_TYPE_ARGSEP, 2 }, 84 { "$", NULL, MPEXPR_TYPE_VARIABLE, 1 }, 85 86 { "abs", (mpexpr_fun_t) mpf_abs, MPEXPR_TYPE_UNARY }, 87 { "ceil", (mpexpr_fun_t) mpf_ceil, MPEXPR_TYPE_UNARY }, 88 { "cmp", (mpexpr_fun_t) mpf_cmp, MPEXPR_TYPE_I_BINARY }, 89 { "eq", (mpexpr_fun_t) mpf_eq, MPEXPR_TYPE_I_TERNARY_UI }, 90 { "floor", (mpexpr_fun_t) mpf_floor, MPEXPR_TYPE_UNARY }, 91 { "integer_p",(mpexpr_fun_t) mpf_integer_p, MPEXPR_TYPE_I_UNARY }, 92 { "max", (mpexpr_fun_t) mpf_cmp, MPEXPR_TYPE_MAX | MPEXPR_TYPE_PAIRWISE }, 93 { "min", (mpexpr_fun_t) mpf_cmp, MPEXPR_TYPE_MIN | MPEXPR_TYPE_PAIRWISE }, 94 { "reldiff", (mpexpr_fun_t) mpf_reldiff, MPEXPR_TYPE_BINARY }, 95 { "sgn", (mpexpr_fun_t) e_mpf_sgn, MPEXPR_TYPE_I_UNARY }, 96 { "sqrt", (mpexpr_fun_t) mpf_sqrt, MPEXPR_TYPE_UNARY }, 97 { "trunc", (mpexpr_fun_t) mpf_trunc, MPEXPR_TYPE_UNARY }, 98 99 { NULL } 100 }; 101 102 const struct mpexpr_operator_t * const mpf_expr_standard_table 103 = _mpf_expr_standard_table; 104 105 106 int 107 mpf_expr (mpf_ptr res, int base, const char *e, ...) 108 { 109 mpf_srcptr var[MPEXPR_VARIABLES]; 110 va_list ap; 111 int ret; 112 va_start (ap, e); 113 114 TRACE (printf ("mpf_expr(): base %d, %s\n", base, e)); 115 ret = mpexpr_va_to_var ((void **) var, ap); 116 va_end (ap); 117 118 if (ret != MPEXPR_RESULT_OK) 119 return ret; 120 121 return mpf_expr_a (mpf_expr_standard_table, res, base, 122 mpf_get_prec (res), e, strlen(e), var); 123 }