Merge branch 'master' into downcast_mode_off
This commit is contained in:
175 files changed
+6360
-4102
No files matched your search
@@ -24,7 +24,6 @@
|
||||
|
||||
#include <units/bits/algorithm.h>
|
||||
#include <array>
|
||||
#include <cassert>
|
||||
#include <cstddef>
|
||||
#include <numeric>
|
||||
#include <optional>
|
||||
|
||||
@@ -48,12 +48,16 @@ using angle = quantity<dim_angle<>, U, Rep>;
|
||||
inline namespace literals {
|
||||
|
||||
// rad
|
||||
constexpr auto operator"" _q_rad(unsigned long long l) { gsl_ExpectsAudit(std::in_range<std::int64_t>(l)); return angle<radian, std::int64_t>(static_cast<std::int64_t>(l)); }
|
||||
constexpr auto operator"" _q_rad(unsigned long long l)
|
||||
{
|
||||
gsl_ExpectsAudit(std::in_range<std::int64_t>(l));
|
||||
return angle<radian, std::int64_t>(static_cast<std::int64_t>(l));
|
||||
}
|
||||
constexpr auto operator"" _q_rad(long double l) { return angle<radian, long double>(l); }
|
||||
|
||||
} // namespace literals
|
||||
|
||||
#endif // UNITS_NO_LITERALS
|
||||
#endif // UNITS_NO_LITERALS
|
||||
|
||||
#ifndef UNITS_NO_REFERENCES
|
||||
|
||||
@@ -69,6 +73,6 @@ using namespace angle_references;
|
||||
|
||||
} // namespace references
|
||||
|
||||
#endif // UNITS_NO_REFERENCES
|
||||
#endif // UNITS_NO_REFERENCES
|
||||
|
||||
} // namespace units
|
||||
@@ -392,7 +392,7 @@ template<typename T, BasePower auto... BPs>
|
||||
constexpr T get_value(const magnitude<BPs...>&)
|
||||
{
|
||||
// Force the expression to be evaluated in a constexpr context, to catch, e.g., overflow.
|
||||
constexpr auto result = detail::checked_static_cast<T>((detail::compute_base_power<T>(BPs) * ...));
|
||||
constexpr auto result = detail::checked_static_cast<T>((detail::compute_base_power<T>(BPs) * ... * T{1}));
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -480,6 +480,51 @@ constexpr auto operator*(magnitude<H1, T1...>, magnitude<H2, T2...>)
|
||||
|
||||
constexpr auto operator/(Magnitude auto l, Magnitude auto r) { return l * pow<-1>(r); }
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Magnitude numerator and denominator implementation.
|
||||
|
||||
namespace detail {
|
||||
|
||||
// The largest integer which can be extracted from any magnitude with only a single basis vector.
|
||||
template<BasePower auto BP>
|
||||
constexpr auto integer_part(magnitude<BP>)
|
||||
{
|
||||
constexpr auto power_num = numerator(BP.power);
|
||||
constexpr auto power_den = denominator(BP.power);
|
||||
|
||||
if constexpr (std::is_integral_v<decltype(BP.get_base())> && (power_num >= power_den)) {
|
||||
constexpr auto largest_integer_power = [=](BasePower auto bp) {
|
||||
bp.power = (power_num / power_den); // Note: integer division intended.
|
||||
return bp;
|
||||
}(BP); // Note: lambda is immediately invoked.
|
||||
|
||||
return magnitude<largest_integer_power>{};
|
||||
} else {
|
||||
return magnitude<>{};
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace detail
|
||||
|
||||
template<BasePower auto... BPs>
|
||||
constexpr auto numerator(magnitude<BPs...>)
|
||||
{
|
||||
return (detail::integer_part(magnitude<BPs>{}) * ... * magnitude<>{});
|
||||
}
|
||||
|
||||
constexpr auto denominator(Magnitude auto m) { return numerator(pow<-1>(m)); }
|
||||
|
||||
// Implementation of conversion to ratio goes here, because it needs `numerator()` and `denominator()`.
|
||||
constexpr ratio as_ratio(Magnitude auto m)
|
||||
requires(is_rational(decltype(m){}))
|
||||
{
|
||||
return ratio{
|
||||
get_value<std::intmax_t>(numerator(m)),
|
||||
get_value<std::intmax_t>(denominator(m)),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// `as_magnitude()` implementation.
|
||||
|
||||
|
||||
@@ -87,6 +87,24 @@ struct ratio {
|
||||
}
|
||||
|
||||
[[nodiscard]] friend constexpr ratio operator/(const ratio& lhs, const ratio& rhs) { return lhs * inverse(rhs); }
|
||||
|
||||
[[nodiscard]] friend constexpr std::intmax_t numerator(const ratio& r)
|
||||
{
|
||||
std::intmax_t true_num = r.num;
|
||||
for (auto i = r.exp; i > 0; --i) {
|
||||
true_num *= 10;
|
||||
}
|
||||
return true_num;
|
||||
}
|
||||
|
||||
[[nodiscard]] friend constexpr std::intmax_t denominator(const ratio& r)
|
||||
{
|
||||
std::intmax_t true_den = r.den;
|
||||
for (auto i = r.exp; i < 0; ++i) {
|
||||
true_den *= 10;
|
||||
}
|
||||
return true_den;
|
||||
}
|
||||
};
|
||||
|
||||
[[nodiscard]] constexpr ratio inverse(const ratio& r) { return ratio(r.den, r.num, -r.exp); }
|
||||
|
||||
@@ -78,16 +78,16 @@ struct basic_symbol_text {
|
||||
{
|
||||
detail::validate_ascii_string<N>(std.data_);
|
||||
}
|
||||
constexpr basic_symbol_text(const StandardCharT (&std)[N + 1], const char (&a)[M + 1]) noexcept :
|
||||
standard_(std), ascii_(a)
|
||||
constexpr basic_symbol_text(const StandardCharT (&std)[N + 1], const char (&ascii)[M + 1]) noexcept :
|
||||
standard_(std), ascii_(ascii)
|
||||
{
|
||||
detail::validate_ascii_string<M>(a);
|
||||
detail::validate_ascii_string<M>(ascii);
|
||||
}
|
||||
constexpr basic_symbol_text(const basic_fixed_string<StandardCharT, N>& std,
|
||||
const basic_fixed_string<char, M>& a) noexcept :
|
||||
standard_(std), ascii_(a)
|
||||
const basic_fixed_string<char, M>& ascii) noexcept :
|
||||
standard_(std), ascii_(ascii)
|
||||
{
|
||||
detail::validate_ascii_string<M>(a.data_);
|
||||
detail::validate_ascii_string<M>(ascii.data_);
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr auto& standard() { return standard_; }
|
||||
|
||||
Reference in new issue
Block a user