-
Notifications
You must be signed in to change notification settings - Fork 1
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
Math support: pow() --use log for efficiency increase #123
Comments
This is related to tetration, the fourth-order hyper-operation. Note: it's unclear if we can gain any actual performance benefit by going down this route, so far all I can prove in my head is that we can use |
Having said this, the current implementation uses recursion, which is vulnerable to stack overflow on very large inputs. If we go ahead with our We might still have a few recursive calls for the base cases and to handle the "extra bit" that doesn't fit within a power of 2, but this will be a minority of calls. |
We could also get rid of repeated calls to
|
I'm pretty sure it is hyper-powers/tetration that we can use here, initial simple proof (where left-superscript defines tetration): Attempting to generalise with a base that differs from 2 and any intermediate exponents: I'm not sure this is actually true tetration in the literal sense, but it is certainly related. Psuedocode for generalised algorithm:
Note: this algorithm doesn't fully work when log is an integer log function, but that can be factored easily enough. It demonstrates that the main iterative part of the algorithm uses no division, only multiplication. Handling the left over bit (assuming p as floor-log is used) will simply require calculating the |
Current code for
pow()
uses divide-and-conquer recursion to avoid a degenerate-case linear recursion, which is very very very slow for large exponents.However, we still manually repeatedly divide the exponent by$2$ to work out how many powers of $2$ this is, roughly. We then use this information to use the laws of powers to combine smaller powers into increasingly larger powers.
This repeated division is equivalent to calculating the base-2 logarithm,$log{2}(b)$ . We should consider using our
ilog()
function to do this instead.Whenever our exponent is not an exact power of$2$ , there will be an additional bit that needs to be dealt with to get the full result.
The text was updated successfully, but these errors were encountered: