From f6696e8f68906432d6013fc880e04f9338d8aa1b Mon Sep 17 00:00:00 2001 From: Chip Hogg Date: Wed, 24 Jul 2024 19:48:05 -0400 Subject: [PATCH] Fix formatting --- .../include/mp-units/framework/magnitude.h | 190 +++++++++--------- test/static/quantity_test.cpp | 11 +- 2 files changed, 99 insertions(+), 102 deletions(-) diff --git a/src/core/include/mp-units/framework/magnitude.h b/src/core/include/mp-units/framework/magnitude.h index b94404db7..b95fa03af 100644 --- a/src/core/include/mp-units/framework/magnitude.h +++ b/src/core/include/mp-units/framework/magnitude.h @@ -307,118 +307,118 @@ template return checked_square(int_power(base, exp / 2)); } -template -[[nodiscard]] consteval std::optional checked_int_pow(T base, std::uintmax_t exp) { - T result = T{1}; - while (exp > 0u) { - if (exp % 2u == 1u) { - if (base > std::numeric_limits::max() / result) { - return std::nullopt; - } - result *= base; - } +template +[[nodiscard]] consteval std::optional checked_int_pow(T base, std::uintmax_t exp) +{ + T result = T{1}; + while (exp > 0u) { + if (exp % 2u == 1u) { + if (base > std::numeric_limits::max() / result) { + return std::nullopt; + } + result *= base; + } - exp /= 2u; + exp /= 2u; - if (base > std::numeric_limits::max() / base) { - return (exp == 0u) - ? std::make_optional(result) - : std::nullopt; - } - base *= base; + if (base > std::numeric_limits::max() / base) { + return (exp == 0u) ? std::make_optional(result) : std::nullopt; } - return result; + base *= base; + } + return result; } -template -[[nodiscard]] consteval std::optional root(T x, std::uintmax_t n) { - // The "zeroth root" would be mathematically undefined. - if (n == 0) { - return std::nullopt; - } +template +[[nodiscard]] consteval std::optional root(T x, std::uintmax_t n) +{ + // The "zeroth root" would be mathematically undefined. + if (n == 0) { + return std::nullopt; + } - // The "first root" is trivial. - if (n == 1) { - return x; - } + // The "first root" is trivial. + if (n == 1) { + return x; + } - // We only support nontrivial roots of floating point types. - if (!std::is_floating_point::value) { - return std::nullopt; - } + // We only support nontrivial roots of floating point types. + if (!std::is_floating_point::value) { + return std::nullopt; + } - // Handle negative numbers: only odd roots are allowed. - if (x < 0) { - if (n % 2 == 0) { - return std::nullopt; - } else { - const auto negative_result = root(-x, n); - if (!negative_result.has_value()) { - return std::nullopt; - } - return static_cast(-negative_result.value()); - } + // Handle negative numbers: only odd roots are allowed. + if (x < 0) { + if (n % 2 == 0) { + return std::nullopt; + } else { + const auto negative_result = root(-x, n); + if (!negative_result.has_value()) { + return std::nullopt; + } + return static_cast(-negative_result.value()); } + } - // Handle special cases of zero and one. - if (x == 0 || x == 1) { - return x; - } + // Handle special cases of zero and one. + if (x == 0 || x == 1) { + return x; + } - // Handle numbers bewtween 0 and 1. - if (x < 1) { - const auto inverse_result = root(T{1} / x, n); - if (!inverse_result.has_value()) { - return std::nullopt; - } - return static_cast(T{1} / inverse_result.value()); + // Handle numbers bewtween 0 and 1. + if (x < 1) { + const auto inverse_result = root(T{1} / x, n); + if (!inverse_result.has_value()) { + return std::nullopt; } + return static_cast(T{1} / inverse_result.value()); + } - // - // At this point, error conditions are finished, and we can proceed with the "core" algorithm. - // + // + // At this point, error conditions are finished, and we can proceed with the "core" algorithm. + // - // Always use `long double` for intermediate computations. We don't ever expect people to be - // calling this at runtime, so we want maximum accuracy. - long double lo = 1.0; - long double hi = static_cast(x); + // Always use `long double` for intermediate computations. We don't ever expect people to be + // calling this at runtime, so we want maximum accuracy. + long double lo = 1.0; + long double hi = static_cast(x); - // Do a binary search to find the closest value such that `checked_int_pow` recovers the input. - // - // Because we know `n > 1`, and `x > 1`, and x^n is monotonically increasing, we know that - // `checked_int_pow(lo, n) < x < checked_int_pow(hi, n)`. We will preserve this as an - // invariant. - while (lo < hi) { - long double mid = lo + (hi - lo) / 2; + // Do a binary search to find the closest value such that `checked_int_pow` recovers the input. + // + // Because we know `n > 1`, and `x > 1`, and x^n is monotonically increasing, we know that + // `checked_int_pow(lo, n) < x < checked_int_pow(hi, n)`. We will preserve this as an + // invariant. + while (lo < hi) { + long double mid = lo + (hi - lo) / 2; - auto result = checked_int_pow(mid, n); + auto result = checked_int_pow(mid, n); - if (!result.has_value()) { - return std::nullopt; - } + if (!result.has_value()) { + return std::nullopt; + } - // Early return if we get lucky with an exact answer. - if (result.value() == x) { - return static_cast(mid); - } + // Early return if we get lucky with an exact answer. + if (result.value() == x) { + return static_cast(mid); + } - // Check for stagnation. - if (mid == lo || mid == hi) { - break; - } + // Check for stagnation. + if (mid == lo || mid == hi) { + break; + } - // Preserve the invariant that `checked_int_pow(lo, n) < x < checked_int_pow(hi, n)`. - if (result.value() < x) { - lo = mid; - } else { - hi = mid; - } + // Preserve the invariant that `checked_int_pow(lo, n) < x < checked_int_pow(hi, n)`. + if (result.value() < x) { + lo = mid; + } else { + hi = mid; } + } - // Pick whichever one gets closer to the target. - const auto lo_diff = x - checked_int_pow(lo, n).value(); - const auto hi_diff = checked_int_pow(hi, n).value() - x; - return static_cast(lo_diff < hi_diff ? lo : hi); + // Pick whichever one gets closer to the target. + const auto lo_diff = x - checked_int_pow(lo, n).value(); + const auto hi_diff = checked_int_pow(hi, n).value() - x; + return static_cast(lo_diff < hi_diff ? lo : hi); } template @@ -444,13 +444,11 @@ template const auto final_result = (exp.den > 1) ? root(pow_result.value(), static_cast(exp.den)) : pow_result; if (final_result.has_value()) { return final_result.value(); + } else { + std::abort(); // Root computation failed. } - else { - std::abort(); // Root computation failed. - } - } - else { - std::abort(); // Power computation failed. + } else { + std::abort(); // Power computation failed. } } diff --git a/test/static/quantity_test.cpp b/test/static/quantity_test.cpp index 744735df6..6fad23d9a 100644 --- a/test/static/quantity_test.cpp +++ b/test/static/quantity_test.cpp @@ -53,8 +53,9 @@ using namespace mp_units::si::unit_symbols; // quantity class invariants ////////////////////////////// -template -constexpr bool within_4_ulps(T a, T b) { +template +constexpr bool within_4_ulps(T a, T b) +{ static_assert(std::is_floating_point_v); auto walk_ulps = [](T x, int n) { while (n > 0) { @@ -223,10 +224,8 @@ static_assert(within_4_ulps(sqrt((1.0 * m) * (1.0 * km)).numerical_value_in(m), // Reproducing issue #494 exactly: constexpr auto val_issue_494 = 8.0 * si::si2019::boltzmann_constant * 1000.0 * K / (std::numbers::pi * 10 * Da); -static_assert( - within_4_ulps( - sqrt(val_issue_494).numerical_value_in(m / s), - sqrt(val_issue_494.numerical_value_in(m * m / s / s)))); +static_assert(within_4_ulps(sqrt(val_issue_494).numerical_value_in(m / s), + sqrt(val_issue_494.numerical_value_in(m* m / s / s)))); ///////////////////////