implementing ratio<num,den,exp> which replaces ratio<num,den>
https://github.com/mpusz/units/issues/14 This "works", as in it passes all static and runtime tests. However quite a few of the tests have been "modified" to make them pass. Whether this is legitimate is debatable and should be the source of some thought / discussion. 1. many of the static tests and some of the runtime tests have had the input ratios of the tests modified in the following way. eg ratio<3,1000> => ratio<3,1,-3>. ie they have been "canonicalised". There are obviously an infinite number of ratios which represent the same rational number. The way `ratio` is implemented it always moves as "many powers of 10" from the `num` and `den` into the `exp` and that makes the `canonical` ratio. Because these are all "types" and the lib uses is_same all over the place, only exact matches will be `is_same`. ie ratio<300,4,0> !is_same ratio<3,4,2> (the latter is the canonical ratio). This is perhaps fine for tests in the devlopment phase, but there may be a need for "more forgiving" comparison / concept of value equality. One such comparison which compares den,num,exp after canonicalisation is the constexpr function `same` as defined at top of `ratio_test.cpp`. We may need to expose this and perhaps add even more soft comparisions. 2. In the runtime tests it is "subjective" how some resukts should be printed. There is the question of "how exactly to format certain ratios". eg omit denominators of "1" and exponents of "0". However before even addressing these in detail a decision needs to be made about the general form of "non-floating-point-converted" ratios which do not map exactly to a "Symbol prefix". Arguably these are "relatively ugly" whatever we do, so we could just go for an easily canonicalised form. An example is: - CHECK(stream.str() == "10 [1/60]W"); + CHECK(stream.str() == "10 [1/6 x 10⁻¹]W"); Which of thses is "better"? Is there a "third", better form? It's not obvious. My opnion is: Both of 1&2 are fine for now, unless we think they go down the wrong avenue, and can be "perfected later"? ie we can expose a softer version of ratio based equality, and decide on canonical way of printing ratios (as far as that is actually a very useful output form, compared with decimal, scientific or engineering notation).
This commit is contained in:
1 parent
eef6371ab4
commit
509b6c9653
12 files changed
+183
-103
No files matched your search
@@ -32,6 +32,7 @@
|
||||
#endif
|
||||
|
||||
#include <ostream>
|
||||
#include <iostream>
|
||||
|
||||
namespace units {
|
||||
|
||||
@@ -51,10 +52,10 @@ concept safe_divisible = // exposition only
|
||||
|
||||
/**
|
||||
* @brief A quantity
|
||||
*
|
||||
*
|
||||
* Property of a phenomenon, body, or substance, where the property has a magnitude that can be
|
||||
* expressed by means of a number and a measurement unit.
|
||||
*
|
||||
*
|
||||
* @tparam D a dimension of the quantity (can be either a BaseDimension or a DerivedDimension)
|
||||
* @tparam U a measurement unit of the quantity
|
||||
* @tparam Rep a type to be used to represent values of a quantity
|
||||
@@ -352,7 +353,7 @@ template<typename D1, typename U1, typename Rep1, typename D2, typename U2, type
|
||||
{
|
||||
using common_rep = decltype(lhs.count() * rhs.count());
|
||||
using ratio = ratio_multiply<typename U1::ratio, typename U2::ratio>;
|
||||
return common_rep(lhs.count()) * common_rep(rhs.count()) * common_rep(ratio::num) / common_rep(ratio::den);
|
||||
return common_rep(lhs.count()) * common_rep(rhs.count()) * common_rep(ratio::num) * std::pow(10, common_rep(ratio::exp)) / common_rep(ratio::den);
|
||||
}
|
||||
|
||||
template<typename D1, typename U1, typename Rep1, typename D2, typename U2, typename Rep2>
|
||||
@@ -376,7 +377,7 @@ template<Scalar Value, typename D, typename U, typename Rep>
|
||||
Expects(q.count() != 0);
|
||||
|
||||
using dim = dim_invert<D>;
|
||||
using ratio = ratio<U::ratio::den, U::ratio::num>;
|
||||
using ratio = ratio<U::ratio::den, U::ratio::num, -U::ratio::exp>;
|
||||
using unit = downcast_unit<dim, ratio>;
|
||||
using common_rep = decltype(v / q.count());
|
||||
using ret = quantity<dim, unit, common_rep>;
|
||||
|
||||
Reference in new issue
Block a user