mirror of
https://github.com/mpusz/mp-units.git
synced 2025-08-04 12:54:25 +02:00
clang-format + TODOs added to ratio
This commit is contained in:
@@ -23,177 +23,186 @@
|
||||
#pragma once
|
||||
|
||||
#include <units/bits/external/hacks.h>
|
||||
#include <type_traits>
|
||||
#include <numeric>
|
||||
#include <cstdint>
|
||||
#include <numeric>
|
||||
#include <type_traits>
|
||||
|
||||
namespace units {
|
||||
|
||||
namespace detail {
|
||||
namespace detail {
|
||||
|
||||
template<typename T>
|
||||
[[nodiscard]] constexpr T abs(T v) noexcept { return v < 0 ? -v : v; }
|
||||
template<typename T>
|
||||
[[nodiscard]] constexpr T abs(T v) noexcept
|
||||
{
|
||||
return v < 0 ? -v : v;
|
||||
}
|
||||
|
||||
}
|
||||
} // namespace detail
|
||||
|
||||
template<std::intmax_t Num, std::intmax_t Den = 1>
|
||||
requires (Den != 0)
|
||||
struct ratio {
|
||||
static_assert(-INTMAX_MAX <= Num, "numerator too negative");
|
||||
static_assert(-INTMAX_MAX <= Den, "denominator too negative");
|
||||
template<std::intmax_t Num, std::intmax_t Den = 1>
|
||||
requires(Den != 0)
|
||||
struct ratio {
|
||||
static_assert(-INTMAX_MAX <= Num, "numerator too negative");
|
||||
static_assert(-INTMAX_MAX <= Den, "denominator too negative");
|
||||
|
||||
static constexpr std::intmax_t num = Num * (Den < 0 ? -1 : 1) / std::gcd(Num, Den);
|
||||
static constexpr std::intmax_t den = detail::abs(Den) / std::gcd(Num, Den);
|
||||
static constexpr std::intmax_t num = Num * (Den < 0 ? -1 : 1) / std::gcd(Num, Den);
|
||||
static constexpr std::intmax_t den = detail::abs(Den) / std::gcd(Num, Den);
|
||||
|
||||
using type = ratio<num, den>;
|
||||
};
|
||||
using type = ratio<num, den>;
|
||||
};
|
||||
|
||||
// is_ratio
|
||||
// is_ratio
|
||||
|
||||
namespace detail {
|
||||
namespace detail {
|
||||
|
||||
template<typename T>
|
||||
inline constexpr bool is_ratio = false;
|
||||
template<typename T>
|
||||
inline constexpr bool is_ratio = false;
|
||||
|
||||
template<intmax_t Num, intmax_t Den>
|
||||
inline constexpr bool is_ratio<ratio<Num, Den>> = true;
|
||||
template<intmax_t Num, intmax_t Den>
|
||||
inline constexpr bool is_ratio<ratio<Num, Den>> = true;
|
||||
|
||||
} // namespace detail
|
||||
} // namespace detail
|
||||
|
||||
template<typename T>
|
||||
concept Ratio = detail::is_ratio<T>;
|
||||
|
||||
// ratio_multiply
|
||||
|
||||
namespace detail {
|
||||
|
||||
static constexpr std::intmax_t safe_multiply(std::intmax_t lhs, std::intmax_t rhs)
|
||||
{
|
||||
constexpr std::uintmax_t c = std::uintmax_t(1) << (sizeof(std::intmax_t) * 4);
|
||||
|
||||
const std::uintmax_t a0 = detail::abs(lhs) % c;
|
||||
const std::uintmax_t a1 = detail::abs(lhs) / c;
|
||||
const std::uintmax_t b0 = detail::abs(rhs) % c;
|
||||
const std::uintmax_t b1 = detail::abs(rhs) / c;
|
||||
|
||||
Expects(a1 == 0 || b1 == 0); // overflow in multiplication
|
||||
Expects(a0 * b1 + b0 * a1 < (c >> 1)); // overflow in multiplication
|
||||
Expects(b0 * a0 <= INTMAX_MAX); // overflow in multiplication
|
||||
Expects((a0 * b1 + b0 * a1) * c <= INTMAX_MAX - b0 * a0); // overflow in multiplication
|
||||
|
||||
return lhs * rhs;
|
||||
}
|
||||
|
||||
template<typename R1, typename R2>
|
||||
struct ratio_multiply_impl {
|
||||
private:
|
||||
static constexpr std::intmax_t gcd1 = std::gcd(R1::num, R2::den);
|
||||
static constexpr std::intmax_t gcd2 = std::gcd(R2::num, R1::den);
|
||||
|
||||
public:
|
||||
using type = ratio<safe_multiply(R1::num / gcd1, R2::num / gcd2), safe_multiply(R1::den / gcd2, R2::den / gcd1)>;
|
||||
static constexpr std::intmax_t num = type::num;
|
||||
static constexpr std::intmax_t den = type::den;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
template<Ratio R1, Ratio R2>
|
||||
using ratio_multiply = detail::ratio_multiply_impl<R1, R2>::type;
|
||||
|
||||
// ratio_divide
|
||||
|
||||
namespace detail {
|
||||
|
||||
template<typename R1, typename R2>
|
||||
struct ratio_divide_impl {
|
||||
static_assert(R2::num != 0, "division by 0");
|
||||
using type = ratio_multiply<R1, ratio<R2::den, R2::num>>;
|
||||
static constexpr std::intmax_t num = type::num;
|
||||
static constexpr std::intmax_t den = type::den;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
template<Ratio R1, Ratio R2>
|
||||
using ratio_divide = detail::ratio_divide_impl<R1, R2>::type;
|
||||
|
||||
// ratio_pow
|
||||
|
||||
namespace detail {
|
||||
|
||||
template<typename R, std::size_t N>
|
||||
struct ratio_pow_impl {
|
||||
using type = ratio_multiply<typename ratio_pow_impl<R, N - 1>::type, R>;
|
||||
};
|
||||
|
||||
template<typename R>
|
||||
struct ratio_pow_impl<R, 1> {
|
||||
using type = R;
|
||||
};
|
||||
|
||||
template<typename R>
|
||||
struct ratio_pow_impl<R, 0> {
|
||||
using type = ratio<1>;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
template<Ratio R, std::size_t N>
|
||||
using ratio_pow = detail::ratio_pow_impl<R, N>::type;
|
||||
|
||||
// ratio_sqrt
|
||||
|
||||
namespace detail {
|
||||
|
||||
constexpr std::intmax_t sqrt_impl(std::intmax_t v, std::intmax_t l, std::intmax_t r)
|
||||
{
|
||||
if(l == r)
|
||||
return r;
|
||||
|
||||
const auto mid = (r + l) / 2;
|
||||
if(mid * mid >= v)
|
||||
return sqrt_impl(v, l, mid);
|
||||
else
|
||||
return sqrt_impl(v, mid + 1, r);
|
||||
}
|
||||
|
||||
static constexpr std::intmax_t sqrt_impl(std::intmax_t v)
|
||||
{
|
||||
return sqrt_impl(v, 1, v);
|
||||
}
|
||||
|
||||
template<typename R>
|
||||
struct ratio_sqrt_impl {
|
||||
using type = ratio<detail::sqrt_impl(R::num), detail::sqrt_impl(R::den)>;
|
||||
};
|
||||
|
||||
template<std::intmax_t Den>
|
||||
struct ratio_sqrt_impl<ratio<0, Den>> {
|
||||
using type = ratio<0>;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
template<Ratio R>
|
||||
using ratio_sqrt = detail::ratio_sqrt_impl<R>::type;
|
||||
template<typename T>
|
||||
concept Ratio = detail::is_ratio<T>;
|
||||
|
||||
|
||||
// common_ratio
|
||||
// ratio_add
|
||||
// TODO implement ratio_add
|
||||
// template<Ratio R1, Ratio R2>
|
||||
// using ratio_add = detail::ratio_add_impl<R1, R2>::type;
|
||||
|
||||
namespace detail {
|
||||
// ratio_subtract
|
||||
// TODO implement ratio_subtract
|
||||
// template<Ratio R1, Ratio R2>
|
||||
// using ratio_subtract = detail::ratio_subtract_impl<R1, R2>::type;
|
||||
|
||||
// TODO: simplified
|
||||
template<typename R1, typename R2>
|
||||
struct common_ratio_impl {
|
||||
static constexpr std::intmax_t gcd_num = std::gcd(R1::num, R2::num);
|
||||
static constexpr std::intmax_t gcd_den = std::gcd(R1::den, R2::den);
|
||||
using type = ratio<gcd_num, (R1::den / gcd_den) * R2::den>;
|
||||
};
|
||||
// ratio_multiply
|
||||
|
||||
}
|
||||
namespace detail {
|
||||
|
||||
template<Ratio R1, Ratio R2>
|
||||
using common_ratio = detail::common_ratio_impl<R1, R2>::type;
|
||||
static constexpr std::intmax_t safe_multiply(std::intmax_t lhs, std::intmax_t rhs)
|
||||
{
|
||||
constexpr std::uintmax_t c = std::uintmax_t(1) << (sizeof(std::intmax_t) * 4);
|
||||
|
||||
const std::uintmax_t a0 = detail::abs(lhs) % c;
|
||||
const std::uintmax_t a1 = detail::abs(lhs) / c;
|
||||
const std::uintmax_t b0 = detail::abs(rhs) % c;
|
||||
const std::uintmax_t b1 = detail::abs(rhs) / c;
|
||||
|
||||
Expects(a1 == 0 || b1 == 0); // overflow in multiplication
|
||||
Expects(a0 * b1 + b0 * a1 < (c >> 1)); // overflow in multiplication
|
||||
Expects(b0 * a0 <= INTMAX_MAX); // overflow in multiplication
|
||||
Expects((a0 * b1 + b0 * a1) * c <= INTMAX_MAX - b0 * a0); // overflow in multiplication
|
||||
|
||||
return lhs * rhs;
|
||||
}
|
||||
|
||||
template<typename R1, typename R2>
|
||||
struct ratio_multiply_impl {
|
||||
private:
|
||||
static constexpr std::intmax_t gcd1 = std::gcd(R1::num, R2::den);
|
||||
static constexpr std::intmax_t gcd2 = std::gcd(R2::num, R1::den);
|
||||
|
||||
public:
|
||||
using type = ratio<safe_multiply(R1::num / gcd1, R2::num / gcd2), safe_multiply(R1::den / gcd2, R2::den / gcd1)>;
|
||||
static constexpr std::intmax_t num = type::num;
|
||||
static constexpr std::intmax_t den = type::den;
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
|
||||
template<Ratio R1, Ratio R2>
|
||||
using ratio_multiply = detail::ratio_multiply_impl<R1, R2>::type;
|
||||
|
||||
// ratio_divide
|
||||
|
||||
namespace detail {
|
||||
|
||||
template<typename R1, typename R2>
|
||||
struct ratio_divide_impl {
|
||||
static_assert(R2::num != 0, "division by 0");
|
||||
using type = ratio_multiply<R1, ratio<R2::den, R2::num>>;
|
||||
static constexpr std::intmax_t num = type::num;
|
||||
static constexpr std::intmax_t den = type::den;
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
|
||||
template<Ratio R1, Ratio R2>
|
||||
using ratio_divide = detail::ratio_divide_impl<R1, R2>::type;
|
||||
|
||||
// ratio_pow
|
||||
|
||||
namespace detail {
|
||||
|
||||
template<typename R, std::size_t N>
|
||||
struct ratio_pow_impl {
|
||||
using type = ratio_multiply<typename ratio_pow_impl<R, N - 1>::type, R>;
|
||||
};
|
||||
|
||||
template<typename R>
|
||||
struct ratio_pow_impl<R, 1> {
|
||||
using type = R;
|
||||
};
|
||||
|
||||
template<typename R>
|
||||
struct ratio_pow_impl<R, 0> {
|
||||
using type = ratio<1>;
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
|
||||
template<Ratio R, std::size_t N>
|
||||
using ratio_pow = detail::ratio_pow_impl<R, N>::type;
|
||||
|
||||
// ratio_sqrt
|
||||
|
||||
namespace detail {
|
||||
|
||||
constexpr std::intmax_t sqrt_impl(std::intmax_t v, std::intmax_t l, std::intmax_t r)
|
||||
{
|
||||
if (l == r) return r;
|
||||
|
||||
const auto mid = (r + l) / 2;
|
||||
if (mid * mid >= v)
|
||||
return sqrt_impl(v, l, mid);
|
||||
else
|
||||
return sqrt_impl(v, mid + 1, r);
|
||||
}
|
||||
|
||||
static constexpr std::intmax_t sqrt_impl(std::intmax_t v) { return sqrt_impl(v, 1, v); }
|
||||
|
||||
template<typename R>
|
||||
struct ratio_sqrt_impl {
|
||||
using type = ratio<detail::sqrt_impl(R::num), detail::sqrt_impl(R::den)>;
|
||||
};
|
||||
|
||||
template<std::intmax_t Den>
|
||||
struct ratio_sqrt_impl<ratio<0, Den>> {
|
||||
using type = ratio<0>;
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
|
||||
template<Ratio R>
|
||||
using ratio_sqrt = detail::ratio_sqrt_impl<R>::type;
|
||||
|
||||
// common_ratio
|
||||
|
||||
namespace detail {
|
||||
|
||||
// TODO: simplified
|
||||
template<typename R1, typename R2>
|
||||
struct common_ratio_impl {
|
||||
static constexpr std::intmax_t gcd_num = std::gcd(R1::num, R2::num);
|
||||
static constexpr std::intmax_t gcd_den = std::gcd(R1::den, R2::den);
|
||||
using type = ratio<gcd_num, (R1::den / gcd_den) * R2::den>;
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
|
||||
template<Ratio R1, Ratio R2>
|
||||
using common_ratio = detail::common_ratio_impl<R1, R2>::type;
|
||||
|
||||
} // namespace units
|
||||
|
Reference in New Issue
Block a user