| 1 |
/* mpq 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 void |
| 42 |
e_mpq_pow_ui (mpq_ptr r, mpq_srcptr b, unsigned long e) |
| 43 |
{ |
| 44 |
mpz_pow_ui (mpq_numref(r), mpq_numref(b), e); |
| 45 |
mpz_pow_ui (mpq_denref(r), mpq_denref(b), e); |
| 46 |
} |
| 47 |
|
| 48 |
/* Wrapped because mpq_sgn is a macro. */ |
| 49 |
static int |
| 50 |
e_mpq_sgn (mpq_srcptr x) |
| 51 |
{ |
| 52 |
return mpq_sgn (x); |
| 53 |
} |
| 54 |
|
| 55 |
/* Wrapped because mpq_equal only guarantees a non-zero return, whereas we |
| 56 |
want 1 or 0 for == and !=. */ |
| 57 |
static int |
| 58 |
e_mpq_equal (mpq_srcptr x, mpq_srcptr y) |
| 59 |
{ |
| 60 |
return mpq_equal (x, y) != 0; |
| 61 |
} |
| 62 |
static int |
| 63 |
e_mpq_notequal (mpq_srcptr x, mpq_srcptr y) |
| 64 |
{ |
| 65 |
return ! mpq_equal (x, y); |
| 66 |
} |
| 67 |
|
| 68 |
static void |
| 69 |
e_mpq_num (mpq_ptr w, mpq_srcptr x) |
| 70 |
{ |
| 71 |
if (w != x) |
| 72 |
mpz_set (mpq_numref(w), mpq_numref(x)); |
| 73 |
mpz_set_ui (mpq_denref(w), 1L); |
| 74 |
} |
| 75 |
static void |
| 76 |
e_mpq_den (mpq_ptr w, mpq_srcptr x) |
| 77 |
{ |
| 78 |
if (w == x) |
| 79 |
mpz_swap (mpq_numref(w), mpq_denref(w)); |
| 80 |
else |
| 81 |
mpz_set (mpq_numref(w), mpq_denref(x)); |
| 82 |
mpz_set_ui (mpq_denref(w), 1L); |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
static const struct mpexpr_operator_t _mpq_expr_standard_table[] = { |
| 87 |
|
| 88 |
{ "**", (mpexpr_fun_t) e_mpq_pow_ui, |
| 89 |
MPEXPR_TYPE_BINARY_UI | MPEXPR_TYPE_RIGHTASSOC, 220 }, |
| 90 |
|
| 91 |
{ "!", (mpexpr_fun_t) e_mpq_sgn, |
| 92 |
MPEXPR_TYPE_LOGICAL_NOT | MPEXPR_TYPE_PREFIX, 210 }, |
| 93 |
{ "-", (mpexpr_fun_t) mpq_neg, |
| 94 |
MPEXPR_TYPE_UNARY | MPEXPR_TYPE_PREFIX, 210 }, |
| 95 |
|
| 96 |
{ "*", (mpexpr_fun_t) mpq_mul, MPEXPR_TYPE_BINARY, 200 }, |
| 97 |
{ "/", (mpexpr_fun_t) mpq_div, MPEXPR_TYPE_BINARY, 200 }, |
| 98 |
|
| 99 |
{ "+", (mpexpr_fun_t) mpq_add, MPEXPR_TYPE_BINARY, 190 }, |
| 100 |
{ "-", (mpexpr_fun_t) mpq_sub, MPEXPR_TYPE_BINARY, 190 }, |
| 101 |
|
| 102 |
{ "<<", (mpexpr_fun_t) mpq_mul_2exp, MPEXPR_TYPE_BINARY_UI, 180 }, |
| 103 |
{ ">>", (mpexpr_fun_t) mpq_div_2exp, MPEXPR_TYPE_BINARY_UI, 180 }, |
| 104 |
|
| 105 |
{ "<=", (mpexpr_fun_t) mpq_cmp, MPEXPR_TYPE_CMP_LE, 170 }, |
| 106 |
{ "<", (mpexpr_fun_t) mpq_cmp, MPEXPR_TYPE_CMP_LT, 170 }, |
| 107 |
{ ">=", (mpexpr_fun_t) mpq_cmp, MPEXPR_TYPE_CMP_GE, 170 }, |
| 108 |
{ ">", (mpexpr_fun_t) mpq_cmp, MPEXPR_TYPE_CMP_GT, 170 }, |
| 109 |
|
| 110 |
{ "==", (mpexpr_fun_t) e_mpq_equal, MPEXPR_TYPE_I_BINARY, 160 }, |
| 111 |
{ "!=", (mpexpr_fun_t) e_mpq_notequal, MPEXPR_TYPE_I_BINARY, 160 }, |
| 112 |
|
| 113 |
{ "&&", (mpexpr_fun_t) e_mpq_sgn, MPEXPR_TYPE_LOGICAL_AND, 120 }, |
| 114 |
{ "||", (mpexpr_fun_t) e_mpq_sgn, MPEXPR_TYPE_LOGICAL_OR, 110 }, |
| 115 |
|
| 116 |
{ ":", NULL, MPEXPR_TYPE_COLON, 101 }, |
| 117 |
{ "?", (mpexpr_fun_t) e_mpq_sgn, MPEXPR_TYPE_QUESTION, 100 }, |
| 118 |
|
| 119 |
{ ")", (mpexpr_fun_t) e_mpq_sgn, MPEXPR_TYPE_CLOSEPAREN, 4 }, |
| 120 |
{ "(", (mpexpr_fun_t) e_mpq_sgn, MPEXPR_TYPE_OPENPAREN, 3 }, |
| 121 |
{ ",", (mpexpr_fun_t) e_mpq_sgn, MPEXPR_TYPE_ARGSEP, 2 }, |
| 122 |
{ "$", NULL, MPEXPR_TYPE_VARIABLE, 1 }, |
| 123 |
|
| 124 |
{ "abs", (mpexpr_fun_t) mpq_abs, MPEXPR_TYPE_UNARY }, |
| 125 |
{ "cmp", (mpexpr_fun_t) mpq_cmp, MPEXPR_TYPE_I_BINARY }, |
| 126 |
{ "den", (mpexpr_fun_t) e_mpq_den, MPEXPR_TYPE_UNARY }, |
| 127 |
{ "max", (mpexpr_fun_t) mpq_cmp, MPEXPR_TYPE_MAX | MPEXPR_TYPE_PAIRWISE }, |
| 128 |
{ "min", (mpexpr_fun_t) mpq_cmp, MPEXPR_TYPE_MIN | MPEXPR_TYPE_PAIRWISE }, |
| 129 |
{ "num", (mpexpr_fun_t) e_mpq_num, MPEXPR_TYPE_UNARY }, |
| 130 |
{ "sgn", (mpexpr_fun_t) e_mpq_sgn, MPEXPR_TYPE_I_UNARY }, |
| 131 |
|
| 132 |
{ NULL } |
| 133 |
}; |
| 134 |
|
| 135 |
const struct mpexpr_operator_t * const mpq_expr_standard_table |
| 136 |
= _mpq_expr_standard_table; |
| 137 |
|
| 138 |
|
| 139 |
int |
| 140 |
mpq_expr (mpq_ptr res, int base, const char *e, ...) |
| 141 |
{ |
| 142 |
mpq_srcptr var[MPEXPR_VARIABLES]; |
| 143 |
va_list ap; |
| 144 |
int ret; |
| 145 |
va_start (ap, e); |
| 146 |
|
| 147 |
TRACE (printf ("mpq_expr(): base %d, %s\n", base, e)); |
| 148 |
ret = mpexpr_va_to_var ((void **) var, ap); |
| 149 |
va_end (ap); |
| 150 |
|
| 151 |
if (ret != MPEXPR_RESULT_OK) |
| 152 |
return ret; |
| 153 |
|
| 154 |
return mpq_expr_a (mpq_expr_standard_table, res, base, e, strlen(e), var); |
| 155 |
} |