Small refactoring of new ratio (resolves #14)

This commit is contained in:
Mateusz Pusz
2019-12-28 18:59:53 +01:00
parent 23d44c1b4a
commit 17e6b0d75d
6 changed files with 31 additions and 19 deletions

View File

@@ -202,6 +202,7 @@ NOTE: This library as of now compiles correctly only with gcc-9.1 and newer.
- Added official CGS system support
- Added official data information system support
- Repository file tree cleanup
- `ratio` refactored to contain `Exp` template parameter
- 0.4.0 Nov 17, 2019
- Support for derived dimensions in `exp` added

View File

@@ -74,7 +74,16 @@ struct scaled_unit : downcast_base<scaled_unit<R, U>> {
};
```
The above type is a framework's private type and the user should never instantiate it directly.
where:
```cpp
template<typename R>
concept UnitRatio = Ratio<R> && (R::num * R::den > 0);
```
and `Ratio` is satisfied by any instantiation of `units::ratio<Num, Den, Exp>`.
The `scaled_unit` type is a framework's private type and the user should never instantiate it directly.
The public user interface to create units consists of:
![Units UML](units.png)
@@ -143,8 +152,8 @@ namespace units::si {
// prefixes
struct prefix : prefix_type {};
struct centi : units::prefix<kilo, prefix, "c", ratio<1, 100>> {};
struct kilo : units::prefix<kilo, prefix, "k", ratio<1'000>> {};
struct centi : units::prefix<centi, prefix, "c", ratio<1, 1, -2>> {};
struct kilo : units::prefix<kilo, prefix, "k", ratio<1, 1, 3>> {};
// length
struct metre : named_unit<metre, "m", prefix> {};

View File

@@ -31,12 +31,14 @@ constexpr auto ratio_text()
{
if constexpr(Ratio::num != 1 || Ratio::den != 1 || Ratio::exp != 0) {
auto txt = basic_fixed_string("[") + regular<Ratio::num>();
if constexpr(Ratio::den == 1 && Ratio::exp == 0) {
return txt + basic_fixed_string("]");
}
else if constexpr (Ratio::den == 1) {
return txt + basic_fixed_string(" \u00D7 10") + superscript<Ratio::exp>() +
basic_fixed_string("]");
if constexpr(Ratio::den == 1) {
if constexpr(Ratio::exp == 0) {
return txt + basic_fixed_string("]");
}
else {
return txt + basic_fixed_string(" \u00D7 10") + superscript<Ratio::exp>() +
basic_fixed_string("]");
}
}
else {
return txt + basic_fixed_string("/") + regular<Ratio::den>() +

View File

@@ -23,7 +23,6 @@
#pragma once
#include <units/prefix.h>
#include <ratio>
namespace units::data {

View File

@@ -40,7 +40,8 @@ template<typename T>
return v < 0 ? -v : v;
}
constexpr std::tuple<std::intmax_t, std::intmax_t, std::intmax_t> normalize(std::intmax_t num, std::intmax_t den, std::intmax_t exp) {
constexpr std::tuple<std::intmax_t, std::intmax_t, std::intmax_t> normalize(std::intmax_t num, std::intmax_t den, std::intmax_t exp)
{
std::intmax_t gcd = std::gcd(num, den);
num = num * (den < 0 ? -1 : 1) / gcd;
den = detail::abs(den) / gcd;

View File

@@ -296,7 +296,7 @@ TEST_CASE("operator<< on a quantity", "[text][ostream][fmt]")
SECTION("iostream")
{
CHECK(stream.str() == "8 [1 \u00D7 10⁻²]m³");
CHECK(stream.str() == "8 [1 × 10⁻²]m³");
}
SECTION("fmt with default format {} on a quantity")
@@ -317,7 +317,7 @@ TEST_CASE("operator<< on a quantity", "[text][ostream][fmt]")
SECTION("iostream")
{
CHECK(stream.str() == "2 [6 \u00D7 10¹]Hz");
CHECK(stream.str() == "2 [6 × 10¹]Hz");
}
SECTION("fmt with default format {} on a quantity")
@@ -338,7 +338,7 @@ TEST_CASE("operator<< on a quantity", "[text][ostream][fmt]")
SECTION("iostream")
{
CHECK(stream.str() == "10 [1/6 \u00D7 10⁻¹]W");
CHECK(stream.str() == "10 [1/6 × 10⁻¹]W");
}
SECTION("fmt with default format {} on a quantity")
@@ -359,7 +359,7 @@ TEST_CASE("operator<< on a quantity", "[text][ostream][fmt]")
SECTION("iostream")
{
CHECK(stream.str() == "30 [1/6 \u00D7 10²]W");
CHECK(stream.str() == "30 [1/6 × 10²]W");
}
SECTION("fmt with default format {} on a quantity")
@@ -404,7 +404,7 @@ TEST_CASE("operator<< on a quantity", "[text][ostream][fmt]")
SECTION("iostream")
{
CHECK(stream.str() == "8 [1 \u00D7 10³]m⋅s");
CHECK(stream.str() == "8 [1 × 10³]m⋅s");
}
SECTION("fmt with default format {} on a quantity")
@@ -425,7 +425,7 @@ TEST_CASE("operator<< on a quantity", "[text][ostream][fmt]")
SECTION("iostream")
{
CHECK(stream.str() == "2 [6 \u00D7 10¹]kg/s");
CHECK(stream.str() == "2 [6 × 10¹]kg/s");
}
SECTION("fmt with default format {} on a quantity")
@@ -446,7 +446,7 @@ TEST_CASE("operator<< on a quantity", "[text][ostream][fmt]")
SECTION("iostream")
{
CHECK(stream.str() == "10 [1/6 \u00D7 10⁻¹]kg/s");
CHECK(stream.str() == "10 [1/6 × 10⁻¹]kg/s");
}
SECTION("fmt with default format {} on a quantity")
@@ -467,7 +467,7 @@ TEST_CASE("operator<< on a quantity", "[text][ostream][fmt]")
SECTION("iostream")
{
CHECK(stream.str() == "30 [6 \u00D7 10⁻²]1/m⋅s");
CHECK(stream.str() == "30 [6 × 10⁻²]1/m⋅s");
}
SECTION("fmt with default format {} on a quantity")