~ubuntu-branches/ubuntu/quantal/gclcvs/quantal

« back to all changes in this revision

Viewing changes to gmp3/demos/expr/exprf.c

  • Committer: Bazaar Package Importer
  • Author(s): Camm Maguire
  • Date: 2004-06-24 15:13:46 UTC
  • Revision ID: james.westby@ubuntu.com-20040624151346-xh0xaaktyyp7aorc
Tags: 2.7.0-26
C_GC_OFFSET is 2 on m68k-linux

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* mpf expression evaluation */
 
2
 
 
3
/*
 
4
Copyright 2000, 2001 Free Software Foundation, Inc.
 
5
 
 
6
This file is part of the GNU MP Library.
 
7
 
 
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.
 
12
 
 
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.
 
17
 
 
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,
 
21
MA 02111-1307, USA.
 
22
*/
 
23
 
 
24
#include <stdio.h>
 
25
#include "gmp.h"
 
26
#include "expr-impl.h"
 
27
 
 
28
 
 
29
/* Change this to "#define TRACE(x) x" to get some traces. */
 
30
#define TRACE(x)
 
31
 
 
32
 
 
33
static int
 
34
e_mpf_sgn (mpf_srcptr x)
 
35
{
 
36
  return mpf_sgn (x);
 
37
}
 
38
 
 
39
 
 
40
static __gmp_const struct mpexpr_operator_t  _mpf_expr_standard_table[] = {
 
41
 
 
42
  { "**",  (mpexpr_fun_t) mpf_pow_ui,
 
43
    MPEXPR_TYPE_BINARY_UI | MPEXPR_TYPE_RIGHTASSOC,                   220 },
 
44
  
 
45
  { "!",   (mpexpr_fun_t) e_mpf_sgn,
 
46
    MPEXPR_TYPE_LOGICAL_NOT | MPEXPR_TYPE_PREFIX,                     210 },
 
47
  { "-",   (mpexpr_fun_t) mpf_neg,
 
48
    MPEXPR_TYPE_UNARY | MPEXPR_TYPE_PREFIX,                           210 },
 
49
 
 
50
  { "*",   (mpexpr_fun_t) mpf_mul,           MPEXPR_TYPE_BINARY,      200 },
 
51
  { "/",   (mpexpr_fun_t) mpf_div,           MPEXPR_TYPE_BINARY,      200 },
 
52
 
 
53
  { "+",   (mpexpr_fun_t) mpf_add,           MPEXPR_TYPE_BINARY,      190 },
 
54
  { "-",   (mpexpr_fun_t) mpf_sub,           MPEXPR_TYPE_BINARY,      190 },
 
55
 
 
56
  { "<<",  (mpexpr_fun_t) mpf_mul_2exp,      MPEXPR_TYPE_BINARY_UI,   180 },
 
57
  { ">>",  (mpexpr_fun_t) mpf_div_2exp,      MPEXPR_TYPE_BINARY_UI,   180 },
 
58
 
 
59
  { "<=",  (mpexpr_fun_t) mpf_cmp,           MPEXPR_TYPE_CMP_LE,      170 },
 
60
  { "<",   (mpexpr_fun_t) mpf_cmp,           MPEXPR_TYPE_CMP_LT,      170 },
 
61
  { ">=",  (mpexpr_fun_t) mpf_cmp,           MPEXPR_TYPE_CMP_GE,      170 },
 
62
  { ">",   (mpexpr_fun_t) mpf_cmp,           MPEXPR_TYPE_CMP_GT,      170 },
 
63
 
 
64
  { "==",  (mpexpr_fun_t) mpf_cmp,           MPEXPR_TYPE_CMP_EQ,      160 },
 
65
  { "!=",  (mpexpr_fun_t) mpf_cmp,           MPEXPR_TYPE_CMP_NE,      160 },
 
66
 
 
67
  { "&&",  (mpexpr_fun_t) e_mpf_sgn,         MPEXPR_TYPE_LOGICAL_AND, 120 },
 
68
  { "||",  (mpexpr_fun_t) e_mpf_sgn,         MPEXPR_TYPE_LOGICAL_OR,  110 },
 
69
 
 
70
  { ":",   NULL,                             MPEXPR_TYPE_COLON,       101 },
 
71
  { "?",   (mpexpr_fun_t) e_mpf_sgn,         MPEXPR_TYPE_QUESTION,    100 },
 
72
 
 
73
  { ")",   NULL,                             MPEXPR_TYPE_CLOSEPAREN,    4 },
 
74
  { "(",   NULL,                             MPEXPR_TYPE_OPENPAREN,     3 },
 
75
  { ",",   NULL,                             MPEXPR_TYPE_ARGSEP,        2 },
 
76
  { "$",   NULL,                             MPEXPR_TYPE_VARIABLE,      1 },
 
77
 
 
78
  { "abs",      (mpexpr_fun_t) mpf_abs,          MPEXPR_TYPE_UNARY        },
 
79
  { "ceil",     (mpexpr_fun_t) mpf_ceil,         MPEXPR_TYPE_UNARY        },
 
80
  { "cmp",      (mpexpr_fun_t) mpf_cmp,          MPEXPR_TYPE_I_BINARY     },
 
81
  { "eq",       (mpexpr_fun_t) mpf_eq,           MPEXPR_TYPE_I_TERNARY_UI },
 
82
  { "floor",    (mpexpr_fun_t) mpf_floor,        MPEXPR_TYPE_UNARY        },
 
83
  { "integer_p",(mpexpr_fun_t) mpf_integer_p,    MPEXPR_TYPE_I_UNARY      },
 
84
  { "max",   (mpexpr_fun_t) mpf_cmp, MPEXPR_TYPE_MAX | MPEXPR_TYPE_PAIRWISE },
 
85
  { "min",   (mpexpr_fun_t) mpf_cmp, MPEXPR_TYPE_MIN | MPEXPR_TYPE_PAIRWISE },
 
86
  { "reldiff",  (mpexpr_fun_t) mpf_reldiff,      MPEXPR_TYPE_BINARY       },
 
87
  { "sgn",      (mpexpr_fun_t) e_mpf_sgn,        MPEXPR_TYPE_I_UNARY      },
 
88
  { "sqrt",     (mpexpr_fun_t) mpf_sqrt,         MPEXPR_TYPE_UNARY        },
 
89
  { "trunc",    (mpexpr_fun_t) mpf_trunc,        MPEXPR_TYPE_UNARY        },
 
90
 
 
91
  { NULL }
 
92
};
 
93
 
 
94
__gmp_const struct mpexpr_operator_t * __gmp_const mpf_expr_standard_table
 
95
= _mpf_expr_standard_table;
 
96
 
 
97
 
 
98
int
 
99
#if HAVE_STDARG
 
100
mpf_expr (mpf_ptr res, int base, __gmp_const char *e, ...)
 
101
#else
 
102
mpf_expr (va_alist)
 
103
     va_dcl
 
104
#endif
 
105
{
 
106
  mpf_srcptr  var[MPEXPR_VARIABLES];
 
107
  va_list     ap;
 
108
  int         ret;
 
109
#if HAVE_STDARG
 
110
  va_start (ap, e);
 
111
#else
 
112
  mpf_ptr           res;
 
113
  int               base;
 
114
  __gmp_const char  *e;
 
115
  va_start (ap);
 
116
  res  = va_arg (ap, mpf_ptr);
 
117
  base = va_arg (ap, int);
 
118
  e    = va_arg (ap, __gmp_const char *);
 
119
#endif
 
120
 
 
121
  TRACE (printf ("mpf_expr(): base %d, %s\n", base, e));
 
122
  ret = mpexpr_va_to_var ((void **) var, ap);
 
123
  va_end (ap);
 
124
 
 
125
  if (ret != MPEXPR_RESULT_OK)
 
126
    return ret;
 
127
 
 
128
  return mpf_expr_a (mpf_expr_standard_table, res, base,
 
129
                     mpf_get_prec (res), e, strlen(e), var);
 
130
}