Merge branch 'master' of github.com:mpusz/units

This commit is contained in:
Mateusz Pusz committed 2024-09-17 17:14:26 -06:00
commit 0c9e62a288
21 files changed
+414 -83

No files matched your search

@@ -380,8 +380,9 @@ template<typename T>
// 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 xld = static_cast<long double>(x);
long double lo = 1.0;
long double hi = static_cast<long double>(x);
long double hi = xld;
// Do a binary search to find the closest value such that `checked_int_pow` recovers the input.
//
@@ -398,7 +399,7 @@ template<typename T>
}
// Early return if we get lucky with an exact answer.
if (result.value() == x) {
if (result.value() == xld) {
return static_cast<T>(mid);
}
@@ -408,7 +409,7 @@ template<typename T>
}
// Preserve the invariant that `checked_int_pow(lo, n) < x < checked_int_pow(hi, n)`.
if (result.value() < x) {
if (result.value() < xld) {
lo = mid;
} else {
hi = mid;
@@ -416,8 +417,8 @@ template<typename T>
}
// 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;
const auto lo_diff = xld - checked_int_pow(lo, n).value();
const auto hi_diff = checked_int_pow(hi, n).value() - xld;
return static_cast<T>(lo_diff < hi_diff ? lo : hi);
}
@@ -1,4 +1,3 @@
// The MIT License (MIT)
//
// Copyright (c) 2018 Mateusz Pusz
@@ -88,6 +88,14 @@ template<Unit auto ToU, Representation ToRep, typename FwdQ, Quantity Q = std::r
return detail::sudo_cast<quantity<detail::make_reference(Q::quantity_spec, ToU), ToRep>>(std::forward<FwdQ>(q));
}
template<Representation ToRep, Unit auto ToU, typename FwdQ, Quantity Q = std::remove_cvref_t<FwdQ>>
requires(convertible(Q::reference, ToU)) && RepresentationOf<ToRep, Q::quantity_spec.character> &&
std::constructible_from<ToRep, typename Q::rep>
[[nodiscard]] constexpr Quantity auto value_cast(FwdQ&& q)
{
return value_cast<ToU, ToRep>(std::forward<FwdQ>(q));
}
/**
* @brief Explicit cast of a quantity's representation
@@ -168,6 +176,14 @@ template<Unit auto ToU, Representation ToRep, typename FwdQP, QuantityPoint QP =
QP::point_origin};
}
template<Representation ToRep, Unit auto ToU, typename FwdQP, QuantityPoint QP = std::remove_cvref_t<FwdQP>>
requires(convertible(QP::reference, ToU)) && RepresentationOf<ToRep, QP::quantity_spec.character> &&
std::constructible_from<ToRep, typename QP::rep>
[[nodiscard]] constexpr QuantityPoint auto value_cast(FwdQP&& qp)
{
return value_cast<ToU, ToRep>(std::forward<FwdQP>(qp));
}
/**
* @brief Explicit cast of a quantity point's representation
*