US customary length units added

This commit is contained in:
Mateusz Pusz
2019-04-09 13:34:55 +02:00
parent d781322a8e
commit 941401de31
3 changed files with 43 additions and 4 deletions

View File

@ -27,9 +27,11 @@
namespace units {
// dimension
struct dimension_length : make_dimension_t<exp<base_dim_length, 1>> {};
template<> struct upcasting_traits<typename dimension_length::base_type> : std::type_identity<dimension_length> {};
// SI units
struct millimeter : unit<dimension_length, std::milli> {};
template<> struct upcasting_traits<typename millimeter::base_type> : std::type_identity<millimeter> {};
@ -42,6 +44,20 @@ namespace units {
struct kilometer : unit<dimension_length, std::kilo> {};
template<> struct upcasting_traits<typename kilometer::base_type> : std::type_identity<kilometer> {};
// US customary units
struct yard : unit<dimension_length, std::ratio<9'144, 10'000>> {};
template<> struct upcasting_traits<typename yard::base_type> : std::type_identity<yard> {};
struct foot : unit<dimension_length, std::ratio_multiply<std::ratio<1, 3>, yard::ratio>> {};
template<> struct upcasting_traits<typename foot::base_type> : std::type_identity<foot> {};
struct inch : unit<dimension_length, std::ratio_multiply<std::ratio<1, 12>, foot::ratio>> {};
template<> struct upcasting_traits<typename inch::base_type> : std::type_identity<inch> {};
struct mile : unit<dimension_length, std::ratio_multiply<std::ratio<1'760>, yard::ratio>> {};
template<> struct upcasting_traits<typename mile::base_type> : std::type_identity<mile> {};
// length
template<Unit U = meter, Number Rep = double>
using length = quantity<dimension_length, U, Rep>;
@ -66,6 +82,22 @@ namespace units {
constexpr auto operator""_km(unsigned long long l) { return length<kilometer, std::int64_t>(l); }
constexpr auto operator""_km(long double l) { return length<kilometer, long double>(l); }
// yd
constexpr auto operator""_yd(unsigned long long l) { return length<yard, std::int64_t>(l); }
constexpr auto operator""_yd(long double l) { return length<yard, long double>(l); }
// ft
constexpr auto operator""_ft(unsigned long long l) { return length<foot, std::int64_t>(l); }
constexpr auto operator""_ft(long double l) { return length<foot, long double>(l); }
// in
constexpr auto operator""_in(unsigned long long l) { return length<inch, std::int64_t>(l); }
constexpr auto operator""_in(long double l) { return length<inch, long double>(l); }
// mi
constexpr auto operator""_mi(unsigned long long l) { return length<mile, std::int64_t>(l); }
constexpr auto operator""_mi(long double l) { return length<mile, long double>(l); }
} // namespace literals
} // namespace units

View File

@ -34,10 +34,10 @@ namespace units {
struct meter_per_second : unit<dimension_velocity, std::ratio<1>> {};
template<> struct upcasting_traits<typename meter_per_second::base_type> : std::type_identity<meter_per_second> {};
struct kilometer_per_hour : unit<dimension_velocity, std::ratio<1000, 3600>> {};
struct kilometer_per_hour : unit<dimension_velocity, std::ratio_divide<kilometer::ratio, hour::ratio>> {};
template<> struct upcasting_traits<typename kilometer_per_hour::base_type> : std::type_identity<kilometer_per_hour> {};
struct mile_per_hour : unit<dimension_velocity, std::ratio<44'704, 100'000>> {};
struct mile_per_hour : unit<dimension_velocity, std::ratio_divide<mile::ratio, hour::ratio>> {};
template<> struct upcasting_traits<typename mile_per_hour::base_type> : std::type_identity<mile_per_hour> {};
template<Unit U = meter_per_second, Number Rep = double>

View File

@ -44,12 +44,17 @@ namespace {
// length
static_assert(1_km == 1000_m);
static_assert(1_m == 100_cm);
static_assert(1_m == 1000_mm);
static_assert(1_km + 1_m == 1001_m);
static_assert(10_km / 5_km == 2);
static_assert(10_km / 2 == 5_km);
// static_assert(1_ft == 12_in);
static_assert(1_m == 100_cm);
static_assert(1_yd == 0.9144_m);
static_assert(1_yd == 3_ft);
static_assert(1_ft == 12_in);
static_assert(1_mi == 1760_yd);
// static_assert(5_in + 8_cm == 207_mm);
// velocity
@ -63,6 +68,8 @@ namespace {
static_assert(1.0_km / 1_h == 1_kmph);
static_assert(1000.0_m / 3600.0_s == 1_kmph);
static_assert(10.0_mi / 2_h == 5_mph);
static_assert(2_kmph * 2_h == 4_km);
// static_assert(2_kmph * 15_min == 500_m); // should not compile
static_assert(2_kmph * 15.0_min == 500_m);