Skip to content
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

Implement integer power method #62

Closed
wants to merge 5 commits into from
Closed

Conversation

CyDragon80
Copy link
Contributor

@CyDragon80 CyDragon80 commented Jun 18, 2018

Basically took the suggestions from #46 and integrated them. For handling negative exponents, I'm not sure if we can just return zero or if there is a mathematical fringe case requiring us to go through the full motions, so I just left the full computation. Based on the other functions, it appears to be assumed that the function should return a new Long instance, so I took steps to ensure that only a new Long is returned from the power function.

@CyDragon80
Copy link
Contributor Author

CyDragon80 commented Jun 19, 2018

Actually other functions will return whatever is fastest to return, so I'm dropping the implicit clones. (Not sure what I was thinking, maybe it was late. This library typically favors speed.) I'm leaving the clone method itself as there does not appear to be a method currently for making a quick deep copy. That way if someone really needs to ensure a clean break from a previous instance, they can call clone explicitly on their end.

@CyDragon80
Copy link
Contributor Author

For negative exponents, I can't think of anything other than one not truncating to zero, so might as well short circuit on that condition. Also it is probably safe to assume no one is using an exponent larger than 32 bit, as that will overflow for anything besides one, which means the exponent does not need 64 bit operations. Does that make sense?

@CyDragon80
Copy link
Contributor Author

CyDragon80 commented Jun 20, 2018

I briefly looked at the method described here based on a discussion here. In my test cases it uses more multiplies, but the routine has less branching. For now the squares method is a little cleaner, but it could always be changed. There is also a version of squares without recursion in that thread, but that seems to also result in more multiplies.

Later today I want to look at some unsigned test cases.

@CyDragon80
Copy link
Contributor Author

CyDragon80 commented Jun 21, 2018

I did a couple of very crude timed loop tests and the recursive squares seems to edge out the others maybe 7-10%, but as I say it was a rather quick and dirty test.
I reworked some checks and treat zero to the zero power as one.
I also added a couple unsigned tests along with some fixes resulting from that.

@CyDragon80 CyDragon80 force-pushed the power branch 2 times, most recently from 3b36522 to e2f4beb Compare June 21, 2018 13:45
@CyDragon80
Copy link
Contributor Author

At this point I should probably invite input from the folks that requested the feature. (Otherwise I'm just debating with the voices in my head.)
@drauschenbach @olydis @nicholasxuu

@kamronbatman
Copy link

This looks good. @CyDragon80, any chance this can get cleaned up, resolved conflicts, and merged?

@CyDragon80
Copy link
Contributor Author

Created a cleaner PR over here: #76

@CyDragon80 CyDragon80 closed this Dec 5, 2018
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.

2 participants