46 double (*
const *
funcs1)(
void *,
double a);
48 double (*
const *
funcs2)(
void *,
double a,
double b);
95 if(numstr[0]==
'0' && (numstr[1]|0x20)==
'x') {
96 d = strtoul(numstr, &next, 16);
101 if (next[0] ==
'd' && next[1] ==
'B') {
105 }
else if (*next >=
'E' && *next <=
'z') {
108 if (next[1] ==
'i') {
130 #define IS_IDENTIFIER_CHAR(c) ((c) - '0' <= 9U || (c) - 'a' <= 25U || (c) - 'A' <= 25U || (c) == '_')
135 for (i=0; prefix[i]; i++) {
136 if (prefix[i] != s[i])
return 0;
157 double (*
func2)(
void *, double, double);
198 av_log(p, level,
"%f\n", x);
204 r= r*1664525+1013904223;
206 return e->
value * (r * (1.0/UINT64_MAX));
215 double t = 1, d = 0,
v;
219 double var0 = p->
var[
id];
220 for(i=0; i<1000; i++) {
234 double low = -1, high = -1,
v, low_v = -DBL_MAX, high_v = DBL_MAX;
235 double var0 = p->
var[0];
237 for(i=-1; i<1024; i++) {
241 p->
var[0] = x_max*pow(0.9, i-255);
242 if (i&1) p->
var[0] *= -1;
243 if (i&2) p->
var[0] += low;
244 else p->
var[0] += high;
247 if (v<=0 && v>low_v) {
251 if (
v>=0 &&
v<high_v) {
255 if (low>=0 && high>=0){
256 for (j=0; j<1000; j++) {
257 p->
var[0] = (low+high)*0.5;
258 if (low == p->
var[0] || high == p->
var[0])
261 if (
v<=0) low = p->
var[0];
262 if (
v>=0) high= p->
var[0];
272 return -low_v<high_v ? low : high;
278 case e_mod:
return e->
value * (d - floor((!CONFIG_FTRAPV || d2) ? d / d2 : d *
INFINITY) * d2);
280 case e_max:
return e->
value * (d > d2 ? d : d2);
281 case e_min:
return e->
value * (d < d2 ? d : d2);
282 case e_eq:
return e->
value * (d == d2 ? 1.0 : 0.0);
283 case e_gt:
return e->
value * (d > d2 ? 1.0 : 0.0);
284 case e_gte:
return e->
value * (d >= d2 ? 1.0 : 0.0);
285 case e_lt:
return e->
value * (d < d2 ? 1.0 : 0.0);
286 case e_lte:
return e->
value * (d <= d2 ? 1.0 : 0.0);
317 char *next = p->
s, *
s0 = p->
s;
353 p->
s= strchr(p->
s,
'(');
365 if (p->
s[0] !=
')') {
386 if (p->
s[0] !=
')') {
481 *sign= (*p->
s ==
'+') - (*p->
s ==
'-');
493 if (next != p->
s && next[0] ==
'd' && next[1] ==
'B') {
503 int sign, sign2,
ret;
505 if ((ret =
parse_dB(&e0, p, &sign)) < 0)
510 if ((ret =
parse_dB(&e2, p, &sign2)) < 0) {
522 if (e0) e0->
value *= (sign|1);
534 while (p->
s[0]==
'*' || p->
s[0]==
'/') {
558 while (*p->
s ==
'+' || *p->
s ==
'-') {
586 while (*p->
s ==
';') {
645 const char *
const *func2_names,
double (*
const *funcs2)(
void *,
double,
double),
646 int log_offset,
void *log_ctx)
704 const char *
const *const_names,
const double *
const_values,
705 const char *
const *func1_names,
double (*
const *
funcs1)(
void *,
double),
706 const char *
const *func2_names,
double (*
const *funcs2)(
void *,
double,
double),
707 void *opaque,
int log_offset,
void *log_ctx)
710 int ret =
av_expr_parse(&e, s, const_names, func1_names,
funcs1, func2_names, funcs2, log_offset, log_ctx);
724 static const double const_values[] = {
730 static const char *
const const_names[] = {
736 int main(
int argc,
char **argv)
740 const char *
const *expr;
741 static const char *
const exprs[] = {
747 "1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)",
768 "bar + PI + E + 100f*2 + foo",
769 "13k + 12f - foo(1, 2)",
783 "st(0, 1); while(lte(ld(0), 100), st(1, ld(1)+ld(0));st(0, ld(0)+1)); ld(1)",
785 "st(1, 1); st(2, 2); st(0, 1); while(lte(ld(0),10), st(3, ld(1)+ld(2)); st(1, ld(2)); st(2, ld(3)); st(0, ld(0)+1)); ld(3)",
787 "st(0, 1); while(lte(ld(0),100), st(1, ld(1)+ld(0)); st(0, ld(0)+1))",
816 "ifnot(1, NaN) + if(0, 1)",
820 "taylor(eq(mod(ld(1),4),1)-eq(mod(ld(1),4),3), PI/2, 1)",
821 "root(sin(ld(0))-1, 2)",
822 "root(sin(ld(0))+6+sin(ld(0)/12)-log(ld(0)), 100)",
823 "7000000B*random(0)",
827 "gcd(30,55)*print(min(9,1))",
831 "between(10, -3, 10)",
832 "between(-4, -2, -1)",
837 for (expr = exprs; *expr; expr++) {
838 printf(
"Evaluating '%s'\n", *expr);
840 const_names, const_values,
841 NULL, NULL, NULL, NULL, NULL, 0, NULL);
843 printf(
"'%s' -> nan\n\n", *expr);
845 printf(
"'%s' -> %f\n\n", *expr, d);
849 const_names, const_values,
850 NULL, NULL, NULL, NULL, NULL, 0, NULL);
851 printf(
"%f == 12.7\n", d);
853 const_names, const_values,
854 NULL, NULL, NULL, NULL, NULL, 0, NULL);
855 printf(
"%f == 0.931322575\n", d);
857 if (argc > 1 && !strcmp(argv[1],
"-t")) {
858 for (i = 0; i < 1050; i++) {
861 const_names, const_values,
862 NULL, NULL, NULL, NULL, NULL, 0, NULL);