Skip to content

Commit

Permalink
MDL-27659 "have evalmath accept numbers expressed with scientific
Browse files Browse the repository at this point in the history
notation"
  • Loading branch information
jamiepratt committed May 28, 2011
1 parent ecf5200 commit 631c0ed
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/evalmath/evalmath.class.php
Expand Up @@ -212,7 +212,7 @@ function nfx($expr) {
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
Expand Down
18 changes: 18 additions & 0 deletions lib/simpletest/testmathslib.php
Expand Up @@ -194,6 +194,24 @@ public function test_rounding_function() {

}

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);

}

}


0 comments on commit 631c0ed

Please sign in to comment.