php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #67738 log(x,10) and log10(x) are different
Submitted: 2014-08-01 18:27 UTC Modified: 2014-08-14 18:14 UTC
From: alexfbp at gmail dot com Assigned: pollita (profile)
Status: Closed Package: Math related
PHP Version: 5.3.28 OS: Linux 2.6.32.3 x86_64
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: alexfbp at gmail dot com
New email:
PHP Version: OS:

 

 [2014-08-01 18:27 UTC] 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)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-08-01 21:54 UTC] 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 22:22 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2014-08-01 22:22 UTC] 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-14 18:14 UTC] requinix@php.net
-Status: Not a bug +Status: Closed -Assigned To: +Assigned To: pollita
 [2014-08-14 18:14 UTC] requinix@php.net
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
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Dec 30 14:01:28 2024 UTC