-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support computing values for all rational powers (#213)
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.
- Loading branch information
Showing
3 changed files
with
227 additions
and
18 deletions.
There are no files selected for viewing
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
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
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