[BACK]Return to exprfr.c CVS log [TXT][DIR] Up to [local] / OpenXM_contrib / gmp / demos / expr

Annotation of OpenXM_contrib/gmp/demos/expr/exprfr.c, Revision 1.1.1.1

1.1       ohara       1: /* mpfr expression evaluation.
                      2:
                      3: Copyright 2000, 2001, 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 the GNU Lesser General Public License as published by
                      9: the Free Software Foundation; either version 2.1 of the License, or (at your
                     10: option) any later version.
                     11:
                     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.
                     16:
                     17: You should have received a copy of the GNU Lesser General Public License
                     18: along with the GNU MP Library; see the file COPYING.LIB.  If not, write to
                     19: the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
                     20: MA 02111-1307, USA. */
                     21:
                     22: #include <stdio.h>
                     23: #include "gmp.h"
                     24: #include "expr-impl.h"
                     25:
                     26:
                     27: /* Change this to "#define TRACE(x) x" to get some traces. */
                     28: #define TRACE(x)
                     29:
                     30:
                     31: /* This set of functions makes it possible to add the ROUND parameter, and a
                     32:    few of the mpfr's are macros and need a wrapper anyway.
                     33:
                     34:    A whole new set of functions like this just to add a rounding parameter
                     35:    is a bit horrible, but it's the easiest way.  There doesn't seem much use
                     36:    having rounding passed down through mpfr_expr_a, since a single rounding
                     37:    for all operations in an expression doesn't give close enough control to
                     38:    be useful.  */
                     39:
                     40: static void
                     41: e_mpfr_abs (mpfr_ptr dst, mpfr_srcptr src)
                     42: {
                     43:   mpfr_abs (dst, src, ROUND);
                     44: }
                     45:
                     46: static void
                     47: e_mpfr_acos (mpfr_ptr dst, mpfr_srcptr src)
                     48: {
                     49:   mpfr_acos (dst, src, ROUND);
                     50: }
                     51:
                     52: static void
                     53: e_mpfr_add (mpfr_ptr dst, mpfr_srcptr src1, mpfr_srcptr src2)
                     54: {
                     55:   mpfr_add (dst, src1, src2, ROUND);
                     56: }
                     57:
                     58: static void
                     59: e_mpfr_agm (mpfr_ptr dst, mpfr_srcptr src1, mpfr_srcptr src2)
                     60: {
                     61:   mpfr_agm (dst, src1, src2, ROUND);
                     62: }
                     63:
                     64: static void
                     65: e_mpfr_asin (mpfr_ptr dst, mpfr_srcptr src)
                     66: {
                     67:   mpfr_asin (dst, src, ROUND);
                     68: }
                     69:
                     70: static void
                     71: e_mpfr_atan (mpfr_ptr dst, mpfr_srcptr src)
                     72: {
                     73:   mpfr_atan (dst, src, ROUND);
                     74: }
                     75:
                     76: /* because mpfr_cmp is a macro */
                     77: static int
                     78: e_mpfr_cmp (mpfr_srcptr x, mpfr_srcptr y)
                     79: {
                     80:   return mpfr_cmp (x, y);
                     81: }
                     82:
                     83: static void
                     84: e_mpfr_dim (mpfr_ptr dst, mpfr_srcptr src1, mpfr_srcptr src2)
                     85: {
                     86:   mpfr_dim (dst, src1, src2, ROUND);
                     87: }
                     88:
                     89: static void
                     90: e_mpfr_const_euler (mpfr_ptr dst)
                     91: {
                     92:   mpfr_const_euler (dst, ROUND);
                     93: }
                     94:
                     95: static void
                     96: e_mpfr_fac_ui (mpfr_ptr dst, unsigned long n)
                     97: {
                     98:   mpfr_fac_ui (dst, n, ROUND);
                     99: }
                    100:
                    101: static void
                    102: e_mpfr_const_log2 (mpfr_ptr dst)
                    103: {
                    104:   mpfr_const_log2 (dst, ROUND);
                    105: }
                    106:
                    107: static void
                    108: e_mpfr_const_pi (mpfr_ptr dst)
                    109: {
                    110:   mpfr_const_pi (dst, ROUND);
                    111: }
                    112:
                    113: static void
                    114: e_mpfr_cos (mpfr_ptr dst, mpfr_srcptr src)
                    115: {
                    116:   mpfr_cos (dst, src, ROUND);
                    117: }
                    118:
                    119: static void
                    120: e_mpfr_cosh (mpfr_ptr dst, mpfr_srcptr src)
                    121: {
                    122:   mpfr_cosh (dst, src, ROUND);
                    123: }
                    124:
                    125: static void
                    126: e_mpfr_div (mpfr_ptr dst, mpfr_srcptr src1, mpfr_srcptr src2)
                    127: {
                    128:   mpfr_div (dst, src1, src2, ROUND);
                    129: }
                    130:
                    131: static void
                    132: e_mpfr_div_2exp (mpfr_ptr dst, mpfr_srcptr src, unsigned long n)
                    133: {
                    134:   mpfr_div_2exp (dst, src, n, ROUND);
                    135: }
                    136:
                    137: static void
                    138: e_mpfr_exp (mpfr_ptr dst, mpfr_srcptr src)
                    139: {
                    140:   mpfr_exp (dst, src, ROUND);
                    141: }
                    142:
                    143: static void
                    144: e_mpfr_expm1 (mpfr_ptr dst, mpfr_srcptr src)
                    145: {
                    146:   mpfr_expm1 (dst, src, ROUND);
                    147: }
                    148:
                    149: static void
                    150: e_mpfr_fma (mpfr_ptr w, mpfr_srcptr x, mpfr_srcptr y, mpfr_srcptr z)
                    151: {
                    152:   mpfr_fma (w, x, y, z, ROUND);
                    153: }
                    154:
                    155: static void
                    156: e_mpfr_hypot (mpfr_ptr dst, mpfr_srcptr src1, mpfr_srcptr src2)
                    157: {
                    158:   mpfr_hypot (dst, src1, src2, ROUND);
                    159: }
                    160:
                    161: static void
                    162: e_mpfr_log (mpfr_ptr dst, mpfr_srcptr src)
                    163: {
                    164:   mpfr_log (dst, src, ROUND);
                    165: }
                    166:
                    167: static void
                    168: e_mpfr_log2 (mpfr_ptr dst, mpfr_srcptr src)
                    169: {
                    170:   mpfr_log2 (dst, src, ROUND);
                    171: }
                    172:
                    173: static void
                    174: e_mpfr_log10 (mpfr_ptr dst, mpfr_srcptr src)
                    175: {
                    176:   mpfr_log10 (dst, src, ROUND);
                    177: }
                    178:
                    179: static void
                    180: e_mpfr_log1p (mpfr_ptr dst, mpfr_srcptr src)
                    181: {
                    182:   mpfr_log1p (dst, src, ROUND);
                    183: }
                    184:
                    185: static void
                    186: e_mpfr_mul (mpfr_ptr dst, mpfr_srcptr src1, mpfr_srcptr src2)
                    187: {
                    188:   mpfr_mul (dst, src1, src2, ROUND);
                    189: }
                    190:
                    191: static void
                    192: e_mpfr_mul_2exp (mpfr_ptr dst, mpfr_srcptr src, unsigned long n)
                    193: {
                    194:   mpfr_mul_2exp (dst, src, n, ROUND);
                    195: }
                    196:
                    197: static void
                    198: e_mpfr_neg (mpfr_ptr dst, mpfr_srcptr src)
                    199: {
                    200:   mpfr_neg (dst, src, ROUND);
                    201: }
                    202:
                    203: static void
                    204: e_mpfr_pow_ui (mpfr_ptr p, mpfr_srcptr b, unsigned long e)
                    205: {
                    206:   mpfr_pow_ui (p, b, e, ROUND);
                    207: }
                    208:
                    209: static void
                    210: e_mpfr_reldiff (mpfr_ptr dst, mpfr_srcptr src1, mpfr_srcptr src2)
                    211: {
                    212:   mpfr_reldiff (dst, src1, src2, ROUND);
                    213: }
                    214:
                    215: static void
                    216: e_mpfr_set_posinf (mpfr_ptr dst)
                    217: {
                    218:   mpfr_set_inf (dst, 1);
                    219: }
                    220:
                    221: static void
                    222: e_mpfr_sin (mpfr_ptr dst, mpfr_srcptr src)
                    223: {
                    224:   mpfr_sin (dst, src, ROUND);
                    225: }
                    226:
                    227: static void
                    228: e_mpfr_sinh (mpfr_ptr dst, mpfr_srcptr src)
                    229: {
                    230:   mpfr_sinh (dst, src, ROUND);
                    231: }
                    232:
                    233: static int
                    234: e_mpfr_sgn (mpfr_srcptr x)
                    235: {
                    236:   return mpfr_sgn (x);
                    237: }
                    238:
                    239: static void
                    240: e_mpfr_sqrt (mpfr_ptr dst, mpfr_srcptr src)
                    241: {
                    242:   mpfr_sqrt (dst, src, ROUND);
                    243: }
                    244:
                    245: static void
                    246: e_mpfr_sub (mpfr_ptr dst, mpfr_srcptr src1, mpfr_srcptr src2)
                    247: {
                    248:   mpfr_sub (dst, src1, src2, ROUND);
                    249: }
                    250:
                    251: static void
                    252: e_mpfr_tan (mpfr_ptr dst, mpfr_srcptr src)
                    253: {
                    254:   mpfr_tan (dst, src, ROUND);
                    255: }
                    256:
                    257: static void
                    258: e_mpfr_tanh (mpfr_ptr dst, mpfr_srcptr src)
                    259: {
                    260:   mpfr_tanh (dst, src, ROUND);
                    261: }
                    262:
                    263:
                    264: static __gmp_const struct mpexpr_operator_t  _mpfr_expr_standard_table[] = {
                    265:
                    266:   { "**",  (mpexpr_fun_t) e_mpfr_pow_ui,
                    267:     MPEXPR_TYPE_BINARY_UI | MPEXPR_TYPE_RIGHTASSOC,                   220 },
                    268:
                    269:   { "!",   (mpexpr_fun_t) e_mpfr_sgn,
                    270:     MPEXPR_TYPE_LOGICAL_NOT | MPEXPR_TYPE_PREFIX,                     210 },
                    271:   { "-",   (mpexpr_fun_t) e_mpfr_neg,
                    272:     MPEXPR_TYPE_UNARY | MPEXPR_TYPE_PREFIX,                           210 },
                    273:
                    274:   { "*",   (mpexpr_fun_t) e_mpfr_mul,        MPEXPR_TYPE_BINARY,      200 },
                    275:   { "/",   (mpexpr_fun_t) e_mpfr_div,        MPEXPR_TYPE_BINARY,      200 },
                    276:
                    277:   { "+",   (mpexpr_fun_t) e_mpfr_add,        MPEXPR_TYPE_BINARY,      190 },
                    278:   { "-",   (mpexpr_fun_t) e_mpfr_sub,        MPEXPR_TYPE_BINARY,      190 },
                    279:
                    280:   { "<<",  (mpexpr_fun_t) e_mpfr_mul_2exp,   MPEXPR_TYPE_BINARY_UI,   180 },
                    281:   { ">>",  (mpexpr_fun_t) e_mpfr_div_2exp,   MPEXPR_TYPE_BINARY_UI,   180 },
                    282:
                    283:   { "<=",  (mpexpr_fun_t) e_mpfr_cmp,        MPEXPR_TYPE_CMP_LE,      170 },
                    284:   { "<",   (mpexpr_fun_t) e_mpfr_cmp,        MPEXPR_TYPE_CMP_LT,      170 },
                    285:   { ">=",  (mpexpr_fun_t) e_mpfr_cmp,        MPEXPR_TYPE_CMP_GE,      170 },
                    286:   { ">",   (mpexpr_fun_t) e_mpfr_cmp,        MPEXPR_TYPE_CMP_GT,      170 },
                    287:
                    288:   { "==",  (mpexpr_fun_t) e_mpfr_cmp,        MPEXPR_TYPE_CMP_EQ,      160 },
                    289:   { "!=",  (mpexpr_fun_t) e_mpfr_cmp,        MPEXPR_TYPE_CMP_NE,      160 },
                    290:
                    291:   { "&&",  (mpexpr_fun_t) e_mpfr_sgn,        MPEXPR_TYPE_LOGICAL_AND, 120 },
                    292:   { "||",  (mpexpr_fun_t) e_mpfr_sgn,        MPEXPR_TYPE_LOGICAL_OR,  110 },
                    293:
                    294:   { ":",   NULL,                             MPEXPR_TYPE_COLON,       101 },
                    295:   { "?",   (mpexpr_fun_t) e_mpfr_sgn,        MPEXPR_TYPE_QUESTION,    100 },
                    296:
                    297:   { ")",   NULL,                             MPEXPR_TYPE_CLOSEPAREN,    4 },
                    298:   { "(",   NULL,                             MPEXPR_TYPE_OPENPAREN,     3 },
                    299:   { ",",   NULL,                             MPEXPR_TYPE_ARGSEP,        2 },
                    300:   { "$",   NULL,                             MPEXPR_TYPE_VARIABLE,      1 },
                    301:
                    302:   { "abs",     (mpexpr_fun_t) e_mpfr_abs,     MPEXPR_TYPE_UNARY        },
                    303:   { "acos",    (mpexpr_fun_t) e_mpfr_acos,    MPEXPR_TYPE_UNARY        },
                    304:   { "agm",     (mpexpr_fun_t) e_mpfr_agm,     MPEXPR_TYPE_BINARY       },
                    305:   { "asin",    (mpexpr_fun_t) e_mpfr_asin,    MPEXPR_TYPE_UNARY        },
                    306:   { "atan",    (mpexpr_fun_t) e_mpfr_atan,    MPEXPR_TYPE_UNARY        },
                    307:   { "ceil",    (mpexpr_fun_t) mpfr_ceil,      MPEXPR_TYPE_UNARY        },
                    308:   { "cmp",     (mpexpr_fun_t) e_mpfr_cmp,     MPEXPR_TYPE_I_BINARY     },
                    309:   { "cos",     (mpexpr_fun_t) e_mpfr_cos,     MPEXPR_TYPE_UNARY        },
                    310:   { "cosh",    (mpexpr_fun_t) e_mpfr_cosh,    MPEXPR_TYPE_UNARY        },
                    311:   { "dim",     (mpexpr_fun_t) e_mpfr_dim,     MPEXPR_TYPE_BINARY       },
                    312:   { "eq",      (mpexpr_fun_t) mpfr_eq,        MPEXPR_TYPE_I_TERNARY_UI },
                    313:   { "exp",     (mpexpr_fun_t) e_mpfr_exp,     MPEXPR_TYPE_UNARY        },
                    314:   { "expm1",   (mpexpr_fun_t) e_mpfr_expm1,   MPEXPR_TYPE_UNARY        },
                    315:   { "fac",     (mpexpr_fun_t) e_mpfr_fac_ui,  MPEXPR_TYPE_UNARY_UI     },
                    316:   { "floor",   (mpexpr_fun_t) mpfr_floor,     MPEXPR_TYPE_UNARY        },
                    317:   { "fma",     (mpexpr_fun_t) e_mpfr_fma,     MPEXPR_TYPE_TERNARY      },
                    318:   { "hypot",   (mpexpr_fun_t) e_mpfr_hypot,   MPEXPR_TYPE_BINARY       },
                    319:   { "inf_p",   (mpexpr_fun_t) mpfr_inf_p,     MPEXPR_TYPE_I_UNARY      },
                    320:   { "log",     (mpexpr_fun_t) e_mpfr_log,     MPEXPR_TYPE_UNARY        },
                    321:   { "log2",    (mpexpr_fun_t) e_mpfr_log2,    MPEXPR_TYPE_UNARY        },
                    322:   { "log10",   (mpexpr_fun_t) e_mpfr_log10,   MPEXPR_TYPE_UNARY        },
                    323:   { "log1p",   (mpexpr_fun_t) e_mpfr_log1p,   MPEXPR_TYPE_UNARY        },
                    324:   { "max",     (mpexpr_fun_t) e_mpfr_cmp,     MPEXPR_TYPE_MAX
                    325:                                               | MPEXPR_TYPE_PAIRWISE   },
                    326:   { "min",     (mpexpr_fun_t) e_mpfr_cmp,     MPEXPR_TYPE_MIN
                    327:                                               | MPEXPR_TYPE_PAIRWISE   },
                    328:   { "nan_p",   (mpexpr_fun_t) mpfr_nan_p,     MPEXPR_TYPE_I_UNARY      },
                    329:   { "number_p",(mpexpr_fun_t) mpfr_number_p,  MPEXPR_TYPE_I_UNARY      },
                    330:   { "reldiff", (mpexpr_fun_t) e_mpfr_reldiff, MPEXPR_TYPE_BINARY       },
                    331:   { "round",   (mpexpr_fun_t) mpfr_round,     MPEXPR_TYPE_UNARY        },
                    332:   { "sgn",     (mpexpr_fun_t) e_mpfr_sgn,     MPEXPR_TYPE_I_UNARY      },
                    333:   { "sin",     (mpexpr_fun_t) e_mpfr_sin,     MPEXPR_TYPE_UNARY        },
                    334:   { "sinh",    (mpexpr_fun_t) e_mpfr_sinh,    MPEXPR_TYPE_UNARY        },
                    335:   { "sqrt",    (mpexpr_fun_t) e_mpfr_sqrt,    MPEXPR_TYPE_UNARY        },
                    336:   { "tan",     (mpexpr_fun_t) e_mpfr_tan,     MPEXPR_TYPE_UNARY        },
                    337:   { "tanh",    (mpexpr_fun_t) e_mpfr_tanh,    MPEXPR_TYPE_UNARY        },
                    338:   { "trunc",   (mpexpr_fun_t) mpfr_trunc,     MPEXPR_TYPE_UNARY        },
                    339:
                    340:   { "euler",   (mpexpr_fun_t) e_mpfr_const_euler, MPEXPR_TYPE_CONSTANT },
                    341:   { "loge2",   (mpexpr_fun_t) e_mpfr_const_log2,  MPEXPR_TYPE_CONSTANT },
                    342:   { "nan",     (mpexpr_fun_t) mpfr_set_nan,       MPEXPR_TYPE_CONSTANT },
                    343:   { "inf",     (mpexpr_fun_t) e_mpfr_set_posinf,  MPEXPR_TYPE_CONSTANT },
                    344:   { "pi",      (mpexpr_fun_t) e_mpfr_const_pi,    MPEXPR_TYPE_CONSTANT },
                    345:
                    346:   { NULL }
                    347: };
                    348:
                    349: __gmp_const struct mpexpr_operator_t * __gmp_const mpfr_expr_standard_table
                    350: = _mpfr_expr_standard_table;
                    351:
                    352:
                    353: int
                    354: #if HAVE_STDARG
                    355: mpfr_expr (mpfr_ptr res, int base, __gmp_const char *e, ...)
                    356: #else
                    357: mpfr_expr (va_alist)
                    358:      va_dcl
                    359: #endif
                    360: {
                    361:   mpfr_srcptr  var[MPEXPR_VARIABLES];
                    362:   va_list     ap;
                    363:   int         ret;
                    364: #if HAVE_STDARG
                    365:   va_start (ap, e);
                    366: #else
                    367:   mpfr_ptr          res;
                    368:   int               base;
                    369:   __gmp_const char  *e;
                    370:   va_start (ap);
                    371:   res  = va_arg (ap, mpfr_ptr);
                    372:   base = va_arg (ap, int);
                    373:   e    = va_arg (ap, __gmp_const char *);
                    374: #endif
                    375:
                    376:   TRACE (printf ("mpfr_expr(): base %d, %s\n", base, e));
                    377:   ret = mpexpr_va_to_var ((void **) var, ap);
                    378:   va_end (ap);
                    379:
                    380:   if (ret != MPEXPR_RESULT_OK)
                    381:     return ret;
                    382:
                    383:   return mpfr_expr_a (mpfr_expr_standard_table, res, base,
                    384:                       mpfr_get_prec (res), e, strlen(e), var);
                    385: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>