diff --git a/docs/CMakeLists.txt b/docs/CMakeLists.txt index 04c7fc08..87801659 100644 --- a/docs/CMakeLists.txt +++ b/docs/CMakeLists.txt @@ -87,6 +87,7 @@ set(unitsSphinxDocs "${CMAKE_CURRENT_SOURCE_DIR}/framework/constants.rst" "${CMAKE_CURRENT_SOURCE_DIR}/framework/conversions_and_casting.rst" "${CMAKE_CURRENT_SOURCE_DIR}/framework/dimensions.rst" + "${CMAKE_CURRENT_SOURCE_DIR}/framework/magnitudes.rst" "${CMAKE_CURRENT_SOURCE_DIR}/framework/quantities.rst" "${CMAKE_CURRENT_SOURCE_DIR}/framework/quantity_kinds.rst" "${CMAKE_CURRENT_SOURCE_DIR}/framework/quantity_like.rst" diff --git a/docs/framework.rst b/docs/framework.rst index cacfa71b..952d66fc 100644 --- a/docs/framework.rst +++ b/docs/framework.rst @@ -17,6 +17,7 @@ Framework Basics framework/quantity_points framework/quantity_kinds framework/dimensions + framework/magnitudes framework/units framework/arithmetics framework/constants diff --git a/docs/framework/magnitudes.rst b/docs/framework/magnitudes.rst new file mode 100644 index 00000000..afdb5b43 --- /dev/null +++ b/docs/framework/magnitudes.rst @@ -0,0 +1,121 @@ +.. namespace:: units + +Magnitudes +========== + +The ratio of two Units of the same Dimension---say, ``inches`` and ``centimeters``---is some +constant number, which is known at compile time. It's a positive real number---a *Magnitude*. + +We also use Magnitudes for *Dimensionless* Units. ``percent`` has a Magnitude of :math:`1/100`, and +``dozen`` would have a Magnitude of :math:`12`. + +Interestingly, it turns out that the usual numeric types are not up to this task. We need a +Magnitude representation that can do everything Units can do. This means, among other things: + +1. We need *exact* symbolic computation of the core operations of Quantity Calculus (i.e., products + and rational powers). + +2. We must support *irrational* Magnitudes, because they frequently occur in practice (e.g., + consider the ratio between ``degrees`` and ``radians``). + +3. We should *avoid overflow* wherever possible (note that ``std::intmax_t`` can't even handle + certain simple SI prefixes, such as ``yotta``, representing :math:`10^{24}`). + +Integers' inadequacies are clear enough, but even floating point falls short. Imagine if we +implemented all angular units in terms of ``radians``: then both ``degrees`` and ``revolutions`` +pick up a factor of :math:`\pi`. The arithmetic with *its floating point representation* is +unlikely to cancel *exactly*. + +Another common alternative choice is ``std::ratio``, but this fails the first requirement: rational +numbers are (`rather infamously `_!) *not* closed under rational +powers. + +The only viable solution we have yet encountered is the *vector space representation*. The +implementation is fascinating---but, for purposes of this present page, it's also a distraction. +*Here,* we're more focused on how to *use* these Magnitudes. + +One type per Magnitude, one value per type +------------------------------------------ + +Each typical numeric type (``double``, ``int64_t``, ...) can represent a wide variety of values: the +more, the better. However, Magnitudes are **not** like that. Instead, they comprise a *variety* of +types, and each type can hold only *one* value. + +.. tip:: + + A given Magnitude represents the *same* number, whether you use it as a *type*, or as a *value* + (i.e., an *instance* of that type). + + Use whichever is more convenient. (In practice, this is usually the *value*: especially for end + users rather than library developers.) + +If these types can only represent one value each, why would we bother to instantiate them? Because +*values are easier to use*. + +- ``mag()`` gives the Magnitude value corresponding to any integer ``N``. - You can combine + values in the usual way using ``*``, ``/``, ``==``, and ``!=``, as well as ``pow(m)`` and + ``root(m)`` for any Magnitude value ``m``. + +Traits: integers and rational Magnitudes +---------------------------------------- + +If you have a Magnitude instance ``m``, we provide traits to help you reason about integers and +rational numbers, or manipulate integer or rational parts. + +- ``is_integral(m)``: indicates whether ``m`` represents an *integral* Magnitude. +- ``is_rational(m)``: indicates whether ``m`` represents a *rational* Magnitude. + +The above traits indicate what kind of Magnitude we already have. The next traits *manipulate* a +Magnitude, letting us break it apart into *constituent* Magnitudes which may be more meaningful. +(For example, imagine going from ``inches`` to ``feet``. Naively, we might multiply by the floating +point representation of ``1.0 / 12.0``. However, if we broke this apart into separate numerator and +denominator, it would let us simply *divide by 12*, yielding **exact** results for inputs that +happen to be multiples of 12.) + +- ``numerator(m)`` (value): a Magnitude representing the "numerator", i.e., the largest integer + which divides ``m``, without turning any of its base powers' exponents negative (or making any + previously-negative exponents *more* negative). - ``denominator(m)`` (value): the "numerator" of + the *inverse* of ``m``. + +These traits interact as one would hope. For example, ``is_rational(m)`` is exactly equivalent to +``m == numerator(m) / denominator(m)``. + +Why these particular definitions? Because they are equivalent to the numerator and denominator when +we have a rational number, and they are compatible with how humans write numbers when we don't. +Example: + +- :math:`m1 = \frac{27 \pi^2}{25}`. Then ``numerator(m1) == mag<27>()``, and + ``denominator(m1) == mag<25>()``. +- :math:`m2 = \sqrt{m1}`. Then ``numerator(m2) == mag<3>()``, and ``denominator(m2) == mag<5>()``. + Note that this is consistent with how humans would typically write ``m2``, as + :math:`\frac{3\sqrt{3} \pi}{5}`. + +Getting values out +------------------ + +Magnitude types represent numbers in non-numeric types. They've got some amazing strengths (exact +rational powers!), and some significant weaknesses (no support for basic addition!). So what if you +just want to turn a Magnitude ``m`` into a traditional numeric type ``T``? + +You call ``get_value(m)``. + +This does what it looks like it does, and it does it at compile time. Any intermediate computations +take place in the "widest type in category"---``long double`` for floating point, and +``std::intmax_t`` or ``std::uintmax_t`` for signed or unsigned integers---before ultimately being +cast back to the target type. For ``T = float``, say, this means we get all the precision we'd have +with something like ``long double``, but without any speed penalty at runtime! + +``get_value(m)`` also has the protections you would hope: for example, if ``T`` is an integral +type, we require ``is_integral(m)``. + +How to use Magnitudes +--------------------- + +- First, start with your basic inputs: this will typically be ``mag()`` for any integer ``N``, or + the built-in Magnitude constant ``pi``. (Again, these are all *values*, not types.) + +- Next, combine and manipulate these using the various "Magnitude math" operations, all of which are + **exact**: ``*``, ``/``, ``pow``, ``root``, ``numerator()``, ``denominator()``. + +- If you need to translate a Magnitude ``m`` to a "real" numeric type ``T``, call + ``get_value(m)``. diff --git a/docs/framework/units.rst b/docs/framework/units.rst index 9e739112..c3ad6b96 100644 --- a/docs/framework/units.rst +++ b/docs/framework/units.rst @@ -203,26 +203,27 @@ complete list of all the :term:`SI` prefixes supported by the library:: namespace si { - struct yocto : prefix {}; - struct zepto : prefix {}; - struct atto : prefix {}; - struct femto : prefix {}; - struct pico : prefix {}; - struct nano : prefix {}; - struct micro : prefix {}; - struct milli : prefix {}; - struct centi : prefix {}; - struct deci : prefix {}; - struct deca : prefix {}; - struct hecto : prefix {}; - struct kilo : prefix {}; - struct mega : prefix {}; - struct giga : prefix {}; - struct tera : prefix {}; - struct peta : prefix {}; - struct exa : prefix {}; - struct zetta : prefix {}; - struct yotta : prefix {}; + struct yocto : prefix(mag<10>())> {}; + struct zepto : prefix(mag<10>())> {}; + struct atto : prefix(mag<10>())> {}; + struct femto : prefix(mag<10>())> {}; + struct pico : prefix(mag<10>())> {}; + struct nano : prefix(mag<10>())> {}; + struct micro : prefix(mag<10>())> {}; + struct milli : prefix(mag<10>())> {}; + struct centi : prefix(mag<10>())> {}; + struct deci : prefix(mag<10>())> {}; + struct deca : prefix(mag<10>())> {}; + struct hecto : prefix(mag<10>())> {}; + struct kilo : prefix(mag<10>())> {}; + struct mega : prefix(mag<10>())> {}; + struct giga : prefix(mag<10>())> {}; + struct tera : prefix(mag<10>())> {}; + struct peta : prefix(mag<10>())> {}; + struct exa : prefix(mag<10>())> {}; + struct zetta : prefix(mag<10>())> {}; + struct yotta : prefix(mag<10>())> {}; } @@ -231,12 +232,14 @@ domain:: namespace iec80000 { - struct kibi : prefix {}; - struct mebi : prefix {}; - struct gibi : prefix {}; - struct tebi : prefix {}; - struct pebi : prefix {}; - struct exbi : prefix {}; + struct kibi : prefix(mag<2>())> {}; + struct mebi : prefix(mag<2>())> {}; + struct gibi : prefix(mag<2>())> {}; + struct tebi : prefix(mag<2>())> {}; + struct pebi : prefix(mag<2>())> {}; + struct exbi : prefix(mag<2>())> {}; + struct zebi : prefix(mag<2>())> {}; + struct yobi : prefix(mag<2>())> {}; } diff --git a/src/core/include/units/magnitude.h b/src/core/include/units/magnitude.h index a4de36d9..566861bf 100644 --- a/src/core/include/units/magnitude.h +++ b/src/core/include/units/magnitude.h @@ -401,6 +401,11 @@ struct pi_base { static constexpr long double value = std::numbers::pi_v; }; +/** + * @brief A convenient Magnitude constant for pi, which we can manipulate like a regular number. + */ +inline constexpr Magnitude auto pi = magnitude{}>{}; + //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Magnitude equality implementation. diff --git a/test/unit_test/runtime/magnitude_test.cpp b/test/unit_test/runtime/magnitude_test.cpp index a861b66c..d4e17e9a 100644 --- a/test/unit_test/runtime/magnitude_test.cpp +++ b/test/unit_test/runtime/magnitude_test.cpp @@ -193,7 +193,6 @@ TEST_CASE("magnitude converts to numerical value") SECTION("pi to the 1 supplies correct values") { - constexpr auto pi = pi_to_the<1>(); check_same_type_and_value(get_value(pi), std::numbers::pi_v); check_same_type_and_value(get_value(pi), std::numbers::pi_v); check_same_type_and_value(get_value(pi), std::numbers::pi_v);