Skip to content

Conversation

marc-mabe
Copy link
Contributor

... to reduce rounding errors.

Internally on log(x, 2) the native C function log2(x) and on log(x, 10) the native C function log10(x) will be called.

Because of after this PR the following will be the same in PHP log10(x) === log10(x) the function log10 is needless and could be deprecated/removed. I will create an RFC if this PR was merged.

This PR is against PHP-5.4 as there is no BC break.
Please merge into PHP-5.4 up to master.
Please tell me if I did something wrong.

if (ZEND_NUM_ARGS() == 1) {
RETURN_DOUBLE(log(num));
}
if (base <= 0.0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you move this statement below most of the others? Exit early seems like a better idea to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because of it's an error condition that shouldn't be the normal case. So on calculate one of the pre-defined base logarithms the one operation to check against <= 0 isn't needed.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I'm misunderstanding it, but it looks like for base 2, it skips the error check entirely? :/

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TazeTSchnitzel for base 2 it will either get caught by log2() or falls down into log(num) / log(2.0).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

log2 is a C function, yes? It's not going to call RETURN_FALSE.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TazeTSchnitzel I'm afraid you've lost me :) why should anything error out when base is 2?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...oh, right. I'm an idiot, ignore me.

By the way, would this be nicer if structured as a switch statement?

@php-pulls
Copy link

Comment on behalf of pollita at php.net:

Merged

@php-pulls php-pulls closed this Aug 14, 2014
@marc-mabe marc-mabe deleted the math_log_improvement branch October 24, 2014 19:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants