Cpp Utilities 1.2.3
Namespaces
Dimensional Analysis

Helper classes, typedefs and functions for dimensional analyse. More...

Namespaces

namespace  Dimensional
 Namespace for all classes, typedefs and functions of dimensional analyse. See Dimensional Analysis for more instrucion.
 

Detailed Description

Helper classes, typedefs and functions for dimensional analyse.

Provide helperful functionalities for dimensional analysis with pros:
Zero-cost abstraction Using template to encapsulate functionalities, grants same memory-layout and runtime performance as primitive types.
Strong typed All values in dimensional objects has its unique unit**, calculation between them is guaranteed by compiler warning and error.
Non-standard unit of same "type", like yard vs meter, is supported, and will be regarded like same unit, std::ratio is used for modifier of the unit. Calculations between same unit with different ratio is safe, ratios of each operand will be performed into the calculation.
Add/subtract with same unit is allowed, whereas with different units will cause compile error.
Multiply/divide values with any units is safe, a new value with new unit will be generated.
User defined unit is supported. Helper types is provided to derive new unit from existsing units.

Sample Code

Quantity<double, Dimensional::length> meters(1);
Quantity<double, Dimensional::length, Dimensional::ratio_yard> yards(1.0);
meters += yards;
yards = meters;
// 2.09361yard 1.9144meter 1.9144meter
std::cout << yards.value() << "yard "
<< yards.standard_value() << "meter "
<< meters.value() << "meter" << std::endl;
auto speed = yards / Quantity<double, Dimensional::time>(1);
using ratio_km_per_h = std::ratio_divide<std::ratio<1000>, std::ratio<3600>>;
using ratio_mile_per_h = std::ratio_divide<Dimensional::ratio_mile, std::ratio<3600>>;
// 0.836127m/s 3.01006km/h 1.87036mile/h
std::cout << speed.value() << "m/s "
<< quantity_cast<ratio_km_per_h>(speed).value() << "km/h "
<< quantity_cast<ratio_mile_per_h>(speed).value() << "mile/h"
<< std::endl;
The Quantity struct is used to describe arithmetic values with units.
Definition: DimensionalAnalysis.hpp:394
Quantity< T, U, NewRatio > quantity_cast(Quantity< T, U, Ratio > x)