Skip to content

Commit

Permalink
xo-unit: tidy example ex6 + docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Rconybea committed May 4, 2024
1 parent 8d5fcda commit 17519e8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
25 changes: 17 additions & 8 deletions docs/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -259,10 +259,14 @@ with output:
Fractional dimension
--------------------

Fractional dimensions are supported; they work in the same way as familiar integral dimensions.
Fractional dimensions have limited support.
Prior to c++26 we can only support fractional dimensions with denominator 2,
such as powers -3/2, -1/2, +1/2, +3/2 etc.

Only caveat is that converting between fractional units with different scales creates a floating-point conversion factor,
which may incur loss of precision based on floating-point roundoff.
c++26 will enable support for support fractional dimensions involving other ratios,
by offering constexpr ``::pow()``

See ``xo-unit/examples/ex6`` for code below

.. code-block:: cpp
:linenos:
Expand All @@ -282,18 +286,23 @@ which may incur loss of precision based on floating-point roundoff.
/* 10% volatility over 30 days */
auto q2 = qty::volatility30d(0.1);
static_assert(q2.basis_power<dim::time, double> == 0.5);
auto sum = q1 + q2;
auto prod = q1 * q2;
static_assert(prod.basis_power<dim::time> == 1);
static_assert(sum.abbrev() == flatstring("yr360^(-1/2)"));
static_assert(prod.abbrev() == flatstring("yr360^-1"));
cerr << "q1: " << q1 << ", q2: " << q2 << ", q1+q2: " << sum << ", q1*q2" << prod << endl;
std::cerr << "q1: " << q1 << std::endl;
std::cerr << "q2: " << q2 << std::endl;
std::cerr << "q1+q2: " << sum << std::endl;
std::cerr << "q1*q2: " << prod << std::endl;
}
with output:

.. code-block::
q1: 0.2yr250^-(1/2), q2: 0.1mo^-(1/2), q1+q2: 0.488675yr250^(1/2), q1*q2: 0.057735yr250^-1
q1: 0.2yr360^(-1/2)
q2: 0.1mo^(-1/2)
q1+q2: 0.54641yr360^(-1/2)
q1*q2: 0.069282yr360^-1
3 changes: 0 additions & 3 deletions example/ex6/ex6.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#include "xo/unit/quantity.hpp"
#include "xo/unit/quantity_iostream.hpp"
#include "xo/flatstring/int128_iostream.hpp"
#include <iostream>

int
Expand All @@ -16,8 +15,6 @@ main () {
/* 10% volatility over 30 days */
auto q2 = q::volatility_30d(0.1);

//quantity<nu::volatility_360d> q2p = q2;

/* 10% volatility per 30 days
* ~ (10% * sqrt(360/30)) volatility over 360 days
* ~ (10% * 3.4641)
Expand Down

0 comments on commit 17519e8

Please sign in to comment.