-
Notifications
You must be signed in to change notification settings - Fork 7.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/log2 #337
Feature/log2 #337
Conversation
... generating the base-2 logarithm of a number much faster than with log(x, 2)
Having basic math functions is good. IMO. |
Thanks for this pull request. We usually try to avoid adding new global functions to the core. We wan't to avoid clashes with existing functions and avoid maintenance overhead. In this case there is a log(X, 2) equivalent to log2 so it would be just an abbrev that saves you 3 chars, which is not sufficient to have possible BC breaks in existing code bases. Imagine people using: function log2($x) {
return log($x, 2);
} in their code. The new function would cause a fatal error for close to no gain. Therefore I am closing this PR. |
@dsp Dit you read the example of the logarithm of 2 issue (not bug) on 32bit platforms using the method var_dump(log2(64), (int)log2(64)); // float(6) & int(6)
var_dump(log(64, 2), (int)log(64, 2)); // float(6) & int(5) That's the reason I added it - not saving of 2 characters. |
@marc-mabe The example is a rounding error in floating point calculation. It's a standard problem when casting floats to int, therefore you cannot rely on the conversation (int)x==(float)y. Therefore I don't think this is a bug, but rather just happens due to how floating point works. Also if it one might consider it being a bug, we want to start fixing it in log(). For example check if the second argument in log() is 2 and use log2() internally instead to be less prone to floating point issues. |
@dsp As noted above it's not a bug, it's an issue / problem. Sure it's coming from rounding errors but Calling Also for consistency I think the already implemented methods |
Please create a new PR. Also ensure that we check if log2 is available on the system and ifdef the code if necessary. PHP relys on C89 and not C99 and has to work with multiple compilers like ICC/SunCC, etc on different platforms (including Solaris, AIX, etc). Not all of them might have a C99 compatible libc. |
@marc-mabe log1p can't be implemented using log because it has different accuracy requirements. Same for expm1 and exp. |
@nikic you are right - but So |
This PR adds the function
log2
. It works the same as the C function and is an addition to the always translated C functions to PHP likelog1p
andlog10
It also fixes an issue on 32bit environments calculating the second logarithm as an integer:
The method
log2
is part of the C99 standard - so there is no HAVE_LOG2 check done.(Not tested if some environments doesn't support it)