From 9085134ed7230734bdccd1d3ff9bd11cf5d9e6da Mon Sep 17 00:00:00 2001 From: Jamie Pratt Date: Sat, 28 May 2011 20:47:34 +0700 Subject: [PATCH] MDL-27659 "have evalmath accept numbers expressed with scientific notation" --- lib/evalmath/evalmath.class.php | 2 +- lib/simpletest/testmathslib.php | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/evalmath/evalmath.class.php b/lib/evalmath/evalmath.class.php index 61ebf92dc6d..6705ed211c2 100644 --- a/lib/evalmath/evalmath.class.php +++ b/lib/evalmath/evalmath.class.php @@ -212,7 +212,7 @@ class EvalMath { while(1) { // 1 Infinite Loop ;) $op = substr($expr, $index, 1); // get the first character at the current index // find out if we're currently at the beginning of a number/variable/function/parenthesis/operand - $ex = preg_match('/^('.self::$namepat.'\(?|\d+(?:\.\d*)?|\.\d+|\()/', substr($expr, $index), $match); + $ex = preg_match('/^('.self::$namepat.'\(?|\d+(?:\.\d*)?(?:(e[+-]?)\d*)?|\.\d+|\()/', substr($expr, $index), $match); //=============== if ($op == '-' and !$expecting_op) { // is it a negation instead of a minus? $stack->push('_'); // put a negation on the stack diff --git a/lib/simpletest/testmathslib.php b/lib/simpletest/testmathslib.php index 4144c22b0bb..10dffcb8f77 100644 --- a/lib/simpletest/testmathslib.php +++ b/lib/simpletest/testmathslib.php @@ -194,6 +194,24 @@ class mathsslib_test extends UnitTestCase { } + public function test_scientific_notation() { + $formula = new calc_formula('=10e10'); + $this->assertWithinMargin($formula->evaluate(), 1e11, 1e11*1e-15); + + $formula = new calc_formula('=10e-10'); + $this->assertWithinMargin($formula->evaluate(), 1e-9, 1e11*1e-15); + + $formula = new calc_formula('=10e+10'); + $this->assertWithinMargin($formula->evaluate(), 1e11, 1e11*1e-15); + + $formula = new calc_formula('=10e10*5'); + $this->assertWithinMargin($formula->evaluate(), 5e11, 1e11*1e-15); + + $formula = new calc_formula('=10e10^2'); + $this->assertWithinMargin($formula->evaluate(), 1e22, 1e22*1e-15); + + } + } -- 2.43.0