-
Notifications
You must be signed in to change notification settings - Fork 205
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
Conversation
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. |
… to zero (unless value was one).
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? |
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. |
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. |
3b36522
to
e2f4beb
Compare
…o the zero power as one.
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.) |
This looks good. @CyDragon80, any chance this can get cleaned up, resolved conflicts, and merged? |
Created a cleaner PR over here: #76 |
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.