units
Use physical dimensions at compile-time or run-time.
dyndim-base.hpp
Go to the documentation of this file.
1 /// @file vnix/units/dyndim-base.hpp
2 /// @brief Definition of vnix::units::dyndim_base.
3 /// @copyright 2019 Thomas E. Vaughan; all rights reserved.
4 /// @license BSD Three-Clause; see LICENSE.
5 
6 #ifndef VNIX_UNITS_DYNDIM_BASE_HPP
7 #define VNIX_UNITS_DYNDIM_BASE_HPP
8 
9 #include <vnix/units/dim.hpp>
10 
11 namespace vnix {
12 namespace units {
13 
14 
15 /// Base-type for a dimensioned value whose dimension is specified, perhaps
16 /// dynamically at run-time, by way of the constructor.
17 ///
18 /// The dimension is specified as a set of exponents, one for each of the five
19 /// base dimensions (time, length, mass, charge, and temperature).
20 ///
21 /// For statdim_base, the dimension is always known statically at compile-time.
22 /// For dyndim_base, the dimension is known statically at compile-time only if
23 /// the dimension be specified to the constructor as a constant expression.
24 class dyndim_base {
25  dim d_; ///< Exponents representing dimension.
26 
27 public:
28  /// Initialize from exponents representing dimension.
29  constexpr dyndim_base(dim dd) : d_(dd) {}
30 
31  /// Exponent for each unit in dimensioned quantity. This is not static
32  /// because it needs to be consistent with signature of dyndim.
33  constexpr dim d() const { return d_; }
34 
35  /// Throw if dimension be non-null.
36  constexpr void number() const {
37  if (d() != nul_dim) { throw "dimensioned quantity is not a number"; }
38  }
39 
40  /// Test for comparison of dimensioned values.
41  /// @tparam B dyndim_base or statdim_base.
42  /// @param b Dimension of right side.
43  template <typename B> constexpr void comparison(B const &b) const {
44  if (d_ != b.d()) { throw "incompatible dimensions for comparison"; }
45  }
46 
47  /// Dimension for sum of dimensioned values.
48  /// @tparam B dyndim_base or statdim_base.
49  /// @param b Dimension of addend.
50  /// @return Dimension of sum.
51  template <typename B> constexpr dyndim_base sum(B const &b) const {
52  if (d_ != b.d()) { throw "incompatible dimensions for addition"; }
53  return d_;
54  }
55 
56  /// Dimension for difference of dimensioned values.
57  /// @tparam B dyndim_base or statdim_base.
58  /// @param b Dimension of subtractor.
59  /// @return Dimension of difference.
60  template <typename B> constexpr dyndim_base diff(B const &b) const {
61  if (d_ != b.d()) { throw "incompatible dimensions for subtraction"; }
62  return d_;
63  }
64 
65  /// Dimension for product of dimensioned values.
66  /// @tparam B dyndim_base or statdim_base.
67  /// @param b Dimension of factor.
68  /// @return Dimension of product.
69  template <typename B> constexpr dyndim_base prod(B const &b) const {
70  return d_ + b.d();
71  }
72 
73  /// Dimension for quotient of dimensioned values.
74  /// @tparam B dyndim_base or statdim_base.
75  /// @param b Dimension of divisor.
76  /// @return Dimension of quotient.
77  template <typename B> constexpr dyndim_base quot(B const &b) const {
78  return d_ - b.d();
79  }
80 
81  /// Base-dimensions corresponding to reciprocal of dimensioned quantity.
82  using recip_basedim = dyndim_base;
83 
84  /// Dimension for reciprocal of dimensioned value.
85  /// @return Dimension of reciprocal.
86  constexpr recip_basedim recip() const { return nul_dim - d_; }
87 
88  /// Dimension for rational power of dimensioned value.
89  /// @tparam PN Numerator of power.
90  /// @tparam PD Denominator of power.
91  /// @return Dimension of result.
92  template <int64_t PN, int64_t PD = 1> constexpr dyndim_base pow() const {
93  return d_ * dim::rat(PN, PD);
94  }
95 
96  /// Dimension for rational power of dimensioned value.
97  /// @param p Rational power.
98  /// @return Dimension of result.
99  constexpr dyndim_base pow(dim::rat p) const { return d_ * p; }
100 
101  /// Dimension for square-root of dimensioned value.
102  /// @return Dimension of square-root.
103  constexpr dyndim_base sqrt() const { return d_ / dim::rat(2); }
104 };
105 
106 
107 } // namespace units
108 } // namespace vnix
109 
110 #endif // ndef VNIX_UNITS_DYNDIM_BASE_HPP
constexpr void comparison(B const &b) const
Test for comparison of dimensioned values.
Definition: dyndim-base.hpp:43
constexpr recip_basedim recip() const
Dimension for reciprocal of dimensioned value.
Definition: dyndim-base.hpp:86
constexpr dyndim_base diff(B const &b) const
Dimension for difference of dimensioned values.
Definition: dyndim-base.hpp:60
Base-type for a dimensioned value whose dimension is specified, perhaps dynamically at run-time...
Definition: dyndim-base.hpp:24
constexpr dim d() const
Exponent for each unit in dimensioned quantity.
Definition: dyndim-base.hpp:33
constexpr dyndim_base(dim dd)
Initialize from exponents representing dimension.
Definition: dyndim-base.hpp:29
constexpr rational(stype n=0, stype d=1)
Initialize from numerator and denominator.
Definition: rational.hpp:47
constexpr basic_dim operator-(basic_dim const &s) const
Subtract corresponding exponents.
Definition: dim.hpp:203
constexpr dyndim_base quot(B const &b) const
Dimension for quotient of dimensioned values.
Definition: dyndim-base.hpp:77
constexpr dyndim_base pow(dim::rat p) const
Dimension for rational power of dimensioned value.
Definition: dyndim-base.hpp:99
static constexpr dim nul_dim
Null dimension.
Definition: dim.hpp:263
constexpr dyndim_base sum(B const &b) const
Dimension for sum of dimensioned values.
Definition: dyndim-base.hpp:51
constexpr void number() const
Throw if dimension be non-null.
Definition: dyndim-base.hpp:36
constexpr dyndim_base prod(B const &b) const
Dimension for product of dimensioned values.
Definition: dyndim-base.hpp:69
constexpr dyndim_base sqrt() const
Dimension for square-root of dimensioned value.
constexpr basic_dim operator*(rat f) const
Multiply exponents by rational factor.
Definition: dim.hpp:211
dim d_
Exponents representing dimension.
Definition: dyndim-base.hpp:25
Thomas E. Vaughan&#39;s public software.
Definition: rational.hpp:13
Classes and functions supporting a model of physically dimensioned quantities.
constexpr dyndim_base pow() const
Dimension for rational power of dimensioned value.
Definition: dyndim-base.hpp:92
constexpr bool operator!=(basic_dim const &d) const
Definition: dim.hpp:229
constexpr basic_dim operator/(rat f) const
Divide exponents by rational factor.
Definition: dim.hpp:217