5#ifndef INCLUDE_GUARD_ARENE_BASE_STDLIB_INCLUDE_STDLIB_DETAIL_RATIO_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_STDLIB_INCLUDE_STDLIB_DETAIL_RATIO_HPP_
26template <
intmax_t N,
intmax_t D = 1>
28 static_assert(D != 0,
"Denominator of a ratio must not be 0");
37 static constexpr intmax_t d_sign{D > 0 ? 1 : -1};
60template <
intmax_t N,
intmax_t D>
66template <
class R1,
class R2>
68 static_assert(
is_ratio<R1>::value,
"Parameters to ratio arithmetic operations must be specializations of std::ratio");
69 static_assert(
is_ratio<R2>::value,
"Parameters to ratio arithmetic operations must be specializations of std::ratio");
79 using type =
ratio<(R1::num / gcd_12) * (R2::num / gcd_21), (R1::den / gcd_21) * (R2::den / gcd_12)>;
85template <
class R1,
class R2>
87 static_assert(
is_ratio<R1>::value,
"Parameters to ratio arithmetic operations must be specializations of std::ratio");
88 static_assert(
is_ratio<R2>::value,
"Parameters to ratio arithmetic operations must be specializations of std::ratio");
100 ratio<(R1::num / gcd_num) * (R2::den / gcd_den) + (R2::num / gcd_num) * (R1::den / gcd_den), R1::den / gcd_den>;
110template <
class R1,
class R2>
112 static_assert(
is_ratio<R1>::value,
"Parameters to ratio comparison operations must be specializations of std::ratio");
113 static_assert(
is_ratio<R2>::value,
"Parameters to ratio comparison operations must be specializations of std::ratio");
135 return (left.num == 0) || (right.num == 0) ||
145 bool flip_direction{
false};
149 if (right.num >= 0) {
153 flip_direction = !flip_direction;
154 left.num = -left.num;
155 right.num = -right.num;
162 if ((left.num > left.den) != (right.num > right.den)) {
163 return right.num > right.den;
169 flip_direction = !flip_direction;
174 intmax_t const left_integ{left.num / left.den};
175 intmax_t const right_integ{right.num / right.den};
176 if (left_integ != right_integ) {
177 left = {left_integ, 1};
178 right = {right_integ, 1};
183 left.num %= left.den;
184 right.num %= right.den;
187 return flip_direction ? right.num * left.den < right.den * left.num : left.num * right.den < left.den * right.num;
195template <
class R1,
class R2>
197 static_assert(
is_ratio<R1>::value,
"Parameters to ratio comparison operations must be specializations of std::ratio");
198 static_assert(
is_ratio<R2>::value,
"Parameters to ratio comparison operations must be specializations of std::ratio");
212template <
class R1,
class R2>
222template <
class R1,
class R2>
232template <
class R1,
class R2>
240template <
class R1,
class R2>
246template <
class R1,
class R2>
252template <
class R1,
class R2>
258template <
class R1,
class R2>
264template <
class R1,
class R2>
270template <
class R1,
class R2>
276template <
class R1,
class R2>
A helper class to represent a compile-time constant with the specified type and value....
Definition integral_constant.hpp:23
integral_constant type
Definition integral_constant.hpp:27
static constexpr value_type value
Definition integral_constant.hpp:34
static constexpr auto min() noexcept -> T
The minimum finite value; the minimum positive normalized value for floating point types.
Definition numeric_limits.hpp:235
static constexpr auto max() noexcept -> T
The maximum finite value.
Definition numeric_limits.hpp:230
A type trait to check if a type is a specialization of std::ratio as required in [ratio....
Definition ratio.hpp:55
A helper for addition to enforce the type requirement in [ratio.general] and improve readability.
Definition ratio.hpp:86
typename ratio_multiply_impl< left, right >::type type
The result of the addition.
Definition ratio.hpp:104
A helper for equality comparison to enforce the type requirement in [ratio.general].
Definition ratio.hpp:111
integral_constant< bool, R1::num==R2::num &&R1::den==R2::den > type
The result of the comparison.
Definition ratio.hpp:117
A helper for less-than comparison to enforce the type requirement in [ratio.general].
Definition ratio.hpp:196
integral_constant< bool, fraction_less(fraction{R1::num, R1::den}, fraction{R2::num, R2::den})> type
The result of the comparison.
Definition ratio.hpp:202
A helper for multiplication to enforce the type requirement in [ratio.general] and improve readabilit...
Definition ratio.hpp:67
ratio<(R1::num/gcd_12) *(R2::num/gcd_21),(R1::den/gcd_21) *(R2::den/gcd_12)> type
The result of the multiplication.
Definition ratio.hpp:79
Inherits from true_type if the first ratio is equal to the second, false_type if not.
Definition ratio.hpp:247
Inherits from true_type if the first ratio is greater than or equal to the second,...
Definition ratio.hpp:277
Inherits from true_type if the first ratio is greater than the second, false_type if not.
Definition ratio.hpp:271
Inherits from true_type if the first ratio is less than or equal to the second, false_type if not.
Definition ratio.hpp:265
Inherits from true_type if the first ratio is less than the second, false_type if not.
Definition ratio.hpp:259
Inherits from true_type if the first ratio is not equal to the second, false_type if they are equal.
Definition ratio.hpp:253
A compile-time ratio of two integers, automatically reduced to lowest terms.
Definition ratio.hpp:27
static constexpr intmax_t num
Definition ratio.hpp:43
ratio< num, den > type
Definition ratio.hpp:48
static constexpr intmax_t den
Definition ratio.hpp:45
static ARENE_MAYBE_UNUSED constexpr auto const & swap
A Customization Point Object implementation of the constexpr compatible std::swap from C++17.
Definition swap.hpp:130
constexpr auto abs(Number value) noexcept -> Number
Calculate the absolute value of a number.
Definition abs.hpp:46
constexpr auto gcd(Integral lhs, Integral rhs) noexcept -> Integral
Calculates the Greatest Common Divisor (GCD) of two Integral values.
Definition gcd.hpp:26
constexpr auto can_directly_compare(fraction const &left, fraction const &right) noexcept -> bool
Check if two fractions can be directly compared (by cross multiplying them) without overflowing.
Definition ratio.hpp:134
constexpr auto fraction_less(fraction left, fraction right) noexcept -> bool
Check if the left fraction is less than the right or not.
Definition ratio.hpp:144
ratio< 1, 1 '000 '000 '000 '000 > pico
The SI prefix for 10^(-12), expressed as a ratio.
Definition ratio.hpp:285
ratio< 1, 1 '000 '000 '000 > nano
The SI prefix for 10^(-9), expressed as a ratio.
Definition ratio.hpp:287
::intmax_t intmax_t
A maximum-width signed integer type.
Definition cstdint.hpp:75
bool_constant< false > false_type
An alias for bool_constant<false>
Definition integral_constant.hpp:79
ratio< 1, 100 > centi
The SI prefix for 10^(-2), expressed as a ratio.
Definition ratio.hpp:293
ratio_add< R1, ratio<-R2::num, R2::den > > ratio_subtract
Defines the ratio yielded by subtracting the two given ratios.
Definition ratio.hpp:241
bool_constant< true > true_type
An alias for bool_constant<true>
Definition integral_constant.hpp:76
ratio< 1, 1 '000 > milli
The SI prefix for 10^(-3), expressed as a ratio.
Definition ratio.hpp:291
ratio< 1 '000 '000 '000 '000, 1 > tera
The SI prefix for 10^12, expressed as a ratio.
Definition ratio.hpp:307
typename ratio_detail::ratio_multiply_impl< R1, R2 >::type ratio_multiply
Defines the ratio yielded by multiplying the two given ratios.
Definition ratio.hpp:213
ratio< 1 '000 '000 '000 '000 '000, 1 > peta
The SI prefix for 10^15, expressed as a ratio.
Definition ratio.hpp:309
ratio< 1, 10 > deci
The SI prefix for 10^(-1), expressed as a ratio.
Definition ratio.hpp:295
ratio< 1 '000, 1 > kilo
The SI prefix for 10^3, expressed as a ratio.
Definition ratio.hpp:301
ratio< 1 '000 '000 '000, 1 > giga
The SI prefix for 10^9, expressed as a ratio.
Definition ratio.hpp:305
ratio< 1, 1 '000 '000 '000 '000 '000 > femto
The SI prefix for 10^(-15), expressed as a ratio.
Definition ratio.hpp:283
ratio< 1, 1 '000 '000 > micro
The SI prefix for 10^(-6), expressed as a ratio.
Definition ratio.hpp:289
ratio< 1, 1 '000 '000 '000 '000 '000 '000 > atto
The SI prefix for 10^(-18), expressed as a ratio.
Definition ratio.hpp:281
ratio< 1 '000 '000 '000 '000 '000 '000, 1 > exa
The SI prefix for 10^18, expressed as a ratio.
Definition ratio.hpp:311
ratio< 10, 1 > deca
The SI prefix for 10^1, expressed as a ratio.
Definition ratio.hpp:297
typename ratio_detail::ratio_add_impl< R1, R2 >::type ratio_add
Defines the ratio yielded by adding the two given ratios.
Definition ratio.hpp:233
ratio< 1 '000 '000, 1 > mega
The SI prefix for 10^6, expressed as a ratio.
Definition ratio.hpp:303
ratio_multiply< R1, ratio< R2::den, R2::num > > ratio_divide
Defines the ratio yielded by dividing the two given ratios.
Definition ratio.hpp:223
ratio< 100, 1 > hecto
The SI prefix for 10^2, expressed as a ratio.
Definition ratio.hpp:299
A type used to represent a fraction for non-template computations.
Definition ratio.hpp:121
intmax_t num
The numerator of the fraction.
Definition ratio.hpp:123
intmax_t den
The denominator of the fraction.
Definition ratio.hpp:125