Work around numbers with very large first factors
We introduce the `known_first_factor` variable template.
This commit is contained in:
1 parent
a99e5f9032
commit
166dd1e944
2 files changed
+48
-2
No files matched your search
@@ -28,6 +28,9 @@
|
||||
using namespace units;
|
||||
using namespace units::detail;
|
||||
|
||||
template<>
|
||||
inline constexpr std::optional<std::intmax_t> units::known_first_factor<9223372036854775783> = 9223372036854775783;
|
||||
|
||||
namespace {
|
||||
|
||||
// A set of non-standard bases for testing purposes.
|
||||
@@ -149,7 +152,17 @@ TEST_CASE("make_ratio performs prime factorization correctly")
|
||||
// all odd numbers up to sqrt(N), will exceed this limit for the following prime. Thus, for this test to pass, we
|
||||
// need to be using a more efficient algorithm. (We could increase the limit, but we don't want users to have to
|
||||
// mess with compiler flags just to compile the code.)
|
||||
as_magnitude<334524384739>();
|
||||
as_magnitude<334'524'384'739>();
|
||||
}
|
||||
|
||||
SECTION ("Can bypass computing primes by providing known_first_factor<N>") {
|
||||
// Sometimes, even wheel factorization isn't enough to handle the compilers' limits on constexpr steps and/or
|
||||
// iterations. To work around these cases, we can explicitly provide the correct answer directly to the compiler.
|
||||
//
|
||||
// In this case, we test that we can represent the largest prime that fits in a signed 64-bit int. The reason this
|
||||
// test can pass is that we have provided the answer, by specializing the `known_first_factor` variable template
|
||||
// above in this file.
|
||||
as_magnitude<9'223'372'036'854'775'783>();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user