![]() |
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.
數量(quantity)被定義為一個任意值類型(value type)的數字和特定的單位(unit)。
例如,米(meter)是一個單位(unit),3.0米(meters)是一個數量(quantity)。
數量遵循兩個獨立的代數法則:值類型(value type)本身的法則,和相關單位(unit)的量綱分析法則(dimensional analysis algebra)。
另外,單位(unit)和數量(quantity)之間定義了代數運算(algebraic operation),用來簡化數量(quantity)的定義;
這和unit-valued數量是相同的。(???)
Quantities are implemented by the quantity
template class defined in boost/units/quantity.hpp
:
數量由定義在boost/units/quantity.hpp文件中的模板類quantity實現:
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.
這個類的模板參數為單位類型(unit type)(Unit)和數值類型(value type)(Y),數值類型默認為雙精度符點數(double-precision floating point)。
數值類型必須有拷貝構造函數和賦值運算符。
+、-、*、和/運算符定義於標量和單位、標量和數量、單位和數量,以及數量和數量之間。
另外,整數和有理數的乘方、開方可以用pow<R>和root<R>函數來計算。
最後,標準的布爾比較(boolean comparison)運算符(==, !=, <, <=, >, >=)提供同一個單位系統(unit system)內的數量(quantity)比較。
所有的運算符在單位(unit)正確的情況下都會直接轉發給相應的值類型(value type)的運算符。
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):
對於大多數值類型(value type)來說,算術運算的結果也是相同的值類型(value type)。
例如,兩個雙精度浮點數(double precision floating point)的和同樣是雙精度浮點數(double precision floating point)。
然而,也是有一些反例的。比如說自然數(natural number)的算術運算就符合如下的規則(使用數字系統(number system)的標準符號(standard notation)):




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.Units庫被設計用來支持任意值類型(value type)的加(addition),減(subtraction),乘(multiplication),除(division),和有理數(rational)的乘方(powers)、開方(roots)。
使用Boost.Typeof庫來推斷操作的結果類型。
對於支持typeof的編譯器,正確的值類型(value type)會被自動推斷。
對於不能為typeof提供語言支持的編譯器,需要自行註冊需要使用的類型。
對於自然數來說,這意味著下面的事情:
BOOST_TYPEOF_REGISTER_TYPE(natural); BOOST_TYPEOF_REGISTER_TYPE(integer); BOOST_TYPEOF_REGISTER_TYPE(rational);
Conversion is only meaningful for quantities as it implies the presence of
at least a multiplicative scale factor and, possibly, and affine linear offset.
Macros for simplifying the definition of conversions between units can be
found in boost/units/conversion.hpp
and boost/units/absolute.hpp
(for affine conversions with offsets).
轉換只對數量(quantity)有意義,它意味著至少一個乘法比例因子(multiplicative scale factor)的存在,和仿射線性偏移(affine linear offset)的可能存在。
簡化單位轉換定義的宏可以在boosot/units/conversion.hpp和boost/units/absolute.hpp(用於帶偏移(offset)的仿射轉換(affine conversion))中找到。
The macro BOOST_UNITS_DEFINE_CONVERSION_FACTOR
specifies a scale factor for conversion from the first unit type to the second.
The first argument must be a base_unit.
The second argument can be either a base_unit or a unit.
宏BOOST_UNITS_DEFINE_CONVERSION_FACTOR定義了從第一個單位(unit)轉換到第二個的比例因子(scale factor)。
第一個參數必須是base_unit。
第二個參數可以是base_unit或者unit。
Let's declare a simple base unit:
我們定義一個簡單的基礎單位:
struct foot_base_unit : base_unit<foot_base_unit, length_dimension, 10> { };
Now, we want to be able to convert feet to meters and vice versa. The foot
is defined as exactly 0.3048 meters, so we can write the following
現在我們想要英尺(foot)和米(meter)之間的相互轉換。一英尺被定義為0.3048米,於是我們這樣寫
BOOST_UNITS_DEFINE_CONVERSION_FACTOR(foot_base_unit, meter_base_unit, double, 0.3048);
Alternately, we could use the SI length typedef:
或者,我們可以使用SI長度的typedef:
BOOST_UNITS_DEFINE_CONVERSION_FACTOR(foot_base_unit, SI::length, double, 0.3048);
Since the SI unit of length is the meter, these two definitions are equivalent.
If these conversions have been defined, then converting between scaled forms
of these units will also automatically work.
長度的SI單位是米,所以這兩個定義是等價的。如果這些轉換被定義了,那麼從這些單位衍生的其它單位也可以自動地工作。
The macro BOOST_UNITS_DEFAULT_CONVERSION
specifies a conversion that will be applied to a base unit when no direct
conversion is possible. This can be used to make arbitrary conversions work
with a single specialization:
宏BOOST_UNITS_DEFAULT_CONVERSION指明了當不存在直接轉換時轉換被應用到基礎單位(base unit)上。這可以只通過一個指明就使得任意的轉換工作。
struct my_unit_tag : boost::units::base_unit<my_unit_tag, boost::units::force_type, 1> {}; // define the conversion factor BOOST_UNITS_DEFINE_CONVERSION_FACTOR(my_unit_tag, SI::force, double, 3.14159265358979323846); // make conversion to SI the default. BOOST_UNITS_DEFAULT_CONVERSION(my_unit_tag, SI::force);
This library is designed to emphasize safety above convenience when performing
operations with dimensioned quantities. Specifically, construction of quantities
is required to fully specify both value and unit. Direct construction from
a scalar value is prohibited (though the static member function from_value
is provided to enable this functionality where it is necessary. In addition,
a quantity_cast
to a reference allows direct access to the underlying value of a quantity
variable. An explicit constructor is provided to enable conversion between
dimensionally compatible quantities in different unit systems. Implicit conversions
between unit systems are allowed only when the reduced units are identical,
allowing, for example, trivial conversions between equivalent units in different
systems (such as SI seconds and CGS seconds) while simultaneously enabling
unintentional unit system mismatches to be caught at compile time and preventing
potential loss of precision and performance overhead from unintended conversions.
Assignment follows the same rules. An exception is made for quantities for
which the unit reduces to dimensionless; in this case, implicit conversion
to the underlying value type is allowed via class template specialization.
Quantities of different value types are implicitly convertible only if the
value types are themselves implicitly convertible. The quantity class also defines
a value()
member for directly accessing the underlying value.
Boost.Units庫被用來設計強調單位轉換操作中的安全性。所以,數量(quantity)的構造要求完全指定數值(value)和單位(unit)。
直接從標量數值(scalar value)構造數量(quantity)是被禁止的(如果需要可以通過靜態成員函數(static member function)from_value完成)。
另外,quantity_cast應用於quantity變量的引用之上可以直接讀取其內部的數值。
顯式(explicit)的構造函數可以從不同單位系統(unit system)的量綱兼容(dimensionally compatible)的數量(quantity)進行轉換。
在不同單位系統(unit system)之間的隱式(implicit)的轉換只有當reduced units一致時才可以進行,不同單位系統之間等價單位的任意轉換都是允許的(例如SI的秒和CGS的秒),同時單位系統(unit system)之間無意(unintential)的不匹配會在編譯時被捕獲,並且無意的精度損失(precision loss)和運行時開銷(preformance overhead)都會被阻止。
賦值運算也符合上面的規則。
當數量(quantity)的單位(unit)被化簡為無量綱(dimensionless)是情況是不同的;在這種情況下,內部的值類型(underlying value type)之間的隱式轉換是通過類模板的特化(class template specialization)。
不同值類型(value type)的數量(quantity)之間只有當值類型可以隱式(implicit)轉換時才可以隱式(implicit)轉換。
類quantity也定義了成員函數value()來直接訪問內部的數值(underlying value)。
To summarize, conversions are allowed under the following conditions :
總結一下,轉換在如下情況下被允許:
quantity<Unit,Y>
to quantity<Unit,Z>
is allowed if Y and Z are implicitly convertible.
Y和Z可以隱式轉換時,quantity<Unit,Y>可以被隱式轉換到quantity<Unit,Z>。
quantity<Unit,Y>
and quantity<Unit,Z>
is allowed if Y and Z are implicitly convertible.
Y和Z可以隱式轉換時,quantity<Unit,Y>和quantity<Unit,Z>可以相互賦值。
quantity<Unit1,Y>
and quantity<Unit2,Z>
is allowed if Unit1 and
Unit2 have the same dimensions
and if Y and Z are implicitly convertible.
Unit1和Unit2擁有相同的量綱並且Y和Z可以隱式轉換時,quantity<Unit1,Y>和quantity<Unit2,Z>可以顯式轉換
quantity<Unit1,Y>
and quantity<Unit2,Z>
is allowed if Unit1 reduces
to exactly the same combination of base units as Unit2
and if Y and Z are convertible.
Unit1和Unit2可以化簡為相同的基礎單位(base unit)組合並且Y和Z可以轉換時,quantity<Unit1,Y>和quantity<Unit2,Z>可以隱式轉換。
quantity<Unit1,Y>
and quantity<Unit2,Z>
is allowed under the same conditions as implicit conversion.
quantity<Unit1,Y>和quantity<Unit2,Z>也可以相互賦值。
quantity<Unit,Y>
can be directly constructed from a value of type Y
using the static member function from_value.
Doing so, naturally, bypasses any type-checking of the newly assigned value,
so this method should be used only when absolutely necessary.
quantity<Unit,Y>可以通過靜態成員函數from_value直接從值類型(value type)Y構造。
但這麼做就跳過了所有的類型檢查(type-checking),所以只有當確實需要時才使用。
Of course, any time implicit conversion is allowed, an explicit conversion
is also legal.
當然,隱式轉換可以時,顯式轉換都是可以的。
Because dimensionless quantities have no associated units, they behave as
normal scalars, and allow implicit conversion to and from the underlying
value type or types that are convertible to/from that value type.
無量綱數量(dimensionless quantity)沒有相關的單位(unit),它們和普通的標量一樣,可以和內部數值類型(underlying value type)之間進行隱式轉換,或者其它可以和數值類型轉換的類型。