Bug #67738 [Com]: log(x,10) and log10(x) are different

From: Date: Fri, 01 Aug 2014 21:54:48 +0000
Subject: Bug #67738 [Com]: log(x,10) and log10(x) are different
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-186929@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=67738&edit=1 ID: 67738 Comment by: phpmpan at mpan dot pl Reported by: alexfbp at gmail dot com Summary: log(x,10) and log10(x) are different Status: Open Type: Bug Package: Math related Operating System: Linux 2.6.32.3 x86_64 PHP Version: 5.3.28 Block user comment: N Private report: N New Comment: This is not an error, but an expected, natural and **perfectly valid** behaviour of floating point numbers. 2.9999999999 and 3.0 are equivalent in terms of FP. log10 is working with a specific set of cases and hence may give numbers that differ less from the value of the corresponding function in ℝ, but its not expected from any general-purpose FP function like log to "polish" results to make them more pleasant to the reader's eye. Also floor(x) was never meant to return result of ⌊x⌋ in ℝ. Previous Comments: ------------------------------------------------------------------------ [2014-08-01 18:27:24] alexfbp at gmail dot com Description: ------------ Appears that internally the log(x,10) and log10(x) gets different result: For log(1000,10) maybe internally the result are 2.9999999999... maybe rounded to 3 when displayed (see the sample codes) The log10(1000) works as expected. I noticed of that when i was doing a class that i shared on phpclasses.org (http://www.phpclasses.org/package/8622) which gets the SI prefixed number of a number / numeric string. The precision are not representative, and i understand that the float format are not enough precise. It did not occur to me(perhaps by time or tiredness) better alternative to change the function by log10 when the base to be used is 10. (in the example, $log10=true) Test script: --------------- $log10=true; //Change to "false" for the second test if ($log10) { $aux1=log10(1000); } else { $aux1=log(1000,10); } $aux2=floor($aux1); $aux3=(int)$aux2; var_dump($aux1,$aux2,$aux3); Expected result: ---------------- In both cases ($log10 true and false), the var_dump() output could be like: float(3) float(3) int(3) Actual result: -------------- Works as expected for $log10=true, not for false: float(3) float(2) int(2) ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=67738&edit=1

« previous php.bugs (#186929) next »