refactor: UDLs support switched off by default

This commit is contained in:
Mateusz Pusz
2021-04-02 18:56:28 +02:00
parent 5fc7e01d19
commit 1a5b4fc85e
17 changed files with 144 additions and 142 deletions

View File

@ -46,7 +46,7 @@ class UnitsConan(ConanFile):
"build_docs": [True, False] "build_docs": [True, False]
} }
default_options = { default_options = {
"udls": True, "udls": False,
"downcast_mode": "on", "downcast_mode": "on",
"build_docs": True "build_docs": True
} }

View File

@ -128,7 +128,7 @@ udls
**Values**: ``True``/``False`` **Values**: ``True``/``False``
**Defaulted to**: ``True`` **Defaulted to**: ``False``
Determines if library should provide User Defined Literals (UDLs) for quantities of various units. Determines if library should provide User Defined Literals (UDLs) for quantities of various units.
@ -163,7 +163,7 @@ UNITS_UDLS
**Values**: ``ON``/``OFF`` **Values**: ``ON``/``OFF``
**Defaulted to**: ``ON`` **Defaulted to**: ``OFF``
Equivalent to `udls`_. Equivalent to `udls`_.

View File

@ -25,6 +25,7 @@ cmake_minimum_required(VERSION 3.2)
function(add_example target) function(add_example target)
add_executable(${target}_alt ${target}.cpp) add_executable(${target}_alt ${target}.cpp)
target_link_libraries(${target}_alt PRIVATE mp-units::mp-units) target_link_libraries(${target}_alt PRIVATE mp-units::mp-units)
target_compile_definitions(${target}_alt PRIVATE UNITS_UDLS=1)
endfunction() endfunction()
add_example(box_example mp-units::si) add_example(box_example mp-units::si)

View File

@ -73,8 +73,8 @@ void example()
{ {
// SI (int) // SI (int)
{ {
using namespace units::isq::si::literals; using namespace units::isq::si::references;
constexpr Length auto distance = 220_q_km; // constructed from a UDL constexpr Length auto distance = 220 * km; // constructed from a reference
constexpr si::time<si::hour, int> duration(2); // constructed from a value constexpr si::time<si::hour, int> duration(2); // constructed from a value
std::cout << "SI units with 'int' as representation\n"; std::cout << "SI units with 'int' as representation\n";
@ -87,8 +87,8 @@ void example()
// SI (double) // SI (double)
{ {
using namespace units::isq::si::literals; using namespace units::isq::si::references;
constexpr Length auto distance = 220._q_km; // constructed from a UDL constexpr Length auto distance = 220. * km; // constructed from a reference
constexpr si::time<si::hour> duration(2); // constructed from a value constexpr si::time<si::hour> duration(2); // constructed from a value
std::cout << "\nSI units with 'double' as representation\n"; std::cout << "\nSI units with 'double' as representation\n";
@ -103,8 +103,8 @@ void example()
// Customary Units (int) // Customary Units (int)
{ {
using namespace units::isq::si::international::literals; using namespace units::isq::si::international::references;
constexpr Length auto distance = 140_q_mi; // constructed from a UDL constexpr Length auto distance = 140 * mi; // constructed from a reference
constexpr si::time<si::hour, int> duration(2); // constructed from a value constexpr si::time<si::hour, int> duration(2); // constructed from a value
std::cout << "\nUS Customary Units with 'int' as representation\n"; std::cout << "\nUS Customary Units with 'int' as representation\n";
@ -119,8 +119,8 @@ void example()
// Customary Units (double) // Customary Units (double)
{ {
using namespace units::isq::si::international::literals; using namespace units::isq::si::international::references;
constexpr Length auto distance = 140._q_mi; // constructed from a UDL constexpr Length auto distance = 140. * mi; // constructed from a reference
constexpr si::time<si::hour> duration(2); // constructed from a value constexpr si::time<si::hour> duration(2); // constructed from a value
std::cout << "\nUS Customary Units with 'double' as representation\n"; std::cout << "\nUS Customary Units with 'double' as representation\n";
@ -137,8 +137,8 @@ void example()
// CGS (int) // CGS (int)
{ {
using namespace units::isq::si::cgs::literals; using namespace units::isq::si::cgs::references;
constexpr Length auto distance = 22'000'000_q_cm; // constructed from a UDL constexpr Length auto distance = 22'000'000 * cm; // constructed from a reference
constexpr si::cgs::time<si::hour, int> duration(2); // constructed from a value constexpr si::cgs::time<si::hour, int> duration(2); // constructed from a value
std::cout << "\nCGS units with 'int' as representation\n"; std::cout << "\nCGS units with 'int' as representation\n";
@ -156,8 +156,8 @@ void example()
// CGS (double) // CGS (double)
{ {
using namespace units::isq::si::cgs::literals; using namespace units::isq::si::cgs::references;
constexpr Length auto distance = 22'000'000._q_cm; // constructed from a UDL constexpr Length auto distance = 22'000'000. * cm; // constructed from a reference
constexpr si::cgs::time<si::hour> duration(2); // constructed from a value constexpr si::cgs::time<si::hour> duration(2); // constructed from a value
std::cout << "\nCGS units with 'double' as representation\n"; std::cout << "\nCGS units with 'double' as representation\n";

View File

@ -39,43 +39,37 @@
namespace { namespace {
using namespace units::isq; using namespace units::isq;
using namespace si::references;
using m = si::metre; inline constexpr Acceleration auto g = si::si2019::standard_gravity<>;
using m2 = si::square_metre; inline constexpr Density auto air_density = 1.225 * (kg / m3);
using m3 = si::cubic_metre;
using kg = si::kilogram;
using N = si::newton;
using kgpm3 = si::kilogram_per_metre_cub;
inline constexpr auto g = si::si2019::standard_gravity<>;
inline constexpr si::density<kgpm3> air_density(1.225);
class Box { class Box {
si::area<m2> base_; si::area<si::square_metre> base_;
si::length<m> height_; si::length<si::metre> height_;
si::density<kgpm3> density_ = air_density; si::density<si::kilogram_per_metre_cub> density_ = air_density;
public: public:
constexpr Box(const si::length<m>& length, const si::length<m>& width, si::length<m> height) : base_(length * width), height_(std::move(height)) {} constexpr Box(const si::length<si::metre>& length, const si::length<si::metre>& width, si::length<si::metre> height) : base_(length * width), height_(std::move(height)) {}
[[nodiscard]] constexpr si::force<N> filled_weight() const [[nodiscard]] constexpr si::force<si::newton> filled_weight() const
{ {
const si::volume<m3> volume = base_ * height_; const Volume auto volume = base_ * height_;
const si::mass<kg> mass = density_ * volume; const Mass auto mass = density_ * volume;
return mass * g; return mass * g;
} }
[[nodiscard]] constexpr si::length<m> fill_level(const si::mass<kg>& measured_mass) const [[nodiscard]] constexpr si::length<si::metre> fill_level(const si::mass<si::kilogram>& measured_mass) const
{ {
return height_ * measured_mass * g / filled_weight(); return height_ * measured_mass * g / filled_weight();
} }
[[nodiscard]] constexpr si::volume<m3> spare_capacity(const si::mass<kg>& measured_mass) const [[nodiscard]] constexpr si::volume<si::cubic_metre> spare_capacity(const si::mass<si::kilogram>& measured_mass) const
{ {
return (height_ - fill_level(measured_mass)) * base_; return (height_ - fill_level(measured_mass)) * base_;
} }
constexpr void set_contents_density(const si::density<kgpm3>& density_in) constexpr void set_contents_density(const si::density<si::kilogram_per_metre_cub>& density_in)
{ {
assert(density_in > air_density); assert(density_in > air_density);
density_ = density_in; density_ = density_in;
@ -88,14 +82,13 @@ public:
int main() int main()
{ {
using namespace units; using namespace units;
using namespace si::literals;
const si::length<m> height(200.0_q_mm); const si::length<si::metre> height = 200.0 * mm;
auto box = Box(1000.0_q_mm, 500.0_q_mm, height); auto box = Box(1000.0 * mm, 500.0 * mm, height);
box.set_contents_density(1000.0_q_kg_per_m3); box.set_contents_density(1000.0 * (kg / m3));
const auto fill_time = 200.0_q_s; // time since starting fill const auto fill_time = 200.0 * s; // time since starting fill
const auto measured_mass = 20.0_q_kg; // measured mass at fill_time const auto measured_mass = 20.0 * kg; // measured mass at fill_time
const Length auto fill_level = box.fill_level(measured_mass); const Length auto fill_level = box.fill_level(measured_mass);
const Dimensionless auto fill_percent = quantity_cast<percent>(fill_level / height); const Dimensionless auto fill_percent = quantity_cast<percent>(fill_level / height);

View File

@ -34,27 +34,28 @@ int main()
{ {
using namespace units::isq; using namespace units::isq;
using namespace units::isq::si; using namespace units::isq::si;
using namespace units::isq::si::references;
std::cout << "mp-units capacitor time curve example...\n"; std::cout << "mp-units capacitor time curve example...\n";
std::cout.setf(std::ios_base::fixed, std::ios_base::floatfield); std::cout.setf(std::ios_base::fixed, std::ios_base::floatfield);
std::cout.precision(3); std::cout.precision(3);
constexpr auto C = 0.47_q_uF; constexpr auto C = 0.47 * uF;
constexpr auto V0 = 5.0_q_V; constexpr auto V0 = 5.0 * V;
constexpr auto R = 4.7_q_kR; constexpr auto R = 4.7 * kR;
for (auto t = 0_q_ms; t <= 50_q_ms; ++t) { for (auto t = 0 * ms; t <= 50 * ms; ++t) {
const Voltage auto Vt = V0 * units::exp(-t / (R * C)); const Voltage auto Vt = V0 * units::exp(-t / (R * C));
std::cout << "at " << t << " voltage is "; std::cout << "at " << t << " voltage is ";
if (Vt >= 1_q_V) if (Vt >= 1 * V)
std::cout << Vt; std::cout << Vt;
else if (Vt >= 1_q_mV) else if (Vt >= 1 * mV)
std::cout << quantity_cast<millivolt>(Vt); std::cout << quantity_cast<millivolt>(Vt);
else if (Vt >= 1_q_uV) else if (Vt >= 1 * uV)
std::cout << quantity_cast<microvolt>(Vt); std::cout << quantity_cast<microvolt>(Vt);
else if (Vt >= 1_q_nV) else if (Vt >= 1 * nV)
std::cout << quantity_cast<nanovolt>(Vt); std::cout << quantity_cast<nanovolt>(Vt);
else else
std::cout << quantity_cast<picovolt>(Vt); std::cout << quantity_cast<picovolt>(Vt);

View File

@ -29,55 +29,54 @@
namespace { namespace {
using namespace units;
void simple_quantities() void simple_quantities()
{ {
using namespace units::isq::si; using namespace units::isq;
using namespace units::isq::si::international; using namespace units::isq::si::references;
using namespace units::isq::si::international::references;
using distance = length<metre>; using distance = si::length<si::metre>;
using duration = isq::si::time<second>; using duration = si::time<si::second>;
constexpr distance km = 1.0_q_km; constexpr distance kilometers = 1.0 * km;
constexpr distance miles = 1.0_q_mi; constexpr distance miles = 1.0 * mi;
constexpr duration sec = 1_q_s; constexpr duration seconds = 1 * s;
constexpr duration min = 1_q_min; constexpr duration minutes = 1 * min;
constexpr duration hr = 1_q_h; constexpr duration hours = 1 * h;
std::cout << "A physical quantities library can choose the simple\n"; std::cout << "A physical quantities library can choose the simple\n";
std::cout << "option to provide output using a single type for each base unit:\n\n"; std::cout << "option to provide output using a single type for each base unit:\n\n";
std::cout << km << '\n'; std::cout << kilometers << '\n';
std::cout << miles << '\n'; std::cout << miles << '\n';
std::cout << sec << '\n'; std::cout << seconds << '\n';
std::cout << min << '\n'; std::cout << minutes << '\n';
std::cout << hr << "\n\n"; std::cout << hours << "\n\n";
} }
void quantities_with_typed_units() void quantities_with_typed_units()
{ {
using namespace units::isq; using namespace units::isq;
using namespace units::isq::si; using namespace units::isq::si::references;
using namespace units::isq::si::international; using namespace units::isq::si::international::references;
constexpr length<kilometre> km = 1.0_q_km; constexpr si::length<si::kilometre> kilometers = 1.0 * km;
constexpr length<mile> miles = 1.0_q_mi; constexpr si::length<si::international::mile> miles = 1.0 * mi;
std::cout.precision(6); std::cout.precision(6);
constexpr si::time<second> sec = 1_q_s; constexpr si::time<si::second> seconds = 1 * s;
constexpr si::time<minute> min = 1_q_min; constexpr si::time<si::minute> minutes = 1 * min;
constexpr si::time<hour> hr = 1_q_h; constexpr si::time<si::hour> hours = 1 * h;
std::cout << "A more flexible option is to provide separate types for each unit,\n\n"; std::cout << "A more flexible option is to provide separate types for each unit,\n\n";
std::cout << km << '\n'; std::cout << kilometers << '\n';
std::cout << miles << '\n'; std::cout << miles << '\n';
std::cout << sec << '\n'; std::cout << seconds << '\n';
std::cout << min << '\n'; std::cout << minutes << '\n';
std::cout << hr << "\n\n"; std::cout << hours << "\n\n";
constexpr length<metre> meter = 1_q_m; constexpr si::length<si::metre> meter = 1 * m;
std::cout << "then a wide range of pre-defined units can be defined and converted,\n" std::cout << "then a wide range of pre-defined units can be defined and converted,\n"
" for consistency and repeatability across applications:\n\n"; " for consistency and repeatability across applications:\n\n";
@ -105,34 +104,35 @@ void quantities_with_typed_units()
void calcs_comparison() void calcs_comparison()
{ {
using namespace units::isq::si; using namespace units::isq;
using namespace units::isq::si::references;
std::cout << "\nA distinct unit for each type is efficient and accurate\n" std::cout << "\nA distinct unit for each type is efficient and accurate\n"
"when adding two values of the same very big\n" "when adding two values of the same very big\n"
"or very small type:\n\n"; "or very small type:\n\n";
length<femtometre, float> L1A = 2._q_fm; si::length<si::femtometre, float> L1A = 2. * fm;
length<femtometre, float> L2A = 3._q_fm; si::length<si::femtometre, float> L2A = 3. * fm;
length<femtometre, float> LrA = L1A + L2A; si::length<si::femtometre, float> LrA = L1A + L2A;
fmt::print("{:%.30Q %q}\n + {:%.30Q %q}\n = {:%.30Q %q}\n\n", L1A, L2A, LrA); fmt::print("{:%.30Q %q}\n + {:%.30Q %q}\n = {:%.30Q %q}\n\n", L1A, L2A, LrA);
std::cout << "The single unit method must convert large\n" std::cout << "The single unit method must convert large\n"
"or small values in other units to the base unit.\n" "or small values in other units to the base unit.\n"
"This is both inefficient and inaccurate\n\n"; "This is both inefficient and inaccurate\n\n";
length<metre, float> L1B = L1A; si::length<si::metre, float> L1B = L1A;
length<metre, float> L2B = L2A; si::length<si::metre, float> L2B = L2A;
length<metre, float> LrB = L1B + L2B; si::length<si::metre, float> LrB = L1B + L2B;
fmt::print("{:%.30Q %q}\n + {:%.30Q %q}\n = {:%.30Q %q}\n\n", L1B, L2B, LrB); fmt::print("{:%.30Q %q}\n + {:%.30Q %q}\n = {:%.30Q %q}\n\n", L1B, L2B, LrB);
std::cout << "In multiplication and division:\n\n"; std::cout << "In multiplication and division:\n\n";
area<square_femtometre, float> ArA = L1A * L2A; si::area<si::square_femtometre, float> ArA = L1A * L2A;
fmt::print("{:%.30Q %q}\n * {:%.30Q %q}\n = {:%.30Q %q}\n\n", L1A, L2A, ArA); fmt::print("{:%.30Q %q}\n * {:%.30Q %q}\n = {:%.30Q %q}\n\n", L1A, L2A, ArA);
std::cout << "similar problems arise\n\n"; std::cout << "similar problems arise\n\n";
area<square_metre, float> ArB = L1B * L2B; si::area<si::square_metre, float> ArB = L1B * L2B;
fmt::print("{:%.30Q %q}\n * {:%.30Q %q}\n = {:%.30Q %q}\n\n", L1B, L2B, ArB); fmt::print("{:%.30Q %q}\n * {:%.30Q %q}\n = {:%.30Q %q}\n\n", L1B, L2B, ArB);
} }

View File

@ -43,10 +43,11 @@ inline constexpr std::common_type_t<typename Target::rep, typename Source::rep>
int main() int main()
{ {
using namespace units::isq::si; using namespace units::isq::si;
using namespace units::isq::si::references;
std::cout << "conversion factor in mp-units...\n\n"; std::cout << "conversion factor in mp-units...\n\n";
constexpr length<metre> lengthA = 2.0_q_m; constexpr length<metre> lengthA = 2.0 * m;
constexpr length<millimetre> lengthB = lengthA; constexpr length<millimetre> lengthB = lengthA;
std::cout << fmt::format("lengthA( {} ) and lengthB( {} )\n", lengthA, lengthB) std::cout << fmt::format("lengthA( {} ) and lengthB( {} )\n", lengthA, lengthB)

View File

@ -26,6 +26,7 @@
#include <units/isq/si/fps/mass.h> #include <units/isq/si/fps/mass.h>
#include <units/isq/si/fps/power.h> #include <units/isq/si/fps/power.h>
#include <units/isq/si/fps/speed.h> #include <units/isq/si/fps/speed.h>
#include <units/isq/si/fps/volume.h>
#include <units/isq/si/length.h> #include <units/isq/si/length.h>
#include <units/isq/si/mass.h> #include <units/isq/si/mass.h>
#include <units/isq/si/power.h> #include <units/isq/si/power.h>
@ -62,8 +63,8 @@ auto fmt_line(const Q a)
// Print the ship details in the units as defined in the Ship struct, in other si::imperial units, and in SI // Print the ship details in the units as defined in the Ship struct, in other si::imperial units, and in SI
void print_details(std::string_view description, const Ship& ship) void print_details(std::string_view description, const Ship& ship)
{ {
using namespace units::isq::si::fps::literals; using namespace units::isq::si::fps::references;
const auto waterDensity = 62.4_q_lb_per_ft3; const auto waterDensity = 62.4 * (lb / ft3);
std::cout << fmt::format("{}\n", description); std::cout << fmt::format("{}\n", description);
std::cout << fmt::format("{:20} : {}\n", "length", fmt_line<si::fps::length<si::fps::yard>, si::length<si::metre>>(ship.length)) std::cout << fmt::format("{:20} : {}\n", "length", fmt_line<si::fps::length<si::fps::yard>, si::length<si::metre>>(ship.length))
<< fmt::format("{:20} : {}\n", "draft", fmt_line<si::fps::length<si::fps::yard>, si::length<si::metre>>(ship.draft)) << fmt::format("{:20} : {}\n", "draft", fmt_line<si::fps::length<si::fps::yard>, si::length<si::metre>>(ship.draft))
@ -79,17 +80,18 @@ void print_details(std::string_view description, const Ship& ship)
int main() int main()
{ {
using namespace units::isq::si::literals; using namespace units::isq::si::references;
using namespace units::isq::si::fps::literals; using namespace units::isq::si::fps::references;
using units::isq::si::fps::references::ft; // collides with si::femtotonne (alias unit of mass)
// KMS Bismark, using the units the Germans would use, taken from Wiki // KMS Bismark, using the units the Germans would use, taken from Wiki
auto bismark = Ship{.length{251._q_m}, .draft{9.3_q_m}, .beam{36_q_m}, .speed{56_q_km_per_h}, .mass{50'300_q_t}, .mainGuns{380_q_mm}, .shellMass{800_q_kg}, .shellSpeed{820._q_m_per_s}, .power{110.45_q_kW}}; auto bismark = Ship{.length{251. * m}, .draft{9.3 * m}, .beam{36 * m}, .speed{56 * (km / h)}, .mass{50'300 * t}, .mainGuns{380 * mm}, .shellMass{800 * kg}, .shellSpeed{820. * (m / s)}, .power{110.45 * kW}};
// USS Iowa, using units from the foot-pound-second system // USS Iowa, using units from the foot-pound-second system
auto iowa = Ship{.length{860._q_ft}, .draft{37._q_ft + 2._q_in}, .beam{108._q_ft + 2._q_in}, .speed{33_q_knot}, .mass{57'540_q_lton}, .mainGuns{16_q_in}, .shellMass{2700_q_lb}, .shellSpeed{2690._q_ft_per_s}, .power{212'000_q_hp}}; auto iowa = Ship{.length{860. * ft}, .draft{37. * ft + 2. * in}, .beam{108. * ft + 2. * in}, .speed{33 * knot}, .mass{57'540 * lton}, .mainGuns{16 * in}, .shellMass{2700 * lb}, .shellSpeed{2690. * (ft / s)}, .power{212'000 * hp}};
// HMS King George V, using units from the foot-pound-second system // HMS King George V, using units from the foot-pound-second system
auto kgv = Ship{.length{745.1_q_ft}, .draft{33._q_ft + 7.5_q_in}, .beam{103.2_q_ft + 2.5_q_in}, .speed{28.3_q_knot}, .mass{42'245_q_lton}, .mainGuns{14_q_in}, .shellMass{1'590_q_lb}, .shellSpeed{2483._q_ft_per_s}, .power{110'000_q_hp}}; auto kgv = Ship{.length{745.1 * ft}, .draft{33. * ft + 7.5 * in}, .beam{103.2 * ft + 2.5 * in}, .speed{28.3 * knot}, .mass{42'245 * lton}, .mainGuns{14 * in}, .shellMass{1'590 * lb}, .shellSpeed{2483. * (ft / s)}, .power{110'000 * hp}};
print_details("KMS Bismark, defined in appropriate units from the SI system", bismark); print_details("KMS Bismark, defined in appropriate units from the SI system", bismark);
std::cout << "\n\n"; std::cout << "\n\n";

View File

@ -20,6 +20,8 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE. // SOFTWARE.
#define UNITS_UDLS 1
#include <units/format.h> #include <units/format.h>
#include <units/isq/si/international/length.h> #include <units/isq/si/international/length.h>
#include <units/isq/si/international/speed.h> // IWYU pragma: keep #include <units/isq/si/international/speed.h> // IWYU pragma: keep

View File

@ -41,9 +41,9 @@ struct state_variable {
}; };
using namespace units::isq; using namespace units::isq;
using namespace units::isq::si::literals; using namespace units::isq::si::references;
constexpr auto radar_transmit_interval = 5.0_q_s; constexpr auto radar_transmit_interval = 5.0 * s;
constexpr double kalman_range_gain = 0.2; constexpr double kalman_range_gain = 0.2;
constexpr double kalman_speed_gain = 0.1; constexpr double kalman_speed_gain = 0.1;
@ -74,16 +74,16 @@ int main()
std::cout << "\n\n1d aircraft α-β filter example2 from https://www.kalmanfilter.net/alphabeta.html#ex2"; std::cout << "\n\n1d aircraft α-β filter example2 from https://www.kalmanfilter.net/alphabeta.html#ex2";
std::cout << "\n\n"; std::cout << "\n\n";
constexpr auto measurements = std::array{0.0_q_m, // N.B measurement[0] is unknown and unused constexpr auto measurements = std::array{0.0 * m, // N.B measurement[0] is unknown and unused
30110.0_q_m, 30265.0_q_m, 30740.0_q_m, 30750.0_q_m, 31135.0_q_m, 30110.0 * m, 30265.0 * m, 30740.0 * m, 30750.0 * m, 31135.0 * m,
31015.0_q_m, 31180.0_q_m, 31610.0_q_m, 31960.0_q_m, 31865.0_q_m}; 31015.0 * m, 31180.0 * m, 31610.0 * m, 31960.0 * m, 31865.0 * m};
constexpr auto num_measurements = measurements.size(); constexpr auto num_measurements = measurements.size();
std::array<state, num_measurements> track; std::array<state, num_measurements> track;
// We need an initial estimate of track[0] as there is no previous state to get a prediction from // We need an initial estimate of track[0] as there is no previous state to get a prediction from
track[0].range.estimated_current_state = 30'000_q_m; track[0].range.estimated_current_state = 30'000 * m;
track[0].speed.estimated_current_state = 40.0_q_m_per_s; track[0].speed.estimated_current_state = 40.0 * (m / s);
for (auto n = 0U; n < num_measurements; ++n) { for (auto n = 0U; n < num_measurements; ++n) {
if (n > 0) { if (n > 0) {

View File

@ -60,7 +60,7 @@ std::ostream& operator<<(std::ostream& os, const matrix<ET, OT>& v)
namespace { namespace {
using namespace units::isq; using namespace units::isq;
using namespace units::isq::si::literals; using namespace units::isq::si::references;
template<typename Rep = double> template<typename Rep = double>
using vector = std::math::fs_vector<Rep, 3>; using vector = std::math::fs_vector<Rep, 3>;
@ -73,9 +73,9 @@ void vector_of_quantity_add()
{ {
std::cout << "\nvector_of_quantity_add:\n"; std::cout << "\nvector_of_quantity_add:\n";
vector<si::length<si::metre>> v = { 1_q_m, 2_q_m, 3_q_m }; vector<si::length<si::metre>> v = { 1 * m, 2 * m, 3 * m };
vector<si::length<si::metre>> u = { 3_q_m, 2_q_m, 1_q_m }; vector<si::length<si::metre>> u = { 3 * m, 2 * m, 1 * m };
vector<si::length<si::kilometre>> t = { 3_q_km, 2_q_km, 1_q_km }; vector<si::length<si::kilometre>> t = { 3 * km, 2 * km, 1 * km };
std::cout << "v = " << v << "\n"; std::cout << "v = " << v << "\n";
std::cout << "u = " << u << "\n"; std::cout << "u = " << u << "\n";
@ -90,28 +90,28 @@ void vector_of_quantity_multiply_same()
{ {
std::cout << "\nvector_of_quantity_multiply_same:\n"; std::cout << "\nvector_of_quantity_multiply_same:\n";
vector<si::length<si::metre>> v = { 1_q_m, 2_q_m, 3_q_m }; vector<si::length<si::metre>> v = { 1 * m, 2 * m, 3 * m };
vector<si::length<si::metre>> u = { 3_q_m, 2_q_m, 1_q_m }; vector<si::length<si::metre>> u = { 3 * m, 2 * m, 1 * m };
std::cout << "v = " << v << "\n"; std::cout << "v = " << v << "\n";
std::cout << "u = " << u << "\n"; std::cout << "u = " << u << "\n";
std::cout << "v * u = " << v * u << "\n"; std::cout << "v * u = " << v * u << "\n";
std::cout << "2_q_m * v = " << 2._q_m * v << "\n"; std::cout << "2 * m * v = " << 2. * m * v << "\n";
} }
void vector_of_quantity_multiply_different() void vector_of_quantity_multiply_different()
{ {
std::cout << "\nvector_of_quantity_multiply_different:\n"; std::cout << "\nvector_of_quantity_multiply_different:\n";
vector<si::force<si::newton>> v = { 1_q_N, 2_q_N, 3_q_N }; vector<si::force<si::newton>> v = { 1 * N, 2 * N, 3 * N };
vector<si::length<si::metre>> u = { 3_q_m, 2_q_m, 1_q_m }; vector<si::length<si::metre>> u = { 3 * m, 2 * m, 1 * m };
std::cout << "v = " << v << "\n"; std::cout << "v = " << v << "\n";
std::cout << "u = " << u << "\n"; std::cout << "u = " << u << "\n";
std::cout << "v * u = " << v * u << "\n"; std::cout << "v * u = " << v * u << "\n";
std::cout << "2_q_N * u = " << 2._q_N * u << "\n"; std::cout << "2 * N * u = " << 2. * N * u << "\n";
std::cout << "2 * u = " << 2 * u << "\n"; std::cout << "2 * u = " << 2 * u << "\n";
} }
@ -119,12 +119,12 @@ void vector_of_quantity_divide_by_scalar()
{ {
std::cout << "\nvector_of_quantity_divide_by_scalar:\n"; std::cout << "\nvector_of_quantity_divide_by_scalar:\n";
vector<si::length<si::metre>> v = { 4_q_m, 8_q_m, 12_q_m }; vector<si::length<si::metre>> v = { 4 * m, 8 * m, 12 * m };
std::cout << "v = " << v << "\n"; std::cout << "v = " << v << "\n";
// TODO Uncomment when bug in the LA is fixed // TODO Uncomment when bug in the LA is fixed
// std::cout << "v / 2_q_s = " << v / 2_q_s << "\n"; // std::cout << "v / (2 * s) = " << v / (2 * s) << "\n";
// std::cout << "v / 2 = " << v / 2 << "\n"; // std::cout << "v / 2 = " << v / 2 << "\n";
} }
@ -140,9 +140,9 @@ void matrix_of_quantity_add()
{ {
std::cout << "\nmatrix_of_quantity_add:\n"; std::cout << "\nmatrix_of_quantity_add:\n";
matrix<si::length<si::metre>> v = {{ 1_q_m, 2_q_m, 3_q_m }, { 4_q_m, 5_q_m, 6_q_m }, { 7_q_m, 8_q_m, 9_q_m }}; matrix<si::length<si::metre>> v = {{ 1 * m, 2 * m, 3 * m }, { 4 * m, 5 * m, 6 * m }, { 7 * m, 8 * m, 9 * m }};
matrix<si::length<si::metre>> u = {{ 3_q_m, 2_q_m, 1_q_m }, { 3_q_m, 2_q_m, 1_q_m }, { 3_q_m, 2_q_m, 1_q_m }}; matrix<si::length<si::metre>> u = {{ 3 * m, 2 * m, 1 * m }, { 3 * m, 2 * m, 1 * m }, { 3 * m, 2 * m, 1 * m }};
matrix<si::length<si::millimetre>> t = {{ 3_q_mm, 2_q_mm, 1_q_mm }, { 3_q_mm, 2_q_mm, 1_q_mm }, { 3_q_mm, 2_q_mm, 1_q_mm }}; matrix<si::length<si::millimetre>> t = {{ 3 * mm, 2 * mm, 1 * mm }, { 3 * mm, 2 * mm, 1 * mm }, { 3 * mm, 2 * mm, 1 * mm }};
std::cout << "v =\n" << v << "\n"; std::cout << "v =\n" << v << "\n";
std::cout << "u =\n" << u << "\n"; std::cout << "u =\n" << u << "\n";
@ -159,28 +159,28 @@ void matrix_of_quantity_multiply_same()
{ {
std::cout << "\nmatrix_of_quantity_multiply_same:\n"; std::cout << "\nmatrix_of_quantity_multiply_same:\n";
matrix<si::length<si::metre>> v = {{ 1_q_m, 2_q_m, 3_q_m }, { 4_q_m, 5_q_m, 6_q_m }, { 7_q_m, 8_q_m, 9_q_m }}; matrix<si::length<si::metre>> v = {{ 1 * m, 2 * m, 3 * m }, { 4 * m, 5 * m, 6 * m }, { 7 * m, 8 * m, 9 * m }};
vector<si::length<si::metre>> u = { 3_q_m, 2_q_m, 1_q_m }; vector<si::length<si::metre>> u = { 3 * m, 2 * m, 1 * m };
std::cout << "v =\n" << v << "\n"; std::cout << "v =\n" << v << "\n";
std::cout << "u =\n" << u << "\n"; std::cout << "u =\n" << u << "\n";
std::cout << "v * u =\n" << v * u << "\n"; std::cout << "v * u =\n" << v * u << "\n";
std::cout << "2_q_m * u =\n" << 2._q_m * u << "\n"; std::cout << "2 * m * u =\n" << 2. * m * u << "\n";
} }
void matrix_of_quantity_multiply_different() void matrix_of_quantity_multiply_different()
{ {
std::cout << "\nmatrix_of_quantity_multiply_different:\n"; std::cout << "\nmatrix_of_quantity_multiply_different:\n";
vector<si::force<si::newton>> v = { 1_q_N, 2_q_N, 3_q_N }; vector<si::force<si::newton>> v = { 1 * N, 2 * N, 3 * N };
matrix<si::length<si::metre>> u = {{ 1_q_m, 2_q_m, 3_q_m }, { 4_q_m, 5_q_m, 6_q_m }, { 7_q_m, 8_q_m, 9_q_m }}; matrix<si::length<si::metre>> u = {{ 1 * m, 2 * m, 3 * m }, { 4 * m, 5 * m, 6 * m }, { 7 * m, 8 * m, 9 * m }};
std::cout << "v =\n" << v << "\n"; std::cout << "v =\n" << v << "\n";
std::cout << "u =\n" << u << "\n"; std::cout << "u =\n" << u << "\n";
std::cout << "v * u =\n" << v * u << "\n"; std::cout << "v * u =\n" << v * u << "\n";
std::cout << "2_q_N * u =\n" << 2._q_N * u << "\n"; std::cout << "2 * N * u =\n" << 2. * N * u << "\n";
std::cout << "2 * u =\n" << 2 * u << "\n"; std::cout << "2 * u =\n" << 2 * u << "\n";
} }
@ -188,12 +188,12 @@ void matrix_of_quantity_divide_by_scalar()
{ {
std::cout << "\nmatrix_of_quantity_divide_by_scalar:\n"; std::cout << "\nmatrix_of_quantity_divide_by_scalar:\n";
matrix<si::length<si::metre>> v = {{ 2_q_m, 4_q_m, 6_q_m }, { 4_q_m, 6_q_m, 8_q_m }, { 8_q_m, 4_q_m, 2_q_m }}; matrix<si::length<si::metre>> v = {{ 2 * m, 4 * m, 6 * m }, { 4 * m, 6 * m, 8 * m }, { 8 * m, 4 * m, 2 * m }};
std::cout << "v =\n" << v << "\n"; std::cout << "v =\n" << v << "\n";
// TODO Uncomment when bug in the LA is fixed // TODO Uncomment when bug in the LA is fixed
// std::cout << "v / 2_q_s =\n" << v / 2_q_s << "\n"; // std::cout << "v / (2 * s) =\n" << v / (2 * s) << "\n";
// std::cout << "v / 2 =\n" << v / 2 << "\n"; // std::cout << "v / 2 =\n" << v / 2 << "\n";
} }
@ -239,7 +239,7 @@ void quantity_of_vector_multiply_same()
std::cout << "u = " << u << "\n"; std::cout << "u = " << u << "\n";
std::cout << "v * u = " << v * u << "\n"; std::cout << "v * u = " << v * u << "\n";
std::cout << "2_q_m * v = " << 2._q_m * v << "\n"; std::cout << "2 * m * v = " << 2. * m * v << "\n";
} }
void quantity_of_vector_multiply_different() void quantity_of_vector_multiply_different()
@ -253,7 +253,7 @@ void quantity_of_vector_multiply_different()
std::cout << "u = " << u << "\n"; std::cout << "u = " << u << "\n";
std::cout << "v * u = " << v * u << "\n"; std::cout << "v * u = " << v * u << "\n";
std::cout << "2_q_N * u = " << 2._q_N * u << "\n"; std::cout << "2 * N * u = " << 2. * N * u << "\n";
std::cout << "2 * u = " << 2 * u << "\n"; std::cout << "2 * u = " << 2 * u << "\n";
} }
@ -266,7 +266,7 @@ void quantity_of_vector_divide_by_scalar()
std::cout << "v = " << v << "\n"; std::cout << "v = " << v << "\n";
// TODO Uncomment when bug in the LA is fixed // TODO Uncomment when bug in the LA is fixed
// std::cout << "v / 2_q_s = " << v / 2_q_s << "\n"; // std::cout << "v / 2 * s = " << v / 2 * s << "\n";
// std::cout << "v / 2 = " << v / 2 << "\n"; // std::cout << "v / 2 = " << v / 2 << "\n";
} }
@ -311,7 +311,7 @@ void quantity_of_matrix_multiply_same()
std::cout << "u =\n" << u << "\n"; std::cout << "u =\n" << u << "\n";
std::cout << "v * u =\n" << v * u << "\n"; std::cout << "v * u =\n" << v * u << "\n";
std::cout << "2_q_m * u =\n" << 2._q_m * u << "\n"; std::cout << "2 * m * u =\n" << 2. * m * u << "\n";
} }
void quantity_of_matrix_multiply_different() void quantity_of_matrix_multiply_different()
@ -325,7 +325,7 @@ void quantity_of_matrix_multiply_different()
std::cout << "u =\n" << u << "\n"; std::cout << "u =\n" << u << "\n";
std::cout << "v * u =\n" << v * u << "\n"; std::cout << "v * u =\n" << v * u << "\n";
std::cout << "2_q_N * u =\n" << 2._q_N * u << "\n"; std::cout << "2 * N * u =\n" << 2. * N * u << "\n";
std::cout << "2 * u =\n" << 2 * u << "\n"; std::cout << "2 * u =\n" << 2 * u << "\n";
} }
@ -338,7 +338,7 @@ void quantity_of_matrix_divide_by_scalar()
std::cout << "v =\n" << v << "\n"; std::cout << "v =\n" << v << "\n";
// TODO Uncomment when bug in the LA is fixed // TODO Uncomment when bug in the LA is fixed
// std::cout << "v / 2_q_s =\n" << v / 2_q_s << "\n"; // std::cout << "v / (2 * s) =\n" << v / (2 * s) << "\n";
// std::cout << "v / 2 =\n" << v / 2 << "\n"; // std::cout << "v / 2 =\n" << v / 2 << "\n";
} }

View File

@ -43,14 +43,14 @@ Energy auto total_energy(Momentum auto p, Mass auto m, Speed auto c)
void si_example() void si_example()
{ {
using namespace units::isq::si; using namespace units::isq::si;
using GeV = gigaelectronvolt; using namespace units::isq::si::references;
constexpr Speed auto c = si2019::speed_of_light<>; constexpr Speed auto c = si2019::speed_of_light<>;
std::cout << "\n*** SI units (c = " << c << ") ***\n"; std::cout << "\n*** SI units (c = " << c << ") ***\n";
const Momentum auto p = 4._q_GeV / c; const Momentum auto p = 4. * GeV / c;
const Mass auto m = 3._q_GeV / pow<2>(c); const Mass auto m = 3. * GeV / pow<2>(c);
const Energy auto E = total_energy(p, m, c); const Energy auto E = total_energy(p, m, c);
std::cout << "[in GeV]\n" std::cout << "[in GeV]\n"
@ -68,17 +68,16 @@ void si_example()
<< "E = " << E_si << "\n"; << "E = " << E_si << "\n";
std::cout << "\n[converted from SI units back to GeV]\n" std::cout << "\n[converted from SI units back to GeV]\n"
<< "E = " << quantity_cast<GeV>(E_si) << "\n"; << "E = " << quantity_cast<gigaelectronvolt>(E_si) << "\n";
} }
void natural_example() void natural_example()
{ {
using namespace units::isq::natural; using namespace units::isq::natural;
using GeV = gigaelectronvolt;
constexpr Speed auto c = speed_of_light<>; constexpr Speed auto c = speed_of_light<>;
const momentum<GeV> p(4); const momentum<gigaelectronvolt> p(4);
const mass<GeV> m(3); const mass<gigaelectronvolt> m(3);
const Energy auto E = total_energy(p, m, c); const Energy auto E = total_energy(p, m, c);
std::cout << "\n*** Natural units (c = " << c << ") ***\n" std::cout << "\n*** Natural units (c = " << c << ") ***\n"

View File

@ -38,15 +38,15 @@ constexpr units::isq::Speed auto avg_speed(D d, T t)
void example() void example()
{ {
using namespace units::isq; using namespace units::isq;
using namespace units::isq::si::literals; using namespace units::isq::si::references;
Length auto d1 = 123_q_m; Length auto d1 = 123 * m;
Time auto t1 = 10_q_s; Time auto t1 = 10 * s;
Speed auto v1 = avg_speed(d1, t1); Speed auto v1 = avg_speed(d1, t1);
auto temp1 = v1 * 50_q_m; // produces intermediate unknown dimension with 'unknown_coherent_unit' as its 'coherent_unit' auto temp1 = v1 * (50 * m); // produces intermediate unknown dimension with 'unknown_coherent_unit' as its 'coherent_unit'
Speed auto v2 = temp1 / 100_q_m; // back to known dimensions again Speed auto v2 = temp1 / (100 * m); // back to known dimensions again
Length auto d2 = v2 * 60_q_s; Length auto d2 = v2 * (60 * s);
std::cout << "d1 = " << d1 << '\n'; std::cout << "d1 = " << d1 << '\n';
std::cout << "t1 = " << t1 << '\n'; std::cout << "t1 = " << t1 << '\n';

View File

@ -27,7 +27,7 @@ project(mp-units
) )
option(UNITS_AS_SYSTEM_HEADERS "Exports library as system headers" OFF) option(UNITS_AS_SYSTEM_HEADERS "Exports library as system headers" OFF)
option(UNITS_UDLS "Enables definitions of User Defined Literals (UDLs) provided for quantities of various units" ON) option(UNITS_UDLS "Enables definitions of User Defined Literals (UDLs) provided for quantities of various units" OFF)
message(STATUS "UNITS_AS_SYSTEM_HEADERS: ${UNITS_AS_SYSTEM_HEADERS}") message(STATUS "UNITS_AS_SYSTEM_HEADERS: ${UNITS_AS_SYSTEM_HEADERS}")
message(STATUS "UNITS_UDLS: ${UNITS_UDLS}") message(STATUS "UNITS_UDLS: ${UNITS_UDLS}")

View File

@ -35,6 +35,7 @@ target_link_libraries(unit_tests_runtime PRIVATE
mp-units::mp-units mp-units::mp-units
Catch2::Catch2 Catch2::Catch2
) )
target_compile_definitions(unit_tests_runtime PRIVATE UNITS_UDLS=1)
if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
target_compile_options(unit_tests_runtime PRIVATE target_compile_options(unit_tests_runtime PRIVATE

View File

@ -37,6 +37,7 @@ target_link_libraries(unit_tests_static_truncating PRIVATE
target_compile_options(unit_tests_static_truncating PRIVATE target_compile_options(unit_tests_static_truncating PRIVATE
$<IF:$<CXX_COMPILER_ID:MSVC>,/wd4242 /wd4244,-Wno-conversion> $<IF:$<CXX_COMPILER_ID:MSVC>,/wd4242 /wd4244,-Wno-conversion>
) )
target_compile_definitions(unit_tests_static_truncating PRIVATE UNITS_UDLS=1)
add_library(unit_tests_static add_library(unit_tests_static
cgs_test.cpp cgs_test.cpp
@ -73,3 +74,4 @@ target_link_libraries(unit_tests_static PRIVATE
unit_tests_static_truncating unit_tests_static_truncating
mp-units::mp-units mp-units::mp-units
) )
target_compile_definitions(unit_tests_static PRIVATE UNITS_UDLS=1)