![]() |
Home | Libraries | People | FAQ | More |
A quantity is defined as a value of an arbitrary value type that is associated with a specific unit. For example, while meter is a unit, 3.0 meters is a quantity. Quantities obey two separate algebras: the native algebra for their value type, and the dimensional analysis algebra for the associated unit. In addition, algebraic operations are defined between units and quantities to simplify the definition of quantities; it is effectively equivalent to algebra with a unit-valued quantity.
Quantities are implemented by the quantity
template class defined in boost/units/quantity.hpp
:
template<class Unit,class Y = double> class quantity;
This class is templated on both unit type (Unit
)
and value type (Y
), with the
latter defaulting to double-precision floating point if not otherwise specified.
The value type must have a normal copy constructor and copy assignment operator.
Operators +, -, *, and / are provided for algebraic operations between scalars
and units, scalars and quantities, units and quantities, and between quantities.
In addition, integral and rational powers and roots can be computed using the
pow
<R>
and root
<R>
functions. Finally, the standard set of boolean comparison operators ( ==, !=, <,
<=, >,
and >=
) are provided to allow comparison of quantities from the same unit system.
All operators simply delegate to the corresponding operator of the value type
if the units permit.
For most common value types, the result type of arithmetic operators is the same as the value type itself. For example, the sum of two double precision floating point numbers is another double precision floating point number. However, there are instances where this is not the case. A simple example is given by the natural numbers where the operator arithmetic obeys the following rules (using the standard notation for number systems):
This library is designed to support arbitrary value type algebra for addition,
subtraction, multiplication, division, and rational powers and roots. It
uses Boost.Typeof to deduce the result of these operators. For compilers
that support typeof
, the
appropriate value type will be automatically deduced. For compilers that
do not provide language support for typeof
it is necessary to register all the types used. For the case of natural numbers,
this would amount to something like the following:
BOOST_TYPEOF_REGISTER_TYPE(natural); BOOST_TYPEOF_REGISTER_TYPE(integer); BOOST_TYPEOF_REGISTER_TYPE(rational);