-
Notifications
You must be signed in to change notification settings - Fork 22
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
Support computing values for all rational powers #213
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
I thought we would need to wait for a `constexpr` version of `std::powl`, but I was wrong. Instead, we can get away with a `root` function. Note that there's no guarantee that `std::powl` would be better in all cases, even if we _could_ just use it. After all, its second argument is `1.0l / n`, which is a lossy representation of the Nth root. On the implementation side, we take advantage of the fact that this function is only ever meant to be called at compile time, which lets us prioritize accuracy over "speed" (because the runtime speed is always essentially infinite). We do a binary search between 1 and `x` after checking that `x > 1` (and `n > 1`). When we end up with two neighboring floating point values, we pick whichever gives the closest result when we run it through `checked_int_pow`. This algorithm is guaranteed to produce the best representable result for a "pure root". For other rational powers, it's technically possible that we could have some lossiness in the `checked_int_pow` computation, if we enter a floating point realm that is high enough such that not all integer values can be represented. That said, I tried a bunch of test cases to compare it to `powl`, and it's passed all the ones that I was able to come up with. Fixes #116.
gcc is not standards compliant (!!) https://stackoverflow.com/a/5483978/15777264
geoffviola
reviewed
Dec 18, 2023
geoffviola
approved these changes
Dec 19, 2023
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Algorithm makes sense. New API looks useful. Tests look good.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I thought we would need to wait for a
constexpr
version ofstd::powl
, but I was wrong. Instead, we can get away with aroot
function. Note that there's no guarantee that
std::powl
would bebetter in all cases, even if we could just use it. After all, its
second argument is
1.0l / n
, which is a lossy representation of theNth root.
On the implementation side, we take advantage of the fact that this
function is only ever meant to be called at compile time, which lets us
prioritize accuracy over "speed" (because the runtime speed is always
essentially infinite). We do a binary search between 1 and
x
afterchecking that
x > 1
(andn > 1
). When we end up with twoneighboring floating point values, we pick whichever gives the closest
result when we run it through
checked_int_pow
.This algorithm is guaranteed to produce the best representable result
for a "pure root". For other rational powers, it's technically possible
that we could have some lossiness in the
checked_int_pow
computation,if we enter a floating point realm that is high enough such that not all
integer values can be represented. That said, I tried a bunch of test
cases to compare it to
powl
, and it's passed all the ones that I wasable to come up with.
Fixes #116.