|  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 x86_64
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: alexfbp at gmail dot com
New email:
PHP Version: OS:


 [2014-08-01 18:27 UTC] alexfbp at gmail dot com
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 ( 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) {
} else {

Expected result:
In both cases ($log10 true and false), the var_dump() output could be like:

Actual result:
Works as expected for $log10=true, not for false:


Add a Patch

Pull Requests

Add a Pull Request


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]
-Status: Open +Status: Not a bug
 [2014-08-01 22:22 UTC]
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:

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]
-Status: Not a bug +Status: Closed -Assigned To: +Assigned To: pollita
 [2014-08-14 18:14 UTC]
Fixed in master. In hindsight I could have left this open as a change request.
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Jun 23 08:01:28 2024 UTC