xo-unit ++ docs ++ quantity arithmetic + example
This commit is contained in:
parent
412a0ba163
commit
b0ce5eaee9
34 changed files with 713 additions and 173 deletions
|
|
@ -3,8 +3,8 @@
|
|||
xo_doxygen_collect_deps()
|
||||
xo_docdir_doxygen_config()
|
||||
xo_docdir_sphinx_config(
|
||||
index.rst examples.rst glossary.rst install.rst implementation.rst development.rst
|
||||
quantity-reference.rst quantity-class.rst quantity-factoryfunctions.rst quantity-unitvars.rst
|
||||
index.rst examples.rst glossary.rst install.rst implementation.rst
|
||||
quantity-reference.rst quantity-class.rst quantity-factoryfunctions.rst quantity-unitvars.rst quantity-source-code.rst
|
||||
xquantity-reference.rst xquantity-class.rst
|
||||
scaled-unit-reference.rst scaled-unit-class.rst scaled-unit-constants.rst
|
||||
natural-unit-class.rst
|
||||
|
|
@ -12,5 +12,7 @@ xo_docdir_sphinx_config(
|
|||
bu-store-class.rst basis-unit-reference.rst
|
||||
basis-unit-class.rst basis-unit-constants.rst
|
||||
dimension-enum.rst
|
||||
development.rst
|
||||
ubuntu-github-workflow.rst
|
||||
)
|
||||
#xo_utest_coverage_config2()
|
||||
|
|
|
|||
|
|
@ -22,7 +22,7 @@ Context
|
|||
+----------------+ |
|
||||
| bu_store | |
|
||||
+----------------+---------------+
|
||||
|cYEL basis_unit |
|
||||
|cBLU basis_unit |
|
||||
+--------------------------------+
|
||||
| dimension |
|
||||
+--------------------------------+
|
||||
|
|
|
|||
|
|
@ -22,7 +22,7 @@ Context
|
|||
+----------------+ |
|
||||
| bu_store | |
|
||||
+----------------+---------------+
|
||||
|cYEL basis_unit |
|
||||
|cBLU basis_unit |
|
||||
+--------------------------------+
|
||||
| dimension |
|
||||
+--------------------------------+
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ Built-in named units for each native dimension
|
|||
+----------------+ |
|
||||
| bu_store | |
|
||||
+----------------+---------------+
|
||||
|cYEL basis_unit |
|
||||
|cBLU basis_unit |
|
||||
+--------------------------------+
|
||||
| dimension |
|
||||
+--------------------------------+
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ Context
|
|||
+--------------------------------+
|
||||
| natural_unit |
|
||||
+--------------------------------+
|
||||
|cYEL bpu |
|
||||
|cBLU bpu |
|
||||
+----------------+ |
|
||||
| bu_store | |
|
||||
+----------------+---------------+
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@ Basis Unit Store
|
|||
+--------------------------------+
|
||||
| bpu |
|
||||
+----------------+ |
|
||||
|cYEL bu_store | |
|
||||
|cBLU bu_store | |
|
||||
+----------------+---------------+
|
||||
| basis_unit |
|
||||
+--------------------------------+
|
||||
|
|
|
|||
|
|
@ -15,6 +15,7 @@ author = 'Roland Conybeare'
|
|||
|
||||
#extensions = []
|
||||
extensions = [ "breathe",
|
||||
"sphinx.ext.mathjax", # inline math
|
||||
"sphinx.ext.autodoc", # generate info from docstrings
|
||||
"sphinxcontrib.ditaa", # diagrams-through-ascii-art
|
||||
"sphinxcontrib.plantuml" # text -> uml diagrams
|
||||
|
|
|
|||
|
|
@ -24,7 +24,7 @@ Context
|
|||
+----------------+---------------+
|
||||
| basis_unit |
|
||||
+--------------------------------+
|
||||
|cYEL dimension |
|
||||
|cBLU dimension |
|
||||
+--------------------------------+
|
||||
|
||||
Introduction
|
||||
|
|
|
|||
|
|
@ -4,26 +4,35 @@ Glossary
|
|||
--------
|
||||
|
||||
.. glossary::
|
||||
basis dimension
|
||||
Orthogonal directions associated with basis units, for example *mass*, *length*, *time*.
|
||||
In xo-unit these are represented by the enum ``xo::qty::dim``.
|
||||
dimension
|
||||
dim
|
||||
| Fundamental, orthogonal directions associated available for constructing units.
|
||||
| For example *mass*, *length*, *time*.
|
||||
| In *xo-unit* these are represented by the enum :doc:`xo::qty::dimension<dimension-enum>`.
|
||||
|
||||
basis unit (bu)
|
||||
An implementation type representing a quantity (with associated scale) in the direction of a single :term:`basis dimension`.
|
||||
For example *milliseconds*, *seconds*, and *hours* stand for different basis units with the *time* dimension.
|
||||
In xo-unit these are represented by the template type ``xo::qty::basis_unit``.
|
||||
basis unit
|
||||
bu
|
||||
| An implementation type representing a quantity (with associated scale) in the direction of a single :term:`dimension`.
|
||||
| For example *milliseconds*, *seconds*, and *hours* stand for different basis units within the *time* dimension.
|
||||
| In *xo-unit* these are represented by the template type :doc:`xo::qty::basis_unit<basis-unit-class>`.
|
||||
|
||||
basis power unit
|
||||
bpu
|
||||
A rational power of a (single) basis unit. For example *s\ :sup:-2* or *hr\ :sup:-(1/2)*.
|
||||
In xo-unit these are represented by the template type ``xo::qty::bpu``.
|
||||
| A rational power of a (single) basis unit.
|
||||
| For example :math:`s^{-2}` for unit "per second squared" or :math:`yr^{-(1/2)}` for "per square-root of a year".
|
||||
| In *xo-unit* these are represented by the template type :doc:`xo::qty::bpu<bpu-class>`
|
||||
|
||||
natural unit (nu)
|
||||
A cartesian product of basis power units (bpus). For example *kg.m.s\ :sup:-2* or *hr\ :sup:-(1/2)*.
|
||||
In xo-unit these are represented by template type ``xo::qty::natural_unit``.
|
||||
natural unit
|
||||
nu
|
||||
| A cartesian product of basis power units (bpus);
|
||||
| For example :math:`kg.m.s^{-2}` or :math:`hr^{-(1/2)}`.
|
||||
| In *xo-unit* these are represented by template type :doc:`xo::qty::natural_unit<natural-unit-class>`.
|
||||
|
||||
scaled unit (su)
|
||||
A dimensionless multiple of a natural unit. Used as intermediate value when coalescing quantities involving different units.
|
||||
In xo-unit these are represented by template type ``xo::qty::scaled_unit``.
|
||||
scaled unit
|
||||
su
|
||||
| A dimensionless multiple of a natural unit.
|
||||
| Used as intermediate value when coalescing quantities involving different units.
|
||||
| In *xo-unit* these are represented by template type :doc:`xo::qty::scaled_unit<scaled-unit-class>`.
|
||||
|
||||
XO
|
||||
A set of integrated c++ libraries for complex event processing, with browser and python integration.
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@
|
|||
Components
|
||||
==========
|
||||
|
||||
Library dependencies for *xo-unit*:
|
||||
Library dependency tower for *xo-unit*:
|
||||
|
||||
.. ditaa::
|
||||
|
||||
|
|
@ -14,28 +14,31 @@ Library dependencies for *xo-unit*:
|
|||
+-----------------+
|
||||
| xo_flatstring |
|
||||
+-----------------+
|
||||
| xo_cmake |
|
||||
+-----------------+
|
||||
|
||||
``xo-unit`` also depends on ``xo-cmake`` macros.
|
||||
Install instructions :doc:`here<install>`
|
||||
|
||||
Abstraction tower for *xo-unit* components:
|
||||
|
||||
.. ditaa::
|
||||
:--scale: 0.85
|
||||
|
||||
+-----------+-----------+
|
||||
| quantity | xquantity |
|
||||
+-----------+-----------+
|
||||
| scaled_unit |
|
||||
+-----------------------+
|
||||
| natural_unit |
|
||||
+-----------------------+
|
||||
| bpu |
|
||||
+-----------+ |
|
||||
| bu_store | |
|
||||
+-----------+-----------+
|
||||
| basis_unit |
|
||||
+-----------------------+
|
||||
| dimension |
|
||||
+-----------------------+
|
||||
+----------------+---------------+
|
||||
| quantity | xquantity |
|
||||
+----------------+---------------+
|
||||
| scaled_unit |
|
||||
+--------------------------------+
|
||||
| natural_unit |
|
||||
+--------------------------------+
|
||||
| bpu |
|
||||
+----------------+ |
|
||||
| bu_store | |
|
||||
+----------------+---------------+
|
||||
| basis_unit |
|
||||
+--------------------------------+
|
||||
| dimension |
|
||||
+--------------------------------+
|
||||
|
||||
- :doc:`quantity<quantity-reference>`:
|
||||
|
||||
|
|
|
|||
|
|
@ -28,7 +28,7 @@ runtime (since we can't construct new c++ types at runtime).
|
|||
|
||||
.. toctree::
|
||||
:maxdepth: 2
|
||||
:caption: xo-unit contents:
|
||||
:caption: xo-unit contents
|
||||
|
||||
install
|
||||
examples
|
||||
|
|
@ -42,10 +42,7 @@ runtime (since we can't construct new c++ types at runtime).
|
|||
basis-unit-reference
|
||||
dimension-enum
|
||||
development
|
||||
|
||||
Indices and Tables
|
||||
------------------
|
||||
|
||||
* :ref:`glossary`
|
||||
* :ref:`genindex`
|
||||
* :ref:`search`
|
||||
ubuntu-github-workflow
|
||||
glossary
|
||||
genindex
|
||||
search
|
||||
|
|
|
|||
|
|
@ -7,26 +7,35 @@ Install
|
|||
=======
|
||||
|
||||
``xo-unit`` uses supporting header-only libraries ``xo-ratio`` and ``xo-flatstring``.
|
||||
All three are on github:
|
||||
and (optionally) cmake macros ``xo-cmake``. These are on github:
|
||||
|
||||
- `xo-unit source`_
|
||||
- `xo-ratio source`_
|
||||
- `xo-flatstring source`_
|
||||
- `xo-unit source`_ (constexpr quantities, units and dimension-checking)
|
||||
- `xo-ratio source`_ (constexpr exact ratios)
|
||||
- `xo-flatstring source`_ (constexpr strings)
|
||||
- `xo-cmake source`_ (shared cmake macros)
|
||||
|
||||
.. _xo-unit source: https://github.com/rconybea/xo-unit
|
||||
.. _xo-ratio source: https://github.com/rconybea/xo-ratio
|
||||
.. _xo-flatstring source: https://github.com/rconybea/xo-flatstring
|
||||
.. _xo-cmake source: https://github.com/rconybea/xo-cmake
|
||||
|
||||
`xo-cmake` is nccessary to invoke `xo` cmake build for:
|
||||
|
||||
- site install
|
||||
- example programs
|
||||
- unit tests
|
||||
|
||||
Can omit to instead copy `xo_unit`, `xo-ratio` and `xo-flatstring` source trees;
|
||||
see instructions below for including as git submodule
|
||||
|
||||
Implementation relies on some c++20 features (for example class-instances as template arguments).
|
||||
Tested with gcc 12.3, 13.2
|
||||
|
||||
``xo-unit`` also relies on sister header-only libraries:
|
||||
|
||||
One way to use ``xo-unit`` in a project is to import the source tree directly:
|
||||
|
||||
Include as submodule
|
||||
--------------------
|
||||
|
||||
One way to use ``xo-unit`` in a project is to import the source tree directly:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
cd myproject
|
||||
|
|
@ -50,21 +59,18 @@ to c++ source files that rely on xo-unit
|
|||
Ubuntu Install Pattern
|
||||
----------------------
|
||||
|
||||
Example instructions (from github actions CI) for build starting from stock ubuntu are in `ubuntu-main.yml`_
|
||||
|
||||
.. _ubuntu-main.yml: https://github.com/Rconybea/xo-unit/blob/main/.github/workflows/ubuntu-main.yml
|
||||
|
||||
:doc:`Example instructions<ubuntu-github-workflow>` (used for github actions CI) for build starting from stock ubuntu
|
||||
|
||||
Installing from source
|
||||
----------------------
|
||||
|
||||
Although the xo-unit, xo-ratio and xo-flatstring libraries are header-only,
|
||||
they do have a build dependency on shared cmake macros, and a bootstrap script `xo-cmake-config`.
|
||||
Install scripts for `xo-unit`, `xo-ratio` and `xo-flatstring` depend on shared cmake macros
|
||||
and a bootstrap script `xo-cmake-config`.
|
||||
|
||||
* `xo-cmake`_ source
|
||||
|
||||
.. _xo-cmake: https://github.com/rconybea/xo-cmake
|
||||
|
||||
|
||||
Preamble:
|
||||
|
||||
.. code-block:: bash
|
||||
|
|
@ -183,7 +189,7 @@ Preamble:
|
|||
git clone https://github.com/rconybea/xo-unit
|
||||
|
||||
PREFIX=~/local # ..or other desired installation prefix
|
||||
|
||||
|
||||
Build and Install `catch2` (assuming ubuntu here):
|
||||
|
||||
.. code-block:: bash
|
||||
|
|
@ -216,7 +222,7 @@ Build and Install `xo-randomgen` (no unit tests yet):
|
|||
cmake --build xo-randomgen/.build -j
|
||||
cmake --install xo-randomgen/.build
|
||||
|
||||
Build, Test and Install `xo-flatstring`:
|
||||
Build, Test and Install `xo-flatstring`:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
|
|
@ -255,7 +261,7 @@ To enable building example programs:
|
|||
|
||||
cd ~/proj/xo
|
||||
cmake -DCMAKE_INSTALL_PREFIX=$PREFIX -DXO_ENABLE_EXAMPLES=1 -B xo-unit/.build -S xo-unit
|
||||
|
||||
|
||||
Run examples from the build directory:
|
||||
|
||||
.. code-block:: bash
|
||||
|
|
@ -288,17 +294,16 @@ Preamble (assuming ubuntu here):
|
|||
sudo apt-get install python3-sphinx-rtd-theme
|
||||
sudo apt-get install python3-breathe
|
||||
|
||||
Build `xo-unit` docs
|
||||
Build `xo-unit` docs
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
cd ~/proj/xo
|
||||
cmake -DCMAKE_INSTALL_PREFIX=$PREFIX -B xo-unit/.build
|
||||
cmake --build xo-unit/.build -- docs
|
||||
cmake --build xo-unit/.build -- docs
|
||||
cmake --install xo-unit/.build # if docs built, installs to $PREFIX/share/doc/xo_unit/html
|
||||
|
||||
Supported compilers
|
||||
-------------------
|
||||
|
||||
* developed with gcc 12.3.0 and gcc 13.2.0; github CI using gcc 11.4.0 (asof March 2024)
|
||||
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@ Context
|
|||
+----------------+---------------+
|
||||
| scaled_unit |
|
||||
+--------------------------------+
|
||||
|cYEL natural_unit |
|
||||
|cBLU natural_unit |
|
||||
+--------------------------------+
|
||||
| bpu |
|
||||
+----------------+ |
|
||||
|
|
|
|||
|
|
@ -3,10 +3,16 @@
|
|||
Quantity
|
||||
========
|
||||
|
||||
Dimensioned quantity with compile-time unit checking/conversion
|
||||
|
||||
Context
|
||||
-------
|
||||
|
||||
.. ditaa::
|
||||
:--scale: 0.85
|
||||
|
||||
+----------------+---------------+
|
||||
|cYEL quantity | xquantity |
|
||||
|cBLU quantity | xquantity |
|
||||
+----------------+---------------+
|
||||
| scaled_unit |
|
||||
+--------------------------------+
|
||||
|
|
@ -36,15 +42,37 @@ Quantity
|
|||
|
||||
rectangle constexpr #e0f0ff {
|
||||
|
||||
object unit<<natural_unit>>
|
||||
object unit<<scaled_unit>>
|
||||
unit : is_natural() = true
|
||||
|
||||
qty1 o-- unit : s_unit (static constexpr)
|
||||
qty1 o-- unit : s_scaled_unit (static constexpr)
|
||||
|
||||
}
|
||||
|
||||
Arithmetic on :doc:`xo::qty::quantity<quantity-reference>` instances
|
||||
does *not* use ``xo::qty::quantity::s_scaled_unit`` at runtime;
|
||||
instead gets everything it needs at compile time.
|
||||
- Arithmetic on :doc:`xo::qty::quantity<quantity-reference>` instances
|
||||
does *not* use ``xo::qty::quantity::s_scaled_unit`` at runtime;
|
||||
instead gets everything it needs at compile time.
|
||||
|
||||
- The :code:`xo::qty::quantity` template takes a :doc:`xo::qty::scaled_unit<scaled-unit-class>` instance,
|
||||
but only accepts values with :code:`xo::qty::scaled_unit::is_natural() == true`.
|
||||
|
||||
This accomodation (instead of requiring a :doc:`xo::qty::natural_unit<natural-unit-class>` instance
|
||||
is to make possible code like this possible:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
#include "xo/unit/quantity.hpp"
|
||||
|
||||
using namespace xo::qty;
|
||||
|
||||
quantity<u::meter / u::second> x;
|
||||
quantity<u::meter * u::mter> y;
|
||||
|
||||
while rejecting attempt to mix multiple scales in the same quantity value:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
quantity<u::meter * u::millimeter> x; // will not compile
|
||||
|
||||
Class
|
||||
-----
|
||||
|
|
@ -81,19 +109,16 @@ Assignment
|
|||
----------
|
||||
|
||||
.. doxygengroup:: quantity-assignment
|
||||
:content-only:
|
||||
|
||||
Access Methods
|
||||
--------------
|
||||
|
||||
.. doxygengroup:: quantity-access-methods
|
||||
:content-only:
|
||||
|
||||
Constants
|
||||
---------
|
||||
|
||||
.. doxygengroup:: quantity-constants
|
||||
:content-only:
|
||||
|
||||
Conversion Methods
|
||||
------------------
|
||||
|
|
@ -101,13 +126,11 @@ Conversion Methods
|
|||
Amount-preserving conversion to quantities with different units and/or representation.
|
||||
|
||||
.. doxygengroup:: quantity-unit-conversion
|
||||
:content-only:
|
||||
|
||||
Arithmetic
|
||||
----------
|
||||
Arithmetic Operators
|
||||
--------------------
|
||||
|
||||
.. doxygengroup:: quantity-arithmetic
|
||||
:content-only:
|
||||
.. doxygengroup:: quantity-operators
|
||||
|
||||
Support methods for arithmetic operations
|
||||
|
||||
|
|
|
|||
|
|
@ -1,9 +1,18 @@
|
|||
.. _quantity_factoryfunctions:
|
||||
|
||||
Quantity Factory Functions
|
||||
==========================
|
||||
|
||||
Convenience functions for creating quantities with compile-time units
|
||||
|
||||
Context
|
||||
-------
|
||||
|
||||
.. ditaa::
|
||||
:--scale: 0.85
|
||||
|
||||
+----------------+---------------+
|
||||
|cYEL quantity | xquantity |
|
||||
|cBLU quantity | xquantity |
|
||||
+----------------+---------------+
|
||||
| scaled_unit |
|
||||
+--------------------------------+
|
||||
|
|
@ -21,8 +30,8 @@
|
|||
.. toctree::
|
||||
:maxdepth: 2
|
||||
|
||||
Quantity Factory Functions
|
||||
==========================
|
||||
Introduction
|
||||
------------
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
|
|
|
|||
|
|
@ -4,9 +4,10 @@ Quantity Reference
|
|||
==================
|
||||
|
||||
.. ditaa::
|
||||
:--scale: 0.85
|
||||
|
||||
+----------------+---------------+
|
||||
|cYEL quantity | xquantity |
|
||||
|cBLU quantity | xquantity |
|
||||
+----------------+---------------+
|
||||
| scaled_unit |
|
||||
+--------------------------------+
|
||||
|
|
@ -27,3 +28,4 @@ Quantity Reference
|
|||
quantity-class
|
||||
quantity-factoryfunctions
|
||||
quantity-unitvars
|
||||
quantity-source-code
|
||||
|
|
|
|||
11
docs/quantity-source-code.rst
Normal file
11
docs/quantity-source-code.rst
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
.. _quantity-source-code:
|
||||
|
||||
Quantity Source Code
|
||||
====================
|
||||
|
||||
github: `quantity.hpp @github.com`_
|
||||
|
||||
.. _`quantity.hpp @github.com`: https://github.com/rconybea/xo-unit/blob/main/include/xo/unit/quantity.hpp
|
||||
|
||||
.. literalinclude:: ../include/xo/unit/quantity.hpp
|
||||
:language: c++
|
||||
|
|
@ -1,11 +1,35 @@
|
|||
.. _quantity-unitvars:
|
||||
|
||||
.. toctree::
|
||||
:maxdepth: 2
|
||||
|
||||
Quantity Unit Variables
|
||||
=======================
|
||||
|
||||
Built-in unit quantities
|
||||
|
||||
Context
|
||||
-------
|
||||
|
||||
.. ditaa::
|
||||
:--scale: 0.85
|
||||
|
||||
+----------------+---------------+
|
||||
|cBLU quantity | xquantity |
|
||||
+----------------+---------------+
|
||||
| scaled_unit |
|
||||
+--------------------------------+
|
||||
| natural_unit |
|
||||
+--------------------------------+
|
||||
| bpu |
|
||||
+----------------+ |
|
||||
| bu_store | |
|
||||
+----------------+---------------+
|
||||
| basis_unit |
|
||||
+--------------------------------+
|
||||
| dimension |
|
||||
+--------------------------------+
|
||||
|
||||
Introduction
|
||||
------------
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
#include <xo/unit/quantity.hpp>
|
||||
|
|
@ -28,11 +52,16 @@ Can use these to assemble unit quantities with compound dimensions
|
|||
|
||||
Mass
|
||||
----
|
||||
.. doxygenvariable:: xo::qty::qty::picogram
|
||||
.. doxygenvariable:: xo::qty::qty::nanogram
|
||||
.. doxygenvariable:: xo::qty::qty::microgram
|
||||
.. doxygenvariable:: xo::qty::qty::milligram
|
||||
.. doxygenvariable:: xo::qty::qty::gram
|
||||
.. doxygenvariable:: xo::qty::qty::kilogram
|
||||
.. doxygenvariable:: xo::qty::qty::tonne
|
||||
.. doxygenvariable:: xo::qty::qty::kilotonne
|
||||
.. doxygenvariable:: xo::qty::qty::megatonne
|
||||
.. doxygenvariable:: xo::qty::qty::gigatonne
|
||||
|
||||
Distance
|
||||
--------
|
||||
|
|
@ -46,20 +75,24 @@ Distance
|
|||
.. doxygenvariable:: xo::qty::qty::gigameter
|
||||
.. doxygenvariable:: xo::qty::qty::lightsecond
|
||||
.. doxygenvariable:: xo::qty::qty::astronomicalunit
|
||||
.. doxygenvariable:: xo::qty::qty::inch
|
||||
.. doxygenvariable:: xo::qty::qty::foot
|
||||
.. doxygenvariable:: xo::qty::qty::yard
|
||||
.. doxygenvariable:: xo::qty::qty::mile
|
||||
|
||||
Time
|
||||
----
|
||||
.. notyet .. doxygenvariable:: xo::qty::qty::picosecond
|
||||
.. notyet .. doxygenvariable:: xo::qty::qty::nanosecond
|
||||
.. notyet .. doxygenvariable:: xo::qty::qty::microsecond
|
||||
.. notyet .. doxygenvariable:: xo::qty::qty::millisecond
|
||||
.. doxygenvariable:: xo::qty::qty::picosecond
|
||||
.. doxygenvariable:: xo::qty::qty::nanosecond
|
||||
.. doxygenvariable:: xo::qty::qty::microsecond
|
||||
.. doxygenvariable:: xo::qty::qty::millisecond
|
||||
.. doxygenvariable:: xo::qty::qty::second
|
||||
.. doxygenvariable:: xo::qty::qty::minute
|
||||
.. doxygenvariable:: xo::qty::qty::hour
|
||||
.. doxygenvariable:: xo::qty::qty::day
|
||||
.. notyet doxygenvariable:: xo::qty::qty::week
|
||||
.. notyet doxygenvariable:: xo::qty::qty::month
|
||||
.. notyet doxygenvariable:: xo::qty::qty::year
|
||||
.. notyet doxygenvariable:: xo::qty::qty::year250
|
||||
.. notyet doxygenvariable:: xo::qty::qty::year360
|
||||
.. notyet doxygenvariable:: xo::qty::qty::year365
|
||||
.. doxygenvariable:: xo::qty::qty::week
|
||||
.. doxygenvariable:: xo::qty::qty::month
|
||||
.. doxygenvariable:: xo::qty::qty::year
|
||||
.. doxygenvariable:: xo::qty::qty::year250
|
||||
.. doxygenvariable:: xo::qty::qty::year360
|
||||
.. doxygenvariable:: xo::qty::qty::year365
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ Context
|
|||
+----------------+----------------+
|
||||
| quantity | xquantity |
|
||||
+----------------+----------------+
|
||||
|cYEL scaled_unit |
|
||||
|cBLU scaled_unit |
|
||||
+---------------------------------+
|
||||
| natural_unit |
|
||||
+---------------------------------+
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@
|
|||
Scaled Unit Constants
|
||||
=====================
|
||||
|
||||
Built-in unit constants.
|
||||
Built-in unit constants, for use with conversions
|
||||
|
||||
Context
|
||||
-------
|
||||
|
|
@ -14,7 +14,7 @@ Context
|
|||
+----------------+---------------+
|
||||
| quantity | xquantity |
|
||||
+----------------+---------------+
|
||||
|cYEL scaled_unit |
|
||||
|cBLU scaled_unit |
|
||||
+--------------------------------+
|
||||
| natural_unit |
|
||||
+--------------------------------+
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ A dimensionless multiple of a :doc:`natural_unit<natural-unit-class>`
|
|||
+----------------+---------------+
|
||||
| quantity | xquantity |
|
||||
+----------------+---------------+
|
||||
|cYEL scaled_unit |
|
||||
|cBLU scaled_unit |
|
||||
+--------------------------------+
|
||||
| natural_unit |
|
||||
+--------------------------------+
|
||||
|
|
|
|||
9
docs/ubuntu-github-workflow.rst
Normal file
9
docs/ubuntu-github-workflow.rst
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
.. ubuntu-github-workflow:
|
||||
|
||||
Ubuntu Github Workflow
|
||||
======================
|
||||
|
||||
Instructions for build starting on a stock ubuntu platform (see `xo-unit/.github/workflows/ubuntu_main.yml`)
|
||||
|
||||
.. literalinclude:: ../.github/workflows/ubuntu-main.yml
|
||||
:language: yaml
|
||||
|
|
@ -3,13 +3,73 @@
|
|||
Xquantity
|
||||
=========
|
||||
|
||||
Polymorphic dimensioned quantity with runtime unit checking/conversion
|
||||
|
||||
Context
|
||||
-------
|
||||
|
||||
.. ditaa::
|
||||
:--scale: 0.85
|
||||
|
||||
+----------------+---------------+
|
||||
| quantity |cBLU xquantity |
|
||||
+----------------+---------------+
|
||||
| scaled_unit |
|
||||
+--------------------------------+
|
||||
| natural_unit |
|
||||
+--------------------------------+
|
||||
| bpu |
|
||||
+----------------+ |
|
||||
| bu_store | |
|
||||
+----------------+---------------+
|
||||
| basis_unit |
|
||||
+--------------------------------+
|
||||
| dimension |
|
||||
+--------------------------------+
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
#include <xo/unit/xquantity.hpp>
|
||||
|
||||
.. uml::
|
||||
:scale: 99%
|
||||
:align: center
|
||||
|
||||
allowmixing
|
||||
|
||||
object qty1<<xquantity>>
|
||||
qty1 : scale = 1.23
|
||||
qty1 : unit = unit
|
||||
|
||||
object unit<<scaled_unit>>
|
||||
unit : is_natural() = true
|
||||
|
||||
qty1 o-- unit
|
||||
|
||||
|
||||
Class
|
||||
-----
|
||||
|
||||
Class with run-time unit representation.
|
||||
|
||||
.. doxygenclass:: xo::qty::xquantity
|
||||
|
||||
Member Variables
|
||||
----------------
|
||||
|
||||
.. doxygengroup:: xquantity-instance-vars
|
||||
|
||||
Constructors
|
||||
------------
|
||||
|
||||
.. doxygengroup:: xquantity-ctors
|
||||
|
||||
Access Methods
|
||||
--------------
|
||||
|
||||
.. doxygengroup:: xquantity-access-methods
|
||||
|
||||
General Methods
|
||||
---------------
|
||||
|
||||
.. doxygengroup:: xquantity-methods
|
||||
|
|
|
|||
|
|
@ -4,9 +4,10 @@ Xquantity Reference
|
|||
===================
|
||||
|
||||
.. ditaa::
|
||||
:--scale: 0.85
|
||||
|
||||
+----------------+---------------+
|
||||
| quantity |cYEL xquantity |
|
||||
| quantity |cBLU xquantity |
|
||||
+----------------+---------------+
|
||||
| scaled_unit |
|
||||
+--------------------------------+
|
||||
|
|
|
|||
|
|
@ -7,3 +7,4 @@ add_subdirectory(ex6)
|
|||
add_subdirectory(ex7)
|
||||
add_subdirectory(ex8)
|
||||
add_subdirectory(ex_su)
|
||||
add_subdirectory(ex_qty)
|
||||
|
|
|
|||
|
|
@ -8,11 +8,12 @@ int
|
|||
main () {
|
||||
using namespace xo::qty;
|
||||
namespace u = xo::qty::u;
|
||||
//namespace q = xo::qty::qty;
|
||||
using namespace std;
|
||||
|
||||
xquantity qty1(7, u::foot);
|
||||
xquantity qty2(6.0, u::inch);
|
||||
constexpr xquantity qty1(7, u::foot);
|
||||
constexpr xquantity qty2(6.0, u::inch);
|
||||
|
||||
// constexpr not supported for xquantity addition
|
||||
xquantity qty3 = qty1 + qty2;
|
||||
|
||||
cerr << "qty1: " << qty1 << endl;
|
||||
|
|
@ -24,6 +25,13 @@ main () {
|
|||
|
||||
/* 2286mm */
|
||||
cerr << "res: " << res << endl;
|
||||
|
||||
/* 12 */
|
||||
xquantity qty4 = qty1 / qty2;
|
||||
|
||||
auto res2 = qty4 + 4;
|
||||
|
||||
cerr << "res2: " << res << endl;
|
||||
}
|
||||
|
||||
/** end ex8.cpp */
|
||||
|
|
|
|||
12
example/ex_qty/CMakeLists.txt
Normal file
12
example/ex_qty/CMakeLists.txt
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
# xo-unit/example/ex_qty/CMakeLists.txt
|
||||
|
||||
set(SELF_EXE xo_unit_ex_qty)
|
||||
set(SELF_SRCS ex_qty.cpp)
|
||||
|
||||
if (XO_ENABLE_EXAMPLES)
|
||||
xo_add_executable(${SELF_EXE} ${SELF_SRCS})
|
||||
xo_self_headeronly_dependency(${SELF_EXE} xo_unit)
|
||||
xo_dependency(${SELF_EXE} xo_flatstring)
|
||||
endif()
|
||||
|
||||
# end CMakeLists.txt
|
||||
18
example/ex_qty/ex_qty.cpp
Normal file
18
example/ex_qty/ex_qty.cpp
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
/* @file ex_qty.cpp */
|
||||
|
||||
#include "xo/unit/quantity.hpp"
|
||||
#include "xo/unit/quantity_iostream.hpp"
|
||||
|
||||
using namespace std;
|
||||
|
||||
int
|
||||
main() {
|
||||
using namespace xo::qty;
|
||||
namespace u = xo::qty::u;
|
||||
|
||||
static_assert(u::meter.n_bpu() == 1);
|
||||
|
||||
//constexpr auto q = qty::meters(2) + u::meter;
|
||||
}
|
||||
|
||||
/* end ex_qty.cpp */
|
||||
|
|
@ -399,7 +399,7 @@ namespace xo {
|
|||
}
|
||||
///@}
|
||||
|
||||
/** @defgroup quantity-arithmetic **/
|
||||
/** @defgroup mpl-quantity-arithmetic **/
|
||||
///@{
|
||||
/** @brief add quantity in-place
|
||||
*
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@
|
|||
#include "quantity_ops.hpp"
|
||||
#include "natural_unit.hpp"
|
||||
#include "scaled_unit.hpp"
|
||||
#include "scaled_unit_concept.hpp"
|
||||
|
||||
namespace xo {
|
||||
namespace qty {
|
||||
|
|
@ -70,22 +71,40 @@ namespace xo {
|
|||
|
||||
/** @defgroup quantity-access-methods quantity access methods **/
|
||||
///@{
|
||||
|
||||
/** @brief value of @c scale_ in quantity representing amount (@c scale_ * @c s_unit) **/
|
||||
constexpr const repr_type & scale() const { return scale_; }
|
||||
|
||||
/** @brief s_unit in quantity representing amount (@c scale_ * @c s_unit) **/
|
||||
constexpr const unit_type & unit() const { return s_scaled_unit; }
|
||||
|
||||
/** @brief true iff this quantity represents a dimensionless value **/
|
||||
constexpr bool is_dimensionless() const {
|
||||
static constexpr bool is_dimensionless() {
|
||||
return s_scaled_unit.is_dimensionless();
|
||||
}
|
||||
///@}
|
||||
|
||||
// unit_qty
|
||||
// zero_qty
|
||||
/** abbreviated suffix for quantities with this unit **/
|
||||
constexpr nu_abbrev_type abbrev() const {
|
||||
return s_scaled_unit.natural_unit_.abbrev();
|
||||
}
|
||||
|
||||
///@}
|
||||
|
||||
/** @defgroup quantity-arithmetic-support **/
|
||||
///@{
|
||||
|
||||
/** create unit quantity with same unit as @c this **/
|
||||
constexpr
|
||||
auto unit_qty() const {
|
||||
return quantity<s_scaled_unit, repr_type>(1);
|
||||
}
|
||||
|
||||
/** create zero quantity with same unit as @c this **/
|
||||
constexpr
|
||||
auto zero_qty() const {
|
||||
return quantity<s_scaled_unit, repr_type>(0);
|
||||
}
|
||||
|
||||
constexpr
|
||||
auto reciprocal() const {
|
||||
return quantity<s_scaled_unit.reciprocal(),
|
||||
|
|
@ -159,6 +178,9 @@ namespace xo {
|
|||
}
|
||||
///@}
|
||||
|
||||
/** @addtogroup quantity-arithmetic-support **/
|
||||
///@{
|
||||
|
||||
/** create quantity representing this amount multiplied by dimensionless value @p x
|
||||
*
|
||||
* @pre x must be an arithmetic type such as @c int or @c double
|
||||
|
|
@ -171,15 +193,36 @@ namespace xo {
|
|||
return quantity<s_scaled_unit, r_repr_type>(x * this->scale_);
|
||||
}
|
||||
|
||||
// divide_by
|
||||
// divide_into
|
||||
/** create quantity representing this quantity divided by dimensionless value @p x
|
||||
*
|
||||
* @pre x must be an arithmetic type such as @c int or @c double
|
||||
**/
|
||||
template <typename Dimensionless>
|
||||
requires std::is_arithmetic_v<Dimensionless>
|
||||
constexpr auto divide_by(Dimensionless x) const {
|
||||
using r_repr_type = std::common_type_t<repr_type, Dimensionless>;
|
||||
|
||||
// divide
|
||||
// add
|
||||
// subtract
|
||||
return quantity<s_scaled_unit, r_repr_type>(this->scale_ / x);
|
||||
}
|
||||
|
||||
/** create quantity representing dimensionless value @x divided by this quantity
|
||||
*
|
||||
* @pre x must be an arithmetic type such as @c int or @c double
|
||||
**/
|
||||
template <typename Dimensionless>
|
||||
requires std::is_arithmetic_v<Dimensionless>
|
||||
constexpr auto divide_into(Dimensionless x) const {
|
||||
using r_repr_type = std::common_type_t<repr_type, Dimensionless>;
|
||||
|
||||
return quantity(static_cast<r_repr_type>(x) / this->scale_,
|
||||
s_scaled_unit.reciprocal());
|
||||
}
|
||||
|
||||
///@}
|
||||
|
||||
/** @defgroup quantity-comparison-support **/
|
||||
///@{
|
||||
|
||||
/** compare two @c quantity instances, under three-way comparison **/
|
||||
template <typename Quantity2>
|
||||
static constexpr
|
||||
|
|
@ -188,18 +231,43 @@ namespace xo {
|
|||
|
||||
return x.scale() <=> y2.scale();
|
||||
}
|
||||
|
||||
///@}
|
||||
|
||||
// operator-
|
||||
// operator+=
|
||||
// operator-=
|
||||
/** @defgroup quantity-operators **/
|
||||
///@{
|
||||
|
||||
/** unary negation; preserves unit information **/
|
||||
quantity operator-() const {
|
||||
return quantity(-scale_);
|
||||
}
|
||||
|
||||
/** add @p y in-place, converting units if necessary **/
|
||||
template <typename Quantity2>
|
||||
constexpr
|
||||
quantity & operator+=(const Quantity2 & y) {
|
||||
quantity y2 = y.template rescale_ext<s_scaled_unit>();
|
||||
|
||||
this->scale_ += y2.scale();
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** subtract @p y in-place, converting units if necessary **/
|
||||
template <typename Quantity2>
|
||||
constexpr
|
||||
quantity & operator-=(const Quantity2 & y) {
|
||||
quantity y2 = y.template rescale_ext<s_scaled_unit>();
|
||||
|
||||
this->scale_ -= y2.scale();
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
// operator*=
|
||||
// operator/=
|
||||
|
||||
/** **/
|
||||
constexpr nu_abbrev_type abbrev() const {
|
||||
return s_scaled_unit.natural_unit_.abbrev();
|
||||
}
|
||||
///@}
|
||||
|
||||
/** @defgroup quantity-assignment quantity assignment operators **/
|
||||
///@{
|
||||
|
|
@ -429,6 +497,9 @@ namespace xo {
|
|||
return x.template with_repr<Repr2>();
|
||||
}
|
||||
|
||||
/** @addtogroup quantity-operators **/
|
||||
///@{
|
||||
|
||||
/** note: won't have constexpr result w/ fractional dimension until c++26 (when ::sqrt(), ::pow() are constexpr)
|
||||
**/
|
||||
template <typename Q1, typename Q2>
|
||||
|
|
@ -442,7 +513,27 @@ namespace xo {
|
|||
return detail::quantity_util::multiply(x, y);
|
||||
}
|
||||
|
||||
/** note: won't have constexpr result w/ fractional dimension until c++26 (when ::sqrt(), ::pow() are constexpr)
|
||||
/** note: does not require unit scaling, so constexpr with c++23 **/
|
||||
template <typename Dimensionless, typename Quantity>
|
||||
requires std::is_arithmetic_v<Dimensionless> && quantity_concept<Quantity>
|
||||
constexpr auto
|
||||
operator* (const Quantity & x, Dimensionless y)
|
||||
{
|
||||
return x.scale_by(y);
|
||||
}
|
||||
|
||||
/** note: does not require unit scaling, so constexpr with c++23 **/
|
||||
template <typename Dimensionless, typename Quantity>
|
||||
requires std::is_arithmetic_v<Dimensionless> && quantity_concept<Quantity>
|
||||
constexpr auto
|
||||
operator* (Dimensionless x, const Quantity & y)
|
||||
{
|
||||
return y.scale_by(x);
|
||||
}
|
||||
|
||||
/** divide quantity @p x by quantity @p y.
|
||||
*
|
||||
* note: won't have constexpr result w/ fractional dimension until c++26 (when ::sqrt(), ::pow() are constexpr)
|
||||
**/
|
||||
template <typename Q1, typename Q2>
|
||||
requires (quantity_concept<Q1>
|
||||
|
|
@ -455,7 +546,30 @@ namespace xo {
|
|||
return detail::quantity_util::divide(x, y);
|
||||
}
|
||||
|
||||
/** note: won't have constexpr result w/ fractional dimension until c++26 (when ::sqrt(), ::pow() are constexpr)
|
||||
/** divide quantity @p x by dimensionless value @p y **/
|
||||
template <typename Dimensionless, typename Quantity>
|
||||
requires std::is_arithmetic_v<Dimensionless> && quantity_concept<Quantity>
|
||||
constexpr auto
|
||||
operator/ (const Quantity & x, Dimensionless y)
|
||||
{
|
||||
return x.divide_by(y);
|
||||
}
|
||||
|
||||
/** divide dimensionless value @p x by quantity @p y **/
|
||||
template <typename Dimensionless, typename Quantity>
|
||||
requires std::is_arithmetic_v<Dimensionless> && quantity_concept<Quantity>
|
||||
constexpr auto
|
||||
operator/ (Dimensionless x, const Quantity & y)
|
||||
{
|
||||
return y.divide_into(x);
|
||||
}
|
||||
|
||||
/** add quantity @p y to quantity @p x. Result will have the same units as @p x.
|
||||
* Representation will be the widest of {@c x::repr_type, @c y::repr_type}.
|
||||
*
|
||||
* note: won't have constexpr result w/ fractional dimension until c++26 (when ::sqrt(), ::pow() are constexpr)
|
||||
*
|
||||
* @pre @p x and @p y expected to have consistent dimensions
|
||||
**/
|
||||
template <typename Q1, typename Q2>
|
||||
requires (quantity_concept<Q1>
|
||||
|
|
@ -468,7 +582,46 @@ namespace xo {
|
|||
return detail::quantity_util::add(x, y);
|
||||
}
|
||||
|
||||
/** note: won't have constexpr result w/ fractional dimension until c++26 (when ::sqrt(), ::pow() are constexpr)
|
||||
/** subtract an arithmetic value from a dimensionless quantity **/
|
||||
template <typename Quantity,
|
||||
typename Dimensionless>
|
||||
requires (quantity_concept<Quantity>
|
||||
&& Quantity::is_dimensionless()
|
||||
&& std::is_arithmetic_v<Dimensionless>)
|
||||
constexpr auto
|
||||
operator+ (const Quantity & x, Dimensionless y)
|
||||
{
|
||||
using repr_type = std::common_type_t<typename Quantity::repr_type, Dimensionless>;
|
||||
|
||||
auto xp = static_cast<repr_type>(x.scale());
|
||||
auto yp = static_cast<repr_type>(y);
|
||||
|
||||
return xp + yp;
|
||||
}
|
||||
|
||||
/** subtract a dimensionless quantity from an arithmetic value **/
|
||||
template <typename Dimensionless,
|
||||
typename Quantity>
|
||||
requires (std::is_arithmetic_v<Dimensionless>
|
||||
&& quantity_concept<Quantity>
|
||||
&& Quantity::is_dimensionless())
|
||||
constexpr auto
|
||||
operator+ (Dimensionless x, const Quantity & y)
|
||||
{
|
||||
using repr_type = std::common_type_t<Dimensionless, typename Quantity::repr_type>;
|
||||
|
||||
auto xp = static_cast<repr_type>(x);
|
||||
auto yp = static_cast<repr_type>(y.scale());
|
||||
|
||||
return xp + yp;
|
||||
}
|
||||
|
||||
/** subtract quantity @p y from quantity @p x. Result will have the same units as @p x.
|
||||
* Representation will be the widest of {@c x::repr_type, @c y::repr_type}
|
||||
*
|
||||
* note: won't have constexpr result w/ fractional dimension until c++26 (when ::sqrt(), ::pow() are constexpr)
|
||||
*
|
||||
* @pre @p x and @p y expected to have consistent dimensions
|
||||
**/
|
||||
template <typename Q1, typename Q2>
|
||||
requires (quantity_concept<Q1>
|
||||
|
|
@ -481,6 +634,8 @@ namespace xo {
|
|||
return detail::quantity_util::subtract(x, y);
|
||||
}
|
||||
|
||||
///@}
|
||||
|
||||
namespace qty {
|
||||
// ----- mass -----
|
||||
|
||||
|
|
@ -528,22 +683,26 @@ namespace xo {
|
|||
namespace qty {
|
||||
// ----- mass constants ----
|
||||
|
||||
/** @brief a quantity representing 1 picogram of mass, with compile-time unit representation **/
|
||||
/** a quantity representing 1 picogram of mass, with compile-time unit representation **/
|
||||
static constexpr auto picogram = picograms(1);
|
||||
/** @brief a quantity representing 1 nanogram of mass, with compile-time unit representation **/
|
||||
/** a quantity representing 1 nanogram of mass, with compile-time unit representation **/
|
||||
static constexpr auto nanogram = nanograms(1);
|
||||
/** @brief a quantity representing 1 microgram of mass, with compile-time unit representation **/
|
||||
/** a quantity representing 1 microgram of mass, with compile-time unit representation **/
|
||||
static constexpr auto microgram = micrograms(1);
|
||||
/** @brief a quantity representing 1 milligram of mass, with compile-time unit representation **/
|
||||
/** a quantity representing 1 milligram of mass, with compile-time unit representation **/
|
||||
static constexpr auto milligram = milligrams(1);
|
||||
/** @brief a quantity representing 1 gram of mass, with compile-time unit representation **/
|
||||
/** a quantity representing 1 gram of mass, with compile-time unit representation **/
|
||||
static constexpr auto gram = grams(1);
|
||||
/** @brief a quantity representing 1 kilogram of mass, with compile-time unit representation **/
|
||||
/** a quantity representing 1 kilogram of mass, with compile-time unit representation **/
|
||||
static constexpr auto kilogram = kilograms(1);
|
||||
/** @brief a quantity representing 1 metric tonne of mass, with compile-time unit representation **/
|
||||
/** a quantity representing 1 metric tonne of mass, with compile-time unit representation **/
|
||||
static constexpr auto tonne = tonnes(1);
|
||||
/** @brief a quantity representing 1 metric kilotonne of mass, with compile-time unit representation **/
|
||||
/** a quantity representing 1 metric kilotonne of mass, with compile-time unit representation **/
|
||||
static constexpr auto kilotonne = kilotonnes(1);
|
||||
/** a quantity representing 1 metric megatonne of mass, with compile-time unit representation **/
|
||||
static constexpr auto megatonne = megatonnes(1);
|
||||
/** a quantity representing 1 metric gigatonne of mass, with compile-time unit representation **/
|
||||
static constexpr auto gigatonne = gigatonnes(1);
|
||||
} /*namespace qty*/
|
||||
|
||||
namespace qty {
|
||||
|
|
@ -618,22 +777,37 @@ namespace xo {
|
|||
static constexpr auto nanometer = nanometers(1);
|
||||
/** a quantity representing 1 micrometer of distance, with compile-time unit representation **/
|
||||
static constexpr auto micrometer = micrometers(1);
|
||||
/** @brief a quantity representing 1 millimeter of distance, with compile-time unit representation **/
|
||||
/** a quantity representing 1 millimeter of distance, with compile-time unit representation **/
|
||||
static constexpr auto millimeter = millimeters(1);
|
||||
/** @brief a quantity representing 1 meter of distance, with compile-time unit representation **/
|
||||
/** a quantity representing 1 meter of distance, with compile-time unit representation **/
|
||||
static constexpr auto meter = meters(1);
|
||||
/** @brief a quantity representing 1 kilometer of distance, with compile-time unit representation **/
|
||||
/** a quantity representing 1 kilometer of distance, with compile-time unit representation **/
|
||||
static constexpr auto kilometer = kilometers(1);
|
||||
/** @brief a quantity representing 1 megameter of distance, with compile-time unit representation **/
|
||||
/** a quantity representing 1 megameter of distance, with compile-time unit representation **/
|
||||
static constexpr auto megameter = megameters(1);
|
||||
/** @brief a quantity representing 1 gigameter of distance, with compile-time unit representation **/
|
||||
/** a quantity representing 1 gigameter of distance, with compile-time unit representation **/
|
||||
static constexpr auto gigameter = gigameters(1);
|
||||
|
||||
/** @brief a quantity representing exactly 1 lightsecond of distance, with compile-time unit representation **/
|
||||
/** a quantity representing exactly 1 lightsecond of distance,
|
||||
* with compile-time unit representation
|
||||
**/
|
||||
static constexpr auto lightsecond = lightseconds(1);
|
||||
/** @brief a quantity representing exactly 1 astronomical unit of distance, with compile-time unit representation **/
|
||||
/** a quantity representing exactly 1 astronomical unit of distance,
|
||||
* with compile-time unit representation
|
||||
**/
|
||||
static constexpr auto astronomicalunit = astronomicalunits(1);
|
||||
|
||||
/** a quantity representing 1 inch of distance, with compile-time unit operations **/
|
||||
static constexpr auto inch = inches(1);
|
||||
|
||||
/** a quantity representing 1 foot of distance, with compile-time unit operations **/
|
||||
static constexpr auto foot = feet(1);
|
||||
|
||||
/** a quantity representing 1 yard of distance, with compile-time unit operations **/
|
||||
static constexpr auto yard = yards(1);
|
||||
|
||||
/** a quantity representing 1 mile of distance, with compile-time unit operations **/
|
||||
static constexpr auto mile = miles(1);
|
||||
} /*namespace qty*/
|
||||
|
||||
namespace qty {
|
||||
|
|
@ -707,17 +881,48 @@ namespace xo {
|
|||
namespace qty {
|
||||
// ----- time constants ----
|
||||
|
||||
/** @brief a quantity representing 1 second of time, with compile-time unit representation **/
|
||||
/** a quantity representing 1 picosecond of time, with compile-time unit representation **/
|
||||
static constexpr auto picosecond = picoseconds(1);
|
||||
|
||||
/** a quantity representing 1 nanosecond of time, with compile-time unit representation **/
|
||||
static constexpr auto nanosecond = nanoseconds(1);
|
||||
|
||||
/** a quantity representing 1 microsecond of time, with compile-time unit representation **/
|
||||
static constexpr auto microsecond = microseconds(1);
|
||||
|
||||
/** a quantity representing 1 millisecond of time, with compile-time unit representation **/
|
||||
static constexpr auto millisecond = milliseconds(1);
|
||||
|
||||
/** a quantity representing 1 second of time, with compile-time unit representation **/
|
||||
static constexpr auto second = seconds(1);
|
||||
|
||||
/** @brief a quantity representing 1 minute of time, with compile-time unit representation **/
|
||||
/** a quantity representing 1 minute of time, with compile-time unit representation **/
|
||||
static constexpr auto minute = minutes(1);
|
||||
|
||||
/** @brief a quantity representing 1 hour of time, with compile-time unit representation **/
|
||||
/** a quantity representing 1 hour of time, with compile-time unit representation **/
|
||||
static constexpr auto hour = hours(1);
|
||||
|
||||
/** @brief a quantity representing 1 day of time (exactly 24 hours), with compile-time unit representation **/
|
||||
/** a quantity representing 1 day of time (exactly 24 hours), with compile-time unit representation **/
|
||||
static constexpr auto day = days(1);
|
||||
|
||||
/** a quantity representing 1 week of time (7 24-hour days), with compile-time unit representation **/
|
||||
static constexpr auto week = weeks(1);
|
||||
|
||||
/** a quantity representing 1 month of time (30 24-hour days), with compile-time unit representation **/
|
||||
static constexpr auto month = months(1);
|
||||
|
||||
/** a quantity representing 1 year of time (365.25 24-hour days), with compile-time unit representation **/
|
||||
static constexpr auto year = years(1);
|
||||
|
||||
/** a quantity representing 1 250-day year of time, with compile-time unit representation **/
|
||||
static constexpr auto year250 = year250s(1);
|
||||
|
||||
/** a quantity representing 1 360-day year of time, with compile-time unit representation **/
|
||||
static constexpr auto year360 = year360s(1);
|
||||
|
||||
/** a quantity representing 1 365-day year of time, with compile-time unit representation **/
|
||||
static constexpr auto year365 = year365s(1);
|
||||
|
||||
} /*namespace qty*/
|
||||
|
||||
namespace qty {
|
||||
|
|
|
|||
|
|
@ -9,24 +9,45 @@
|
|||
|
||||
namespace xo {
|
||||
namespace qty {
|
||||
/** note: does not require unit scaling, so constexpr with c++23 **/
|
||||
template <typename Dimensionless, typename Quantity>
|
||||
requires std::is_arithmetic_v<Dimensionless> && quantity_concept<Quantity>
|
||||
/** @defgroup quantity-dimensionless-operators **/
|
||||
///@{
|
||||
|
||||
/** subtract an arithmetic value from a dimensionless quantity **/
|
||||
template <typename Quantity,
|
||||
typename Dimensionless>
|
||||
requires (quantity_concept<Quantity>
|
||||
&& Quantity::is_dimensionless()
|
||||
&& std::is_arithmetic_v<Dimensionless>)
|
||||
constexpr auto
|
||||
operator* (Dimensionless x, const Quantity & y)
|
||||
operator- (const Quantity & x, Dimensionless y)
|
||||
{
|
||||
return y.scale_by(x);
|
||||
using repr_type = std::common_type_t<typename Quantity::repr_type, Dimensionless>;
|
||||
|
||||
auto xp = static_cast<repr_type>(x.scale());
|
||||
auto yp = static_cast<repr_type>(y);
|
||||
|
||||
return xp - yp;
|
||||
}
|
||||
|
||||
/** note: does not require unit scaling, so constexpr with c++23 **/
|
||||
template <typename Dimensionless, typename Quantity>
|
||||
requires std::is_arithmetic_v<Dimensionless> && quantity_concept<Quantity>
|
||||
/** subtract a dimensionless quantity from an arithmetic value **/
|
||||
template <typename Dimensionless,
|
||||
typename Quantity>
|
||||
requires (std::is_arithmetic_v<Dimensionless>
|
||||
&& quantity_concept<Quantity>
|
||||
&& Quantity::is_dimensionless())
|
||||
constexpr auto
|
||||
operator* (const Quantity & x, Dimensionless y)
|
||||
operator- (Dimensionless x, const Quantity & y)
|
||||
{
|
||||
return x.scale_by(y);
|
||||
using repr_type = std::common_type_t<Dimensionless, typename Quantity::repr_type>;
|
||||
|
||||
auto xp = static_cast<repr_type>(x);
|
||||
auto yp = static_cast<repr_type>(y.scale());
|
||||
|
||||
return xp - yp;
|
||||
}
|
||||
|
||||
///@}
|
||||
|
||||
/** note: won't have constexpr result until c++26 (when ::sqrt(), ::pow() are constexpr)
|
||||
**/
|
||||
template <typename Quantity, typename Quantity2>
|
||||
|
|
|
|||
|
|
@ -44,6 +44,9 @@ namespace xo {
|
|||
/** @defgroup scaled-unit-access-methods scaled-unit access methods **/
|
||||
///@{
|
||||
|
||||
/** always true for scaled_unit **/
|
||||
static constexpr bool is_scaled_unit_type_v = true;
|
||||
|
||||
/** always true for scaled_unit **/
|
||||
constexpr bool is_scaled_unit_type() const { return true; }
|
||||
|
||||
|
|
|
|||
23
include/xo/unit/scaled_unit_concept.hpp
Normal file
23
include/xo/unit/scaled_unit_concept.hpp
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
/** @file scaled_unit_concept.hpp **/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <concepts>
|
||||
|
||||
namespace xo {
|
||||
namespace qty {
|
||||
template <typename ScaledUnit>
|
||||
concept scaled_unit_concept = requires(ScaledUnit su)
|
||||
{
|
||||
typename ScaledUnit::ratio_int_type;
|
||||
|
||||
{ su.is_scaled_unit_type() } -> std::same_as<bool>;
|
||||
{ su.is_natural() } -> std::same_as<bool>;
|
||||
{ su.is_dimensionless() } -> std::same_as<bool>;
|
||||
|
||||
} && ScaledUnit::is_scaled_unit_type_v;
|
||||
} /*namespace qty*/
|
||||
} /*namespace xo*/
|
||||
|
||||
|
||||
/** end scaled_unit_concept.hpp **/
|
||||
|
|
@ -18,7 +18,7 @@ namespace xo {
|
|||
*
|
||||
* Constexpr implementation, but units are explicitly represented:
|
||||
* @code
|
||||
* sizeof(Quantity2) > sizeof(Repr)
|
||||
* sizeof(xquantity) > sizeof(xquantity::repr_type)
|
||||
* @endcode
|
||||
*
|
||||
* Explicit unit representation allows introducing units at runtime,
|
||||
|
|
@ -32,22 +32,47 @@ namespace xo {
|
|||
* - Repr supports conversion from double.
|
||||
**/
|
||||
template <typename Repr = double,
|
||||
typename Int = std::int64_t,
|
||||
typename Int2x = detail::width2x_t<Int>>
|
||||
typename Int = std::int64_t>
|
||||
class xquantity {
|
||||
public:
|
||||
/** @defgroup xquantity-type-traits xquantity type traits **/
|
||||
///@{
|
||||
/** @brief runtime representation for this value's scale **/
|
||||
using repr_type = Repr;
|
||||
/** @brief runtime representation for this value's unit **/
|
||||
using unit_type = natural_unit<Int>;
|
||||
/** @brief type used for numerator and denominator in basis-unit scalefactor ratios **/
|
||||
using ratio_int_type = Int;
|
||||
using ratio_int2x_type = Int2x;
|
||||
/** @brief double-width type for numerator and denominator of intermediate
|
||||
* scalefactor ratios. Use to mitigate loss of precision during computation
|
||||
* of conversion factors between units with widely-differing magnitude
|
||||
**/
|
||||
using ratio_int2x_type = detail::width2x_t<typename unit_type::ratio_int_type>;
|
||||
///@}
|
||||
|
||||
public:
|
||||
/* zero, dimensionless */
|
||||
/** @defgroup xquantity-ctors xquantity constructors **/
|
||||
///@{
|
||||
|
||||
/** create dimensionless, zero quantity **/
|
||||
constexpr xquantity()
|
||||
: scale_{0}, unit_{natural_unit<Int>()} {}
|
||||
/** create quantity representing multiple of @p scale times @p unit **/
|
||||
constexpr xquantity(Repr scale,
|
||||
const natural_unit<Int> & unit)
|
||||
: scale_{scale}, unit_{unit} {}
|
||||
/** create quantity representing multiple of @p scale times @p unit.
|
||||
*
|
||||
* Collects outer scalefactors (if any) from @p unit,
|
||||
* so for example:
|
||||
*
|
||||
* @code
|
||||
* using namespace xo::qty;
|
||||
* xquantity q(123, u::meter * u::millimeter);
|
||||
*
|
||||
* q.scale() --> 0.123
|
||||
* @endcode
|
||||
**/
|
||||
constexpr xquantity(Repr scale,
|
||||
const scaled_unit<Int> & unit)
|
||||
:
|
||||
|
|
@ -59,17 +84,39 @@ namespace xo {
|
|||
),
|
||||
unit_{unit.natural_unit_} {}
|
||||
|
||||
///@}
|
||||
|
||||
static constexpr bool always_constexpr_unit = false;
|
||||
|
||||
/** @defgroup xquantity-access-methods xquantity access methods **/
|
||||
///@{
|
||||
|
||||
/** get member @ref scale_ **/
|
||||
constexpr const repr_type & scale() const { return scale_; }
|
||||
/** get member @ref unit_ **/
|
||||
constexpr const unit_type & unit() const { return unit_; }
|
||||
|
||||
/** true iff this quantity has no dimension **/
|
||||
constexpr bool is_dimensionless() const { return unit_.is_dimensionless(); }
|
||||
|
||||
/** return abbreviation for quantities with this unit **/
|
||||
constexpr nu_abbrev_type abbrev() const { return unit_.abbrev(); }
|
||||
|
||||
///@}
|
||||
|
||||
/** @defgroup xquantity-methods xquantity methods **/
|
||||
///@{
|
||||
|
||||
/** create unit quantity with same unit as @c this **/
|
||||
constexpr xquantity unit_qty() const { return xquantity(1, unit_); }
|
||||
|
||||
/** create zero quantity with same unit as @c this **/
|
||||
constexpr xquantity zero_qty() const { return xquantity(0, unit_); }
|
||||
|
||||
/** create quantity representing reciprocal of @c this **/
|
||||
constexpr xquantity reciprocal() const { return xquantity(1.0 / scale_, unit_.reciprocal()); }
|
||||
|
||||
/** create quantity representing the same value, but in units of @p unit2 **/
|
||||
constexpr
|
||||
auto rescale(const natural_unit<Int> & unit2) const {
|
||||
/* conversion factor from .unit -> unit2*/
|
||||
|
|
@ -86,24 +133,28 @@ namespace xo {
|
|||
}
|
||||
}
|
||||
|
||||
/** create quantity representing this value scaled by dimensionless mutliplier @p x **/
|
||||
template <typename Dimensionless>
|
||||
requires std::is_arithmetic_v<Dimensionless>
|
||||
constexpr auto scale_by(Dimensionless x) const {
|
||||
return xquantity(x * this->scale_, this->unit_);
|
||||
}
|
||||
|
||||
/** create quantity representing this value scaled by dimensionless multiplier @p 1/x **/
|
||||
template <typename Dimensionless>
|
||||
requires std::is_arithmetic_v<Dimensionless>
|
||||
constexpr auto divide_by(Dimensionless x) const {
|
||||
return xquantity(this->scale_ / x, this->unit_);
|
||||
}
|
||||
|
||||
/** create quantity representing dimensionless numerator @p x divided by this value **/
|
||||
template <typename Dimensionless>
|
||||
requires std::is_arithmetic_v<Dimensionless>
|
||||
constexpr auto divide_into(Dimensionless x) const {
|
||||
return xquantity(x / this->scale_, this->unit_.reciprocal());
|
||||
}
|
||||
|
||||
/** multiply two @c xquantity values, or a mixed (@c xquantity, @c quantity) pair **/
|
||||
template <typename Quantity2>
|
||||
static constexpr
|
||||
auto multiply(const xquantity & x, const Quantity2 & y) {
|
||||
|
|
@ -125,6 +176,7 @@ namespace xo {
|
|||
rr.natural_unit_);
|
||||
}
|
||||
|
||||
/** compute quotient @p x / @p y, where @p x and @p y are xquantities **/
|
||||
template <typename Quantity2>
|
||||
static constexpr
|
||||
auto divide(const xquantity & x, const Quantity2 & y) {
|
||||
|
|
@ -149,6 +201,7 @@ namespace xo {
|
|||
rr.natural_unit_);
|
||||
}
|
||||
|
||||
/** compute sum @p x + @p y, where @p x and @p y are xquantities **/
|
||||
template <typename Quantity2>
|
||||
static constexpr
|
||||
auto add(const xquantity & x, const Quantity2 & y) {
|
||||
|
|
@ -176,6 +229,7 @@ namespace xo {
|
|||
}
|
||||
}
|
||||
|
||||
/** compute difference @p x - @p y, where @p x and @p y are xquantities **/
|
||||
template <typename Quantity2>
|
||||
static constexpr
|
||||
auto subtract(const xquantity & x, const Quantity2 & y) {
|
||||
|
|
@ -203,6 +257,7 @@ namespace xo {
|
|||
}
|
||||
}
|
||||
|
||||
/** perform 3-way comparison between @c xquantity values @p x and @p y **/
|
||||
template <typename Quantity2>
|
||||
static constexpr
|
||||
auto compare(const xquantity & x, const Quantity2 & y) {
|
||||
|
|
@ -211,6 +266,10 @@ namespace xo {
|
|||
return x.scale() <=> y2.scale();
|
||||
}
|
||||
|
||||
///@}
|
||||
|
||||
/** @defgroup xquantity-operators xquantity operators **/
|
||||
///@{
|
||||
xquantity operator-() const {
|
||||
return xquantity(-scale_, unit_);
|
||||
}
|
||||
|
|
@ -228,16 +287,20 @@ namespace xo {
|
|||
*this = *this / x;
|
||||
return *this;
|
||||
}
|
||||
///@}
|
||||
|
||||
// TODO: operator+=, operator-=
|
||||
|
||||
constexpr nu_abbrev_type abbrev() const { return unit_.abbrev(); }
|
||||
|
||||
private:
|
||||
/** @brief quantity represents this multiple of a unit amount **/
|
||||
/** @defgroup xquantity-instance-vars **/
|
||||
///@{
|
||||
|
||||
/** quantity represents this multiple of a unit amount **/
|
||||
Repr scale_ = Repr{};
|
||||
/** @brief unit for this quantity **/
|
||||
/** unit for this quantity **/
|
||||
natural_unit<Int> unit_;
|
||||
|
||||
///@}
|
||||
}; /*xquantity*/
|
||||
|
||||
/** note: won't have constexpr result until c++26 (when ::sqrt(), ::pow() are constexpr)
|
||||
|
|
@ -273,6 +336,26 @@ namespace xo {
|
|||
return Q1::multiply(x, y);
|
||||
}
|
||||
|
||||
/** note: won't have constexpr result until c++26 (when ::sqrt(), ::pow() are constexpr)
|
||||
**/
|
||||
template <typename Quantity>
|
||||
requires quantity_concept<Quantity>
|
||||
constexpr auto
|
||||
operator* (double x, const Quantity & y)
|
||||
{
|
||||
return y.scale_by(x);
|
||||
}
|
||||
|
||||
/** note: won't have constexpr result until c++26 (when ::sqrt(), ::pow() are constexpr)
|
||||
**/
|
||||
template <typename Quantity>
|
||||
requires quantity_concept<Quantity>
|
||||
constexpr auto
|
||||
operator* (const Quantity & x, double y)
|
||||
{
|
||||
return x.scale_by(y);
|
||||
}
|
||||
|
||||
/** note: won't have constexpr result until c++26 (when ::sqrt(), ::pow() are constexpr)
|
||||
**/
|
||||
template <typename Quantity, typename Quantity2>
|
||||
|
|
@ -334,7 +417,8 @@ namespace xo {
|
|||
/** note: won't have constexpr result until c++26 (when ::sqrt(), ::pow() are constexpr)
|
||||
**/
|
||||
template <typename Quantity, typename Quantity2>
|
||||
requires quantity_concept<Quantity> && quantity_concept<Quantity2>
|
||||
requires (quantity_concept<Quantity>
|
||||
&& quantity_concept<Quantity2>)
|
||||
constexpr auto
|
||||
operator- (const Quantity & x, const Quantity2 & y)
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue