You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

145 lines
3.9KB

  1. /*
  2. * This file is part of Libav.
  3. *
  4. * Libav is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * Libav is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with Libav; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include <math.h>
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include "timer.h"
  22. #include "eval.h"
  23. static const double const_values[] = {
  24. M_PI,
  25. M_E,
  26. 0
  27. };
  28. static const char *const const_names[] = {
  29. "PI",
  30. "E",
  31. 0
  32. };
  33. int main(int argc, char **argv)
  34. {
  35. int i;
  36. double d;
  37. const char *const *expr;
  38. static const char *const exprs[] = {
  39. "",
  40. "1;2",
  41. "-20",
  42. "-PI",
  43. "+PI",
  44. "1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)",
  45. "80G/80Gi",
  46. "1k",
  47. "1Gi",
  48. "1gi",
  49. "1GiFoo",
  50. "1k+1k",
  51. "1Gi*3foo",
  52. "foo",
  53. "foo(",
  54. "foo()",
  55. "foo)",
  56. "sin",
  57. "sin(",
  58. "sin()",
  59. "sin)",
  60. "sin 10",
  61. "sin(1,2,3)",
  62. "sin(1 )",
  63. "1",
  64. "1foo",
  65. "bar + PI + E + 100f*2 + foo",
  66. "13k + 12f - foo(1, 2)",
  67. "1gi",
  68. "1Gi",
  69. "st(0, 123)",
  70. "st(1, 123); ld(1)",
  71. "lte(0, 1)",
  72. "lte(1, 1)",
  73. "lte(1, 0)",
  74. "lt(0, 1)",
  75. "lt(1, 1)",
  76. "gt(1, 0)",
  77. "gt(2, 7)",
  78. "gte(122, 122)",
  79. /* compute 1+2+...+N */
  80. "st(0, 1); while(lte(ld(0), 100), st(1, ld(1)+ld(0));st(0, ld(0)+1)); ld(1)",
  81. /* compute Fib(N) */
  82. "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)",
  83. "while(0, 10)",
  84. "st(0, 1); while(lte(ld(0),100), st(1, ld(1)+ld(0)); st(0, ld(0)+1))",
  85. "isnan(1)",
  86. "isnan(NAN)",
  87. "isnan(INF)",
  88. "isinf(1)",
  89. "isinf(NAN)",
  90. "isinf(INF)",
  91. "floor(NAN)",
  92. "floor(123.123)",
  93. "floor(-123.123)",
  94. "trunc(123.123)",
  95. "trunc(-123.123)",
  96. "ceil(123.123)",
  97. "ceil(-123.123)",
  98. "sqrt(1764)",
  99. "isnan(sqrt(-1))",
  100. "not(1)",
  101. "not(NAN)",
  102. "not(0)",
  103. "6.0206dB",
  104. "-3.0103dB",
  105. NULL
  106. };
  107. for (expr = exprs; *expr; expr++) {
  108. printf("Evaluating '%s'\n", *expr);
  109. av_expr_parse_and_eval(&d, *expr,
  110. const_names, const_values,
  111. NULL, NULL, NULL, NULL, NULL, 0, NULL);
  112. if (isnan(d))
  113. printf("'%s' -> nan\n\n", *expr);
  114. else
  115. printf("'%s' -> %f\n\n", *expr, d);
  116. }
  117. av_expr_parse_and_eval(&d, "1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)",
  118. const_names, const_values,
  119. NULL, NULL, NULL, NULL, NULL, 0, NULL);
  120. printf("%f == 12.7\n", d);
  121. av_expr_parse_and_eval(&d, "80G/80Gi",
  122. const_names, const_values,
  123. NULL, NULL, NULL, NULL, NULL, 0, NULL);
  124. printf("%f == 0.931322575\n", d);
  125. if (argc > 1 && !strcmp(argv[1], "-t")) {
  126. for (i = 0; i < 1050; i++) {
  127. START_TIMER;
  128. av_expr_parse_and_eval(&d, "1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)",
  129. const_names, const_values,
  130. NULL, NULL, NULL, NULL, NULL, 0, NULL);
  131. STOP_TIMER("av_expr_parse_and_eval");
  132. }
  133. }
  134. return 0;
  135. }