units
Use physical dimensions at compile-time or run-time.
Public Member Functions | Protected Member Functions | Friends | List of all members
vnix::units::dimval< T, B > Class Template Reference

Model of a physically dimensioned quantity. More...

#include <dimval.hpp>

Inheritance diagram for vnix::units::dimval< T, B >:
[legend]
Collaboration diagram for vnix::units::dimval< T, B >:
[legend]

Public Member Functions

 dimval ()
 < Allow access to dimension. More...
 
constexpr dimval (T const &v, dim const &d)
 Initialize from numeric value and from dimension. More...
 
template<typename OT , typename OB >
constexpr dimval (dimval< OT, OB > const &v)
 Initialize from other dimensioned value. More...
 
constexpr dimval (T const &n)
 Initialize from dimensionless number. More...
 
constexpr T to_number () const
 Convert to dimensionless number. More...
 
constexpr dim::rat d (dim::off off) const
 Exponent for base at specified offset. More...
 
template<typename OT , typename OB >
constexpr auto operator== (dimval< OT, OB > const &v) const
 Equality-comparison of two dimensioned values. More...
 
template<typename OT , typename OB >
constexpr auto operator!= (dimval< OT, OB > const &v) const
 Inequality-comparison of two dimensioned values. More...
 
template<typename OT , typename OB >
constexpr auto operator< (dimval< OT, OB > const &v) const
 Less-than comparison of two dimensioned values. More...
 
template<typename OT , typename OB >
constexpr auto operator<= (dimval< OT, OB > const &v) const
 Less-than-or-equal comparison of two dimensioned values. More...
 
template<typename OT , typename OB >
constexpr auto operator> (dimval< OT, OB > const &v) const
 Greater-than comparison of two dimensioned values. More...
 
template<typename OT , typename OB >
constexpr auto operator>= (dimval< OT, OB > const &v) const
 Greater-than-or-equal comparison of two dimensioned values. More...
 
template<typename OT , typename OB >
constexpr auto operator+ (dimval< OT, OB > const &v) const
 Sum of two dimensioned values. More...
 
template<typename OT , typename OB >
constexpr auto operator- (dimval< OT, OB > const &v) const
 Difference between two dimensioned values. More...
 
template<typename OT , typename OB >
constexpr dimvaloperator+= (dimval< OT, OB > const &v)
 Modify present instance by adding in a dimensioned value. More...
 
template<typename OT , typename OB >
constexpr dimvaloperator-= (dimval< OT, OB > const &v)
 Modify present instance by subtracting out a dimensioned value. More...
 
template<typename OT , otest< OT > = 0>
constexpr auto operator* (OT const &n) const
 Scale dimensioned value. More...
 
template<typename OT , typename OB >
constexpr auto operator* (dimval< OT, OB > const &v) const
 Multiply two dimensioned values. More...
 
constexpr auto operator[] (size_t off) const
 Support element-access in case it be supported by numeric type. More...
 
template<typename OT , otest< OT > = 0>
constexpr auto dot (OT const &n) const
 Support dot-product in case it be supported by numeric type. More...
 
template<typename OT , typename OB >
constexpr auto dot (dimval< OT, OB > const &v) const
 Support dot-product in case it be supported by numeric type. More...
 
template<typename OT , otest< OT > = 0>
constexpr auto cross (OT const &n) const
 Support cross-product in case it be supported by numeric type. More...
 
template<typename OT , typename OB >
constexpr auto cross (dimval< OT, OB > const &v) const
 Support cross-product in case it be supported by numeric type. More...
 
template<typename OT , otest< OT > = 0>
constexpr auto operator/ (OT const &n) const
 Scale dimensioned quantity by dividing by number. More...
 
constexpr dimval< T, typename B::recip_basedim > inverse () const
 Invert dimensioned value. More...
 
template<typename OT , typename OB >
constexpr auto operator/ (dimval< OT, OB > const &v) const
 Divide two dimensioned values. More...
 
template<typename OT , otest< OT > = 0>
constexpr dimvaloperator*= (OT const &v)
 Modify present instance by multiplying in a dimensionless value. More...
 
