5#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_UNITS_QUANTITY_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_UNITS_QUANTITY_HPP_
20#include "arene/base/stdlib_choice/common_type.hpp"
21#include "arene/base/stdlib_choice/cstdint.hpp"
22#include "arene/base/stdlib_choice/enable_if.hpp"
23#include "arene/base/stdlib_choice/is_integral.hpp"
24#include "arene/base/stdlib_choice/is_same.hpp"
25#include "arene/base/stdlib_choice/is_signed.hpp"
26#include "arene/base/stdlib_choice/is_unsigned.hpp"
27#include "arene/base/stdlib_choice/make_unsigned.hpp"
28#include "arene/base/stdlib_choice/move.hpp"
29#include "arene/base/stdlib_choice/numeric_limits.hpp"
30#include "arene/base/stdlib_choice/ratio.hpp"
39template <
typename QuantityType,
typename Rep =
double>
43namespace quantity_detail {
47extern constexpr bool is_quantity_v =
false;
53template <
typename QuantityType,
typename Rep>
54extern constexpr bool is_quantity_v<quantity<QuantityType, Rep>> =
true;
84template <
typename QuantityType1,
typename QuantityType2>
89template <
typename QuantityType>
99namespace quantity_detail {
105template <typename T, constraints<std::enable_if_t<std::is_signed<T>::value>> =
nullptr>
118template <typename T, constraints<std::enable_if_t<std::is_unsigned<T>::value>> =
nullptr>
119constexpr auto unsigned_abs(T value)
noexcept -> T {
131 typename ScaleFactor,
136 std::enable_if_t<((ScaleFactor::num < 0) != (ScaleFactor::den < 0))>> =
nullptr>
137constexpr auto scale_value_by(Rep value)
noexcept -> TargetRep {
140 unsigned_scaled_by_positive_factor,
141 "Scaling a value by a negative scale factor may yield a negative result that cannot be represented in "
144 return static_cast<TargetRep
>(value);
155 typename ScaleFactor,
159 std::enable_if_t<std::is_unsigned<TargetRep>::value>,
160 std::enable_if_t<std::is_integral<Rep>::value>,
164 std::enable_if_t<((ScaleFactor::num < 0) == (ScaleFactor::den < 0))>> =
nullptr>
165constexpr auto scale_value_by(Rep value)
noexcept -> TargetRep {
168 scaling_calculation_does_not_overflow,
169 "The scaling calculation will overflow internally; please use a floating point representation"
171 return static_cast<TargetRep
>(value);
182 typename ScaleFactor,
186 std::enable_if_t<std::is_unsigned<TargetRep>::value>,
187 std::enable_if_t<std::is_integral<Rep>::value>,
192 std::enable_if_t<((ScaleFactor::num < 0) == (ScaleFactor::den < 0))>> =
nullptr>
193constexpr auto scale_value_by(Rep value)
noexcept -> TargetRep {
194 if (value == Rep{}) {
195 return static_cast<TargetRep
>(value);
198 constexpr auto unsigned_den = quantity_detail::unsigned_abs(ScaleFactor::den);
199 constexpr auto unsigned_num = quantity_detail::unsigned_abs(ScaleFactor::num);
200 auto const initial_gcd =
gcd(initial_value, unsigned_den);
201 auto const den = unsigned_den / initial_gcd;
202 auto const smaller_value = initial_value / initial_gcd;
204 auto const initial_result = (smaller_value / den) * unsigned_num;
205 auto const remainder = smaller_value % den;
206 auto const final_result = initial_result + remainder * unsigned_num / den;
208 return static_cast<TargetRep
>(final_result);
218 typename ScaleFactor,
222 std::enable_if_t<std::is_integral<TargetRep>::value>,
223 std::enable_if_t<std::is_integral<Rep>::value>,
224 std::enable_if_t<std::is_signed<TargetRep>::value>,
225 std::enable_if_t<std::is_signed<Rep>::value>> =
nullptr>
226constexpr auto scale_value_by(Rep value)
noexcept -> TargetRep {
227 constexpr bool sign_flipped{(ScaleFactor::num < 0) != (ScaleFactor::den < 0)};
228 bool const value_is_negative{value < static_cast<Rep>(0)};
229 bool const result_is_negative{sign_flipped != value_is_negative};
230 constexpr auto unsigned_den = quantity_detail::unsigned_abs(ScaleFactor::den);
231 constexpr auto unsigned_num = quantity_detail::unsigned_abs(ScaleFactor::num);
233 quantity_detail::unsigned_abs(value)
235 if (result_is_negative) {
237 return static_cast<TargetRep
>(-
static_cast<std::int64_t>(unsigned_result));
242 return static_cast<TargetRep
>(unsigned_result);
252 typename ScaleFactor,
256constexpr auto scale_value_by(Rep value)
noexcept -> TargetRep {
257 using intermediate_type =
typename std::common_type<TargetRep, Rep>::type;
258 intermediate_type
const scaled_intermediate{
259 static_cast<intermediate_type
>(value) *
static_cast<intermediate_type
>(ScaleFactor::num) /
260 static_cast<intermediate_type
>(ScaleFactor::den)
262 return static_cast<TargetRep
>(scaled_intermediate);
272template <
typename SourceQuantityType,
typename TargetQuantityType,
typename = constra
ints<>>
273extern constexpr bool has_common_type_specified_v =
false;
283template <
typename SourceQuantityType,
typename TargetQuantityType>
284extern constexpr bool has_common_type_specified_v<
287 constraints<typename units_conversion_traits<SourceQuantityType, TargetQuantityType>::common_type>> =
true;
296template <
typename SourceQuantityType,
typename TargetQuantityType,
typename = constra
ints<>>
297extern constexpr bool has_scale_factor_specified_v =
false;
307template <
typename SourceQuantityType,
typename TargetQuantityType>
308extern constexpr bool has_scale_factor_specified_v<
311 constraints<typename units_conversion_traits<SourceQuantityType, TargetQuantityType>::scale_factor>> =
true;
322template <
typename SourceQuantityType,
typename TargetQuantityType>
323extern constexpr bool is_explicitly_convertible_v =
324 units_conversion_traits<SourceQuantityType, TargetQuantityType>::compatible ||
325 has_common_type_specified_v<SourceQuantityType, TargetQuantityType>;
336template <
typename SourceQuantityType,
typename TargetQuantityType>
337extern constexpr bool is_implicitly_convertible_v =
338 units_conversion_traits<SourceQuantityType, TargetQuantityType>::compatible;
344template <
typename ScaleFactor1,
typename ScaleFactor2>
352template <
typename SourceQuantityType,
typename TargetQuantityType,
typename = constra
ints<>>
353struct scale_factor_helper {
355 has_scale_factor_specified_v<SourceQuantityType, TargetQuantityType>,
356 "No scale factor specified for conversion between types"
359 using type =
typename units_conversion_traits<SourceQuantityType, TargetQuantityType>::scale_factor;
367template <
typename SourceQuantityType,
typename TargetQuantityType>
368using scale_factor =
typename scale_factor_helper<SourceQuantityType, TargetQuantityType>::type;
377template <
typename SourceQuantityType,
typename TargetQuantityType,
typename = constra
ints<>>
378extern constexpr bool common_type_is_source_or_target_v =
false;
387template <
typename SourceQuantityType,
typename TargetQuantityType>
388extern constexpr bool common_type_is_source_or_target_v<
393 typename units_conversion_traits<SourceQuantityType, TargetQuantityType>::common_type,
394 SourceQuantityType>::value ||
396 typename units_conversion_traits<SourceQuantityType, TargetQuantityType>::common_type,
397 TargetQuantityType>::value;
407template <
typename SourceQuantityType,
typename TargetQuantityType>
408struct scale_factor_helper<
412 std::
enable_if_t<!is_implicitly_convertible_v<SourceQuantityType, TargetQuantityType>>,
413 std::enable_if_t<has_common_type_specified_v<SourceQuantityType, TargetQuantityType>>,
414 std::enable_if_t<!common_type_is_source_or_target_v<SourceQuantityType, TargetQuantityType>>>> {
416 using common_type =
typename units_conversion_traits<SourceQuantityType, TargetQuantityType>::common_type;
418 using type = combine_scale_factors_t<
419 scale_factor<SourceQuantityType, common_type>,
420 scale_factor<common_type, TargetQuantityType>>;
431 typename SourceQuantityType,
433 typename TargetQuantityType,
436extern constexpr bool scaled_value_in_range_v =
false;
444template <
typename ScaleFactor,
typename SourceRep,
typename TargetRep,
typename = constra
ints<>>
445extern constexpr bool scaled_max_fits_v =
false;
452template <
typename ScaleFactor,
typename SourceRep,
typename TargetRep>
453extern constexpr bool scaled_max_fits_v<
457 constraints<std::enable_if_t<!std::is_integral<TargetRep>::value>>> =
true;
464template <
typename ScaleFactor,
typename SourceRep,
typename TargetRep>
465extern constexpr bool scaled_max_fits_v<
470 std::enable_if_t<std::is_integral<SourceRep>::value>,
471 std::enable_if_t<std::is_integral<TargetRep>::value>>> =
475 quantity_detail::unsigned_abs(ScaleFactor::num) / quantity_detail::unsigned_abs(ScaleFactor::den)) <=
485template <
typename SourceQuantityType,
typename SourceRep,
typename TargetQuantityType,
typename TargetRep>
486extern constexpr bool scaled_value_in_range_v<
492 scaled_max_fits_v<scale_factor<SourceQuantityType, TargetQuantityType>, SourceRep, TargetRep>;
505template <
typename QuantityType,
typename Rep>
522 explicit constexpr quantity(Rep store_value) noexcept
537 typename OtherQuantityType,
557 typename OtherQuantityType,
558 typename OtherRep = Rep,
561 constexpr auto count_of() const noexcept -> OtherRep {
562 return quantity_detail::scale_value_by<quantity_detail::scale_factor<QuantityType, OtherQuantityType>, OtherRep>(
576 typename OtherQuantityType,
577 typename OtherRep = Rep,
580 constexpr auto as() const noexcept ->
quantity<OtherQuantityType, OtherRep> {
600 template <
typename OtherRep>
615 typename OtherQuantityType,
624 static constexpr auto
639 typename OtherQuantityType,
648 static constexpr auto
651 using rhs_to_lhs_scale_factor = quantity_detail::scale_factor<OtherQuantityType, QuantityType>;
652 constexpr common_rep max_safely_in_range{
static_cast<common_rep
>(
654 quantity_detail::unsigned_abs(rhs_to_lhs_scale_factor::num)) *
655 quantity_detail::unsigned_abs(rhs_to_lhs_scale_factor::den)
657 constexpr common_rep single_increment_delta{
static_cast<common_rep
>(
658 quantity_detail::unsigned_abs(rhs_to_lhs_scale_factor::den) %
659 quantity_detail::unsigned_abs(rhs_to_lhs_scale_factor::num)
661 constexpr common_rep max_in_range{
static_cast<common_rep
>(max_safely_in_range + single_increment_delta - 1)};
664 if (common_rhs >= max_in_range) {
665 return strong_ordering::less;
667 if (quantity_detail::unsigned_abs(common_rhs) > quantity_detail::unsigned_abs(max_in_range)) {
668 return strong_ordering::greater;
683 template <typename Q = QuantityType, constraints<typename units_combination_traits<Q, Q>::sum_type> =
nullptr>
687 static_cast<Rep
>(lhs.count_of<Q>() + rhs.template
count_of<Q>())
707 typename OtherQuantityType,
709 typename Q = QuantityType,
737 typename OtherQuantityType,
739 typename Q = QuantityType,
768 typename Q = QuantityType,
792 typename Q = QuantityType,
820 typename OtherQuantityType,
822 typename Q = QuantityType,
853 typename OtherQuantityType,
855 typename Q = QuantityType,
888 typename Q = QuantityType,
915 typename OtherQuantityType,
917 typename Q = QuantityType,
constexpr generic_ordering_from_three_way_compare() noexcept=default
A class representing a quantity of a physical units type.
Definition quantity.hpp:506
friend constexpr auto operator+(quantity const &lhs, quantity< OtherQuantityType, OtherRep > const &rhs) noexcept -> quantity< typename units_combination_traits< QuantityType, OtherQuantityType >::sum_type, CommonRep >
Add two quantities.
Definition quantity.hpp:713
static constexpr auto three_way_compare(quantity const &lhs, quantity const &rhs) noexcept -> strong_ordering
three-way comparison between two quantities
Definition quantity.hpp:590
friend constexpr auto operator+(quantity const &lhs, quantity const &rhs) noexcept -> quantity< typename units_combination_traits< Q, Q >::sum_type, Rep >
Add two quantities of the same type.
Definition quantity.hpp:684
constexpr auto as() const noexcept -> quantity< OtherQuantityType, OtherRep >
Convert the stored quantity to the specified physical units type and representation.
Definition quantity.hpp:580
constexpr quantity() noexcept
Default construct with a default-constructed representation value.
Definition quantity.hpp:517
friend constexpr auto operator/(quantity const &lhs, Scalar const &rhs) noexcept -> quantity< Q, CommonRep >
Divide a quantity by a scalar.
Definition quantity.hpp:893
constexpr quantity(quantity< OtherQuantityType, OtherRep > const &other) noexcept
Implicitly convert from a quantity with a different physical units type or representation type,...
Definition quantity.hpp:544
friend constexpr auto operator-(quantity const &lhs, quantity< OtherQuantityType, OtherRep > const &rhs) noexcept -> quantity< typename units_combination_traits< QuantityType, OtherQuantityType >::difference_type, CommonRep >
Subtract two quantities.
Definition quantity.hpp:743
friend constexpr auto operator*(quantity const &lhs, quantity< OtherQuantityType, OtherRep > const &rhs) noexcept -> quantity< typename units_combination_traits< QuantityType, OtherQuantityType >::product_type, CommonRep >
Multiply two quantities.
Definition quantity.hpp:828
friend constexpr auto operator*(quantity const &lhs, Scalar const &rhs) noexcept -> quantity< Q, CommonRep >
Multiply a quantity by a scalar.
Definition quantity.hpp:772
constexpr quantity(Rep store_value) noexcept
Construct a quantity holding the specified representation value.
Definition quantity.hpp:522
static constexpr auto three_way_compare(quantity const &lhs, quantity< OtherQuantityType, OtherRep > const &rhs) noexcept -> strong_ordering
three-way comparison between two quantities with different physical units
Definition quantity.hpp:625
static constexpr auto three_way_compare(quantity const &lhs, quantity< QuantityType, OtherRep > const &rhs) noexcept -> strong_ordering
three-way comparison between two quantities with the same physical units but different representation...
Definition quantity.hpp:601
constexpr auto count_of() const noexcept -> OtherRep
Get the numeric representation of the stored quantity, for the specified physical units type.
Definition quantity.hpp:561
friend constexpr auto operator*(Scalar const &lhs, quantity const &rhs) noexcept -> quantity< Q, CommonRep >
Multiply a quantity by a scalar.
Definition quantity.hpp:796
friend constexpr auto operator/(quantity const &lhs, quantity< OtherQuantityType, OtherRep > const &rhs) noexcept -> quantity< typename units_combination_traits< QuantityType, OtherQuantityType >::ratio_type, CommonRep >
Divide two quantities.
Definition quantity.hpp:921
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
#define ARENE_INVARIANT(condition)
Check that an invariant holds, and terminate the application if it does not.
Definition contract.hpp:161
Definition accumulate.hpp:37
strong_ordering_detail::strong_ordering strong_ordering
The enumeration specifying the result of an ordering comparison.
Definition strong_ordering.hpp:147
constexpr auto abs(Number value) noexcept -> Number
Calculate the absolute value of a number.
Definition abs.hpp:46
three_way_compare_detail::compare_three_way compare_three_way
Function object that does a three-way comparison between operands, equivalent to std::compare_three_w...
Definition compare_three_way.hpp:214
constexpr auto gcd(Integral lhs, Integral rhs) noexcept -> Integral
Calculates the Greatest Common Divisor (GCD) of two Integral values.
Definition gcd.hpp:26
constexpr bool always_false_v
A trait which always is false, as a type-dependent expression.
Definition always_false.hpp:25
typename constraints_impl::constraints< EnableIfs... >::type constraints
Helper used to express template constraints in a consistent way using std::enable_if_t.
Definition constraints.hpp:51
Definition accumulate.hpp:36
::uintmax_t uintmax_t
A maximum-width unsigned integer type.
Definition cstdint.hpp:51
typename common_type< Ts... >::type common_type_t
Helper alias template for common_type.
Definition common_type.hpp:99
constexpr auto move(T &&value) noexcept -> remove_reference_t< T > &&
Cast a value to an rvalue.
Definition move.hpp:30
typename enable_if< Condition, Type >::type enable_if_t
Type alias to support constraining templates. Is an alias for the specified type if the Condition is ...
Definition enable_if.hpp:41
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
::uint64_t uint64_t
A 64-bit unsigned integer type.
Definition cstdint.hpp:47
::int64_t int64_t
A 64-bit signed integer type.
Definition cstdint.hpp:71
typename make_unsigned< Type >::type make_unsigned_t
A type alias for the equivalent unsigned type for integral and enumeration types other than bool.
Definition make_unsigned.hpp:252
QuantityType difference_type
The physical quantity type resulting from subtracting quantities with a physical quantity type of Qua...
Definition quantity.hpp:96
QuantityType sum_type
The physical quantity type resulting from adding quantities with a physical quantity type of Quantity...
Definition quantity.hpp:93
Traits class with information about if and how a physical unit of type QuantityType1 can be combined ...
Definition quantity.hpp:85