Bug #67738 [Nab->Csd]: log(x,10) and log10(x) are different
| From: | requinix@php.net | Date: | Thu, 14 Aug 2014 18:14:51 +0000 |
| Subject: | Bug #67738 [Nab->Csd]: log(x,10) and log10(x) are different | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-187130@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
Updated by: requinix@php.net
Reported by: alexfbp at gmail dot com
Summary: log(x,10) and log10(x) are different
-Status: Not a bug
+Status: Closed
Type: Bug
Package: Math related
Operating System: Linux 2.6.32.3 x86_64
PHP Version: 5.3.28
-Assigned To:
+Assigned To: pollita
Block user comment: N
Private report: N
New Comment:
Fixed in master. In hindsight I could have left this open as a change request.
https://github.com/php/php-src/pull/658
https://github.com/php/php-src/commit/37e91cc5d34598b05bcd377f3115f36b43e786ac
Previous Comments:
------------------------------------------------------------------------
[2014-08-01 22:22:31] requinix@php.net
Floating point values have a limited precision. Hence a value might
not have the same string representation after any processing. That also
includes writing a floating point value in your script and directly
printing it without any mathematical operations.
If you would like to know more about "floats" and what IEEE
754 is, read this:
http://www.floating-point-gui.de/
Thank you for your interest in PHP.
log10() uses C's own log10().
log(X,Y) does math: log(X)/log(Y). Logarithms and division combine to give you the 2.999... result.
As with pretty much all floating-point arithmetic, round to a few decimal places at the very end.
You may also be interested in the precision and serialize_precision php.ini settings.
------------------------------------------------------------------------
[2014-08-01 21:54:47] phpmpan at mpan dot pl
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 â.
------------------------------------------------------------------------
[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