template<typename OT , otest< OT > = 0>
constexpr dimvaloperator/= (OT const &v)
 Modify present instance by dividing it by a dimensionless value. More...
 
template<int64_t PN, int64_t PD = 1>
constexpr auto power () const
 Raise dimensioned value to rational power. More...
 
constexpr auto power (dim::rat p) const
 Raise dimensioned value to rational power. More...
 
constexpr auto square_root () const
 Square-root of a dimensioned quantity. More...
 

Protected Member Functions

 dimval (dim const &d)
 Initialize dimension, but leave number undefined. More...
 

Friends

template<typename OT , typename OB >
class dimval
 Allow access to every kind of dimval. More...
 
template<typename OT >
class basic_dyndim
 Allow access to each kind (float or double) of dyndim. More...
 
template<dim::word D, typename OT >
class basic_statdim
 Allow access to every kind of statdim. More...
 
template<typename OT , otest< OT > = 0>
constexpr auto operator* (OT const &n, dimval const &v)
 Scale dimensioned value. More...
 
std::ostream & operator<< (std::ostream &s, dimval const &v)
 Print to to output stream. More...
 

Detailed Description

template<typename T, typename B>
class vnix::units::dimval< T, B >

Model of a physically dimensioned quantity.

Template Parameters
TType of storage (e.g., float or double) for numerical quantity.
BBase-class (statdim_base or dyndim_base) for dimension.

Definition at line 22 of file dimval.hpp.

Constructor & Destructor Documentation

template<typename T, typename B>
vnix::units::dimval< T, B >::dimval ( dim const &  d)
inlineprotected

Initialize dimension, but leave number undefined.

Parameters
dDimension.

Definition at line 72 of file dimval.hpp.

72 : B(d) {}
constexpr dim::rat d(dim::off off) const
Exponent for base at specified offset.
Definition: dimval.hpp:109
template<typename T, typename B>
vnix::units::dimval< T, B >::dimval ( )
inline

< Allow access to dimension.

By default, do not initialize.

Definition at line 78 of file dimval.hpp.

template<typename T, typename B>
constexpr vnix::units::dimval< T, B >::dimval ( T const &  v,
dim const &  d 
)
inline

Initialize from numeric value and from dimension.

NOTE: This is public not because it ought to be called by the user but because there is no easy way to allow every friend operator access to every kind of dimval.

Parameters
vNumeric value.
dDimension.

Definition at line 88 of file dimval.hpp.

88 : number<T>(v), B(d) {}
constexpr dim::rat d(dim::off off) const
Exponent for base at specified offset.
Definition: dimval.hpp:109
template<typename T, typename B>
template<typename OT , typename OB >
constexpr vnix::units::dimval< T, B >::dimval ( dimval< OT, OB > const &  v)
inline

Initialize from other dimensioned value.

Template Parameters
OTNumeric type of other dimensioned value.
OBBase-dimension type of other dimensioned value.
Parameters
vReference to other dimensioned value.

Definition at line 95 of file dimval.hpp.

95 : number<T>(v.v_), B(v.d()) {}
template<typename T, typename B>
constexpr vnix::units::dimval< T, B >::dimval ( T const &  n)
inline

Initialize from dimensionless number.

Parameters
nNumber.

Definition at line 99 of file dimval.hpp.

99 : number<T>(n), B(dim()) {}
basic_dim< dim_base_off > dim
Define dim as specification of basic_dim by way of YAML- and ERB-generated type dim_base_off.
Definition: dim.hpp:260

Member Function Documentation

template<typename T, typename B>
template<typename OT , otest< OT > = 0>
constexpr auto vnix::units::dimval< T, B >::cross ( OT const &  n) const
inline

Support cross-product in case it be supported by numeric type.

Template Parameters
OTNumeric type of factor.
Parameters
nFactor.
Returns
Cross-product.

Definition at line 302 of file dimval.hpp.

302  {
303  auto prod = v_.cross(n);
304  return dimval<decltype(prod), B>(prod, d());
305  }
constexpr dim::rat d(dim::off off) const
Exponent for base at specified offset.
Definition: dimval.hpp:109
template<typename T, typename B>
template<typename OT , typename OB >
constexpr auto vnix::units::dimval< T, B >::cross ( dimval< OT, OB > const &  v) const
inline

