Skip to content

Commit

Permalink
Fixed issue #07277: log(xx) seems to return an incorrect value
Browse files Browse the repository at this point in the history
Dev: add optionnal setting to log for base.
  • Loading branch information
Shnoulle committed Feb 7, 2013
1 parent c8c2284 commit e662664
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
24 changes: 21 additions & 3 deletions application/helpers/expressions/em_core_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ function __construct()
'is_string' => array('is_string', 'LEMis_string', $this->gT('Find whether the type of a variable is string'), 'bool is_string(var)', 'http://www.php.net/manual/en/function.is-string.php', 1),
'join' => array('exprmgr_join', 'LEMjoin', $this->gT('Join strings, return joined string.This function is an alias of implode("",argN)'), 'string join(arg1,arg2,...,argN)', '', -1),
'list' => array('exprmgr_list', 'LEMlist', $this->gT('Return comma-separated list of values'), 'string list(arg1, arg2, ... argN)', '', -2),
'log' => array('log', 'Math.log', $this->gT('Natural logarithm'), 'number log(number)', 'http://www.php.net/manual/en/function.log.php', 1),
'log' => array('exprmgr_log', 'LEMlog', $this->gT('Natural logarithm'), 'number log(number,base=e)', 'http://www.php.net/manual/en/function.log.php', -2),
'ltrim' => array('ltrim', 'ltrim', $this->gT('Strip whitespace (or other characters) from the beginning of a string'), 'string ltrim(string [, charlist])', 'http://www.php.net/manual/en/function.ltrim.php', 1,2),
'max' => array('max', 'Math.max', $this->gT('Find highest value'), 'number max(arg1, arg2, ... argN)', 'http://www.php.net/manual/en/function.max.php', -2),
'min' => array('min', 'Math.min', $this->gT('Find lowest value'), 'number min(arg1, arg2, ... argN)', 'http://www.php.net/manual/en/function.min.php', -2),
Expand Down Expand Up @@ -2000,7 +2000,7 @@ private function RDP_RunFunction($funcNameToken,$params)
if (!$this->RDP_onlyparse) {
switch($funcName) {
case 'sprintf':
// PHP doesn't let you pass array of parameters to sprintf, so must use call_user_func_array
// PHP doesn't let you pass array of parameters to function, so must use call_user_func_array
$result = call_user_func_array('sprintf',$params);
break;
default:
Expand All @@ -2026,7 +2026,6 @@ private function RDP_RunFunction($funcNameToken,$params)
case 'cos':
case 'exp':
case 'is_nan':
case 'log':
case 'sin':
case 'sqrt':
case 'tan':
Expand Down Expand Up @@ -3385,6 +3384,25 @@ function exprmgr_list($args)
return $result;
}

/**
* return log($arg[0],$arg[1]=e)
* @param <type> $args
* @return float
*/
function exprmgr_log($args)
{
if (count($args) < 1)
{
return NAN;
}
$number=$args[0];
if(!is_numeric($number)){return NAN;}
$base=(isset($args[1]))?$args[1]:exp(1);
if(!is_numeric($base)){return NAN;}
if(floatval($base)<=0){return NAN;}
return log($number,$base);
}

/**
* Join together $args[N]
* @param <type> $args
Expand Down
25 changes: 25 additions & 0 deletions scripts/expressions/em_javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* Core JavaScript functions needed by ExpressionManager
* @author Thomas M. White (TMSWhite)
* @author Denis Chenu (Shnoulle)
*
* Portion from php.js is copyright 2012 Kevin van Zonneveld.
* php.js is dual licensed under the MIT licenses.
*/
Expand Down Expand Up @@ -223,6 +224,30 @@ function LEMlist()
return result;
}

/**
* Returns Natural logarithm of a number
*/

function LEMlog()
{
// takes variable number of arguments
if (arguments.length < 1) {
return NaN;
}
var base=Math.exp(1);
if(arguments.length>1){
base = arguments[1];
if (isNaN(base)) { return NaN;}
if (base<=0 ) { return NaN;}
base=Math.abs(parseFloat(arguments[1]));
}
if(base==Math.exp(1)){// Not needed
return Math.log(arguments[0]);
}else{
return Math.log(arguments[0])/Math.log(base);
}
}

/**
* Returns concatenates list
*/
Expand Down

0 comments on commit e662664

Please sign in to comment.