Support cross-product in case it be supported by numeric type.

Template Parameters
OTNumeric type of factor.
OBBase-dimension type of factor.
Parameters
vFactor.
Returns
Dot-product.

Definition at line 313 of file dimval.hpp.

313  {
314  auto const pdim = B::prod(v);
315  auto prod = v_.cross(v.v_);
316  return dimval<decltype(prod), decltype(pdim)>(prod, pdim.d());
317  }
template<typename T, typename B>
constexpr dim::rat vnix::units::dimval< T, B >::d ( dim::off  off) const
inline

Exponent for base at specified offset.

Parameters
offOffset.

Definition at line 109 of file dimval.hpp.

109 { return B::d()[off]; }

Here is the caller graph for this function:

template<typename T, typename B>
template<typename OT , otest< OT > = 0>
constexpr auto vnix::units::dimval< T, B >::dot ( OT const &  n) const
inline

Support dot-product in case it be supported by numeric type.

Template Parameters
OTNumeric type of factor.
Parameters
nFactor.
Returns
Dot-product.

Definition at line 280 of file dimval.hpp.

280  {
281  auto prod = v_.dot(n);
282  return dimval<decltype(prod), B>(prod, d());
283  }
constexpr dim::rat d(dim::off off) const
Exponent for base at specified offset.
Definition: dimval.hpp:109
template<typename T, typename B>
template<typename OT , typename OB >
constexpr auto vnix::units::dimval< T, B >::dot ( dimval< OT, OB > const &  v) const
inline

Support dot-product in case it be supported by numeric type.

Template Parameters
OTNumeric type of factor.
OBBase-dimension type of factor.
Parameters
vFactor.
Returns
Dot-product.

Definition at line 291 of file dimval.hpp.

291  {
292  auto const pdim = B::prod(v);
293  auto prod = v_.dot(v.v_);
294  return dimval<decltype(prod), decltype(pdim)>(prod, pdim.d());
295  }
template<typename T, typename B>
constexpr dimval<T, typename B::recip_basedim> vnix::units::dimval< T, B >::inverse ( ) const
inline

Invert dimensioned value.

Returns
Reciprocal of dimval.

Definition at line 335 of file dimval.hpp.

335  {
336  auto const br = this->recip();
337  return dimval<T, typename B::recip_basedim>(invert(v_), br.d());
338  }
constexpr auto invert(T v)
Return the reciprocal of an instance of type T.
Definition: dimval.hpp:31
template<typename T, typename B>
template<typename OT , typename OB >
constexpr auto vnix::units::dimval< T, B >::operator!= ( dimval< OT, OB > const &  v) const
inline

Inequality-comparison of two dimensioned values.

This will throw an exception if the dimensions are different.

Template Parameters
OTNumeric type of other dimensioned value.
OBBase-dimension type of other dimensioned value.
Parameters
vReference to other dimensioned value.
Returns
True only if this and the other be unequal.

Definition at line 130 of file dimval.hpp.

130  {
131  return !(*this == v);
132  }
template<typename T, typename B>
template<typename OT , otest< OT > = 0>
constexpr auto vnix::units::dimval< T, B >::operator* ( OT const &  n) const
inline

Scale dimensioned value.

This function's scope for matching the template-type parameter is limited by SFINAE.

Template Parameters
OTType of scale-factor.
Parameters
nScale-factor.
Returns
Scaled value.

Definition at line 237 of file dimval.hpp.

237  {
238  auto prod = v_ * n;
239  return dimval<decltype(prod), B>(prod, d());
240  }
constexpr dim::rat d(dim::off off) const
Exponent for base at specified offset.
Definition: dimval.hpp:109
template<typename T, typename B>
template<typename OT , typename OB >
constexpr auto vnix::units::dimval< T, B >::operator* ( dimval< OT, OB > const &  v) const
inline

Multiply two dimensioned values.

Template Parameters
OTNumeric type of factor.
OBBase-dimension type of factor.
Parameters
vFactor.
Returns
Product.

Definition at line 263 of file dimval.hpp.

263  {
264  auto const pdim = B::prod(v);
265  auto prod = v_ * v.v_;
266  return dimval<decltype(prod), decltype(pdim)>(prod, pdim.d());
267  }
template<typename T, typename B>
template<typename OT , otest< OT > = 0>
constexpr dimval& vnix::units::dimval< T, B >::operator*= ( OT const &  v)
inline

Modify present instance by multiplying in a dimensionless value.

This function's scope for matching the template-type parameter is limited by SFINAE.

Template Parameters
OTType of scale-factor.
Parameters
vDimensionless scale-factor.
Returns
Scaled value.

Definition at line 361 of file dimval.hpp.

361  {
362  v_ *= v;
363  return *this;
364  }
template<typename T, typename B>
template<typename OT , typename OB >
constexpr auto vnix::units::dimval< T, B >::operator+ ( dimval< OT, OB > const &  v) const
inline

Sum of two dimensioned values.

Template Parameters
OTNumeric type of addend.
OBBase-dimension type of addend.
Parameters
vAddend.
Returns
Sum.

Definition at line 186 of file dimval.hpp.

186  {
187  auto const sdim = B::sum(v); // Check for compatibility of units.
188  auto sum = v_ + v.v_;
189  return dimval<decltype(sum), B>(sum, sdim.d());
190  }
template<typename T, typename B>
template<typename OT , typename OB >
constexpr dimval& vnix::units::dimval< T, B >::operator+= ( dimval< OT, OB > const &  v)
inline

Modify present instance by adding in a dimensioned value.

Template Parameters
OTNumeric type of addend.
OBBase-dimension type of addend.
Parameters
vAddend.
Returns
Sum.

Definition at line 210 of file dimval.hpp.

210  {
211  B::sum(v); // Check for compatibility of units.
212  v_ += v.v_;
213  return *this;
214  }
template<typename T, typename B>
template<typename OT , typename OB >
constexpr auto vnix::units::dimval< T, B >::operator- ( dimval< OT, OB > const &  v) const
inline

Difference between two dimensioned values.

Template Parameters
OTNumeric type of subtractor.
OBBase-dimension type of subtractor.
Parameters
vSubractor.
Returns
Difference.

Definition at line 198 of file dimval.hpp.

198  {
199  auto const ddim = B::diff(v); // Check for compatibility of units.
200  auto diff = v_ - v.v_;
201  return dimval<decltype(diff), B>(diff, ddim.d());
202  }
template<typename T, typename B>
template<typename OT , typename OB >
constexpr dimval& vnix::units::dimval< T, B >::operator-= ( dimval< OT, OB > const &  v)
inline

Modify present instance by subtracting out a dimensioned value.

Template Parameters
OTNumeric type of subtractor.
OBBase-dimension type of subtractor.
Parameters
vSubtractor.
Returns
Difference.

Definition at line 222 of file dimval.hpp.

222  {
223  B::diff(v); // Check for compatibility of units.
224  v_ -= v.v_;
225  return *this;
226  }
template<typename T, typename B>
template<typename OT , otest< OT > = 0>
constexpr auto vnix::units::dimval< T, B >::operator/ ( OT const &  n) const
inline

Scale dimensioned quantity by dividing by number.

This function's scope for matching the template-type parameter is limited by SFINAE.

Template Parameters
OTType of scale-factor.
Parameters
nScale-divisor.
Returns
Scaled value.

Definition at line 328 of file dimval.hpp.

328  {
329  auto quot = v_ / n;
330  return dimval<decltype(quot), B>(quot, d());
331  }
constexpr dim::rat d(dim::off off) const
Exponent for base at specified offset.
Definition: dimval.hpp:109
template<typename T, typename B>
template<typename OT , typename OB >
constexpr auto vnix::units::dimval< T, B >::operator/ ( dimval< OT, OB > const &  v) const
inline

Divide two dimensioned values.

Template Parameters
OTNumeric type of divisor.
OBBase-dimension type of divisor.
Parameters
vDivisor.
Returns
Quotient.

Definition at line 346 of file dimval.hpp.

346  {
347  auto const qdim = B::quot(v);
348  auto quot = v_ / v.v_;
349  return dimval<decltype(quot), decltype(qdim)>(v_ / v.v_, qdim.d());
350  }
template<typename T, typename B>
template<typename OT , otest< OT > = 0>
constexpr dimval& vnix::units::dimval< T, B >::operator/= ( OT const &  v)
inline

Modify present instance by dividing it by a dimensionless value.

This function's scope for matching the template-type parameter is limited by SFINAE.

Template Parameters
OTType of scale-factor.
Parameters
vDimensionless scale-divisor.
Returns
Scaled value.

Definition at line 375 of file dimval.hpp.

375  {
376  v_ /= v;
377  return *this;
378  }
template<typename T, typename B>
template<typename OT , typename OB >
constexpr auto vnix::units::dimval< T, B >::operator< ( dimval< OT, OB > const &  v) const
inline

Less-than comparison of two dimensioned values.

This will throw an exception if the dimensions are different.

Template Parameters
OTNumeric type of other dimensioned value.
OBBase-dimension type of other dimensioned value.
Parameters
vReference to other dimensioned value.
Returns
True only if this be less than the other.

Definition at line 141 of file dimval.hpp.

141  {
142  B::comparison(v); // Check for compatibility of units.
143  return v_ < v.v_;
144  }
template<typename T, typename B>
template<typename OT , typename OB >
constexpr auto vnix::units::dimval< T, B >::operator<= ( dimval< OT, OB > const &  v) const
inline

Less-than-or-equal comparison of two dimensioned values.

This will throw an exception if the dimensions are different.

Template Parameters
OTNumeric type of other dimensioned value.
OBBase-dimension type of other dimensioned value.
Parameters
vReference to other dimensioned value.
Returns
True only if this be less than or equal to the other.

Definition at line 153 of file dimval.hpp.

153  {
154  B::comparison(v); // Check for compatibility of units.
155  return v_ <= v.v_;
156  }
template<typename T, typename B>
template<typename OT , typename OB >
constexpr auto vnix::units::dimval< T, B >::operator== ( dimval< OT, OB > const &  v) const
inline

Equality-comparison of two dimensioned values.

This will throw an exception if the dimensions are different.

Template Parameters
OTNumeric type of other dimensioned value.
OBBase-dimension type of other dimensioned value.
Parameters
vReference to other dimensioned value.
Returns
True only if this and the other be equal.

Definition at line 118 of file dimval.hpp.

118  {
119  B::comparison(v); // Check for compatibility of units.
120  return v_ == v.v_;
121  }
template<typename T, typename B>
template<typename OT , typename OB >
constexpr auto vnix::units::dimval< T, B >::operator> ( dimval< OT, OB > const &  v) const
inline

Greater-than comparison of two dimensioned values.

This will throw an exception if the dimensions are different.

Template Parameters
OTNumeric type of other dimensioned value.
OBBase-dimension type of other dimensioned value.
Parameters
vReference to other dimensioned value.
Returns
True only if this be greater than the other.

Definition at line 165 of file dimval.hpp.

165  {
166  return !(*this <= v);
167  }
template<typename T, typename B>
template<typename OT , typename OB >
constexpr auto vnix::units::dimval< T, B >::operator>= ( dimval< OT, OB > const &  v) const
inline

Greater-than-or-equal comparison of two dimensioned values.

This will throw an exception if the dimensions are different.

Template Parameters
OTNumeric type of other dimensioned value.
OBBase-dimension type of other dimensioned value.
Parameters
vReference to other dimensioned value.
Returns
True only if this be greater than or equal to the other.

Definition at line 176 of file dimval.hpp.

176  {
177  return !(*this < v);
178  }
template<typename T, typename B>
constexpr auto vnix::units::dimval< T, B >::operator[] ( size_t  off) const
inline

Support element-access in case it be supported by numeric type.

Parameters
offOffset of element.

Definition at line 271 of file dimval.hpp.

271  {
272  auto e = v_[off];
273  return dimval<decltype(e), B>(e, d());
274  }
constexpr dim::rat d(dim::off off) const
Exponent for base at specified offset.
Definition: dimval.hpp:109
template<typename T, typename B>
template<int64_t PN, int64_t PD = 1>
constexpr auto vnix::units::dimval< T, B >::power ( ) const
inline

Raise dimensioned value to rational power.

Template Parameters
PNNumerator of power.
PDDenominator of power (by default, 1).
Returns
Transformed value of different dimension.

Definition at line 384 of file dimval.hpp.

384  {
385  auto const pdim = B::template pow<PN, PD>();
386  auto const powr = pow(v_, PN * 1.0 / PD);
387  return dimval<T, decltype(pdim)>(powr, pdim.d());
388  }
constexpr auto pow(dimval< T, B > const &v)
Raise dimensioned value to rational power.
Definition: dimval.hpp:448
template<typename T, typename B>
constexpr auto vnix::units::dimval< T, B >::power ( dim::rat  p) const
inline

Raise dimensioned value to rational power.

Parameters
pRational power.
Returns
Transformed value of different dimension.

Definition at line 393 of file dimval.hpp.

393  {
394  auto const pdim = B::pow(p);
395  auto const powr = pow(v_, p.to_double());
396  return dimval<T, decltype(pdim)>(powr, pdim.d());
397  }
constexpr auto pow(dimval< T, B > const &v)
Raise dimensioned value to rational power.
Definition: dimval.hpp:448
constexpr double to_double() const
Convert to double.
Definition: rational.hpp:71
constexpr auto pow(dimval< T, B > const &v, dim::rat p)
Raise dimensioned value to rational power.
Definition: dimval.hpp:460
template<typename T, typename B>
constexpr auto vnix::units::dimval< T, B >::square_root ( ) const
inline

Square-root of a dimensioned quantity.

Definition at line 400 of file dimval.hpp.

400  {
401  auto const rdim = B::sqrt();
402  T const root = sqrt(v_);
403  return dimval<T, decltype(rdim)>(root, rdim.d());
404  }
constexpr auto sqrt(dimval< T, B > const &v)
Take the squre root of a dimensioned quantity.
Definition: dimval.hpp:436
template<typename T, typename B>
constexpr T vnix::units::dimval< T, B >::to_number ( ) const
inline

Convert to dimensionless number.

Definition at line 102 of file dimval.hpp.

102  {
103  B::number();
104  return v_;
105  }

Here is the caller graph for this function:

Friends And Related Function Documentation

template<typename T, typename B>
template<typename OT >
friend class basic_dyndim
friend

Allow access to each kind (float or double) of dyndim.

Template Parameters
OTType of dyndim's numeric value.

Definition at line 62 of file dimval.hpp.

template<typename T, typename B>
template<dim::word D, typename OT >
friend class basic_statdim
friend

Allow access to every kind of statdim.

Template Parameters
DEncoding of dimension in dim::word.
OTType of statdim's numeric value.

Definition at line 67 of file dimval.hpp.

template<typename T, typename B>
template<typename OT , typename OB >
friend class dimval
friend

Allow access to every kind of dimval.

Template Parameters
OTType of other dimval's numeric value.
OBType of other dimval's base-class for dimension.

Definition at line 58 of file dimval.hpp.

template<typename T, typename B>
template<typename OT , otest< OT > = 0>
constexpr auto operator* ( OT const &  n,
dimval< T, B > const &  v 
)
friend

Scale dimensioned value.

This function's scope for matching the template-type parameter is limited by SFINAE.

Template Parameters
OTType of scale-factor.
Parameters
nScale-factor.
vOriginal value.
Returns
Scaled value.

Definition at line 252 of file dimval.hpp.

252  {
253  auto prod = n * v.v_;
254  return dimval<decltype(prod), B>(prod, v.d());
255  }
template<typename T, typename B>
std::ostream& operator<< ( std::ostream &  s,
dimval< T, B > const &  v 
)
friend

Print to to output stream.

Definition at line 407 of file dimval.hpp.

407  {
408  return s << v.v_ << v.d();
409  }
constexpr auto s
Constant-expression symbol for s.
Definition: units.hpp:1825

The documentation for this class was generated from the following file: