2019-11-16 18:30:44 +01:00
|
|
|
// The MIT License (MIT)
|
|
|
|
|
//
|
|
|
|
|
// Copyright (c) 2018 Mateusz Pusz
|
|
|
|
|
//
|
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
// in the Software without restriction, including without limitation the rights
|
|
|
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
// furnished to do so, subject to the following conditions:
|
|
|
|
|
//
|
|
|
|
|
// The above copyright notice and this permission notice shall be included in all
|
|
|
|
|
// copies or substantial portions of the Software.
|
|
|
|
|
//
|
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
|
// SOFTWARE.
|
|
|
|
|
|
2025-12-13 13:18:59 +01:00
|
|
|
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
|
|
|
// !!! Before you commit any changes to this file please make sure to check if it !!!
|
|
|
|
|
// !!! renders correctly in the documentation "Examples" section. !!!
|
|
|
|
|
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
|
|
|
|
|
|
|
|
#include "measurement.h"
|
2024-04-24 20:53:54 +02:00
|
|
|
#include <mp-units/bits/hacks.h>
|
2024-01-06 09:25:17 +01:00
|
|
|
#include <mp-units/compat_macros.h>
|
2024-07-16 17:36:00 +02:00
|
|
|
#ifdef MP_UNITS_IMPORT_STD
|
|
|
|
|
import std;
|
|
|
|
|
#else
|
2024-01-06 08:51:00 +01:00
|
|
|
#include <iostream>
|
2024-07-16 17:36:00 +02:00
|
|
|
#endif
|
2024-01-06 08:51:00 +01:00
|
|
|
#ifdef MP_UNITS_MODULES
|
|
|
|
|
import mp_units;
|
|
|
|
|
#else
|
2024-04-24 20:53:54 +02:00
|
|
|
#include <mp-units/framework.h>
|
2025-12-13 13:18:59 +01:00
|
|
|
#include <mp-units/math.h>
|
2023-05-25 12:47:10 +02:00
|
|
|
#include <mp-units/systems/isq/space_and_time.h>
|
2024-04-25 16:30:52 +02:00
|
|
|
#include <mp-units/systems/si.h>
|
2024-01-06 08:51:00 +01:00
|
|
|
#endif
|
2020-09-04 23:00:57 +02:00
|
|
|
|
2019-11-16 18:30:44 +01:00
|
|
|
namespace {
|
|
|
|
|
|
2025-02-11 17:26:19 +01:00
|
|
|
static_assert(mp_units::RepresentationOf<measurement<double>, mp_units::quantity_character::real_scalar>);
|
2024-11-26 14:48:08 +01:00
|
|
|
static_assert(mp_units::RepresentationOf<measurement<double>, mp_units::quantity_character::vector>);
|
2019-11-16 18:30:44 +01:00
|
|
|
|
2019-12-06 12:18:39 +01:00
|
|
|
void example()
|
2019-11-16 18:30:44 +01:00
|
|
|
{
|
2022-12-29 20:18:48 +01:00
|
|
|
using namespace mp_units;
|
|
|
|
|
using namespace mp_units::si::unit_symbols;
|
2019-11-16 18:30:44 +01:00
|
|
|
|
2025-12-13 13:18:59 +01:00
|
|
|
std::cout << "Mass of the Sun: M_sun = " << measurement{19884, 2} * (mag_power<10, 26> * kg) << '\n';
|
|
|
|
|
|
2023-10-06 23:38:04 +02:00
|
|
|
const auto acceleration = isq::acceleration(measurement{9.8, 0.1} * m / s2);
|
2023-10-04 10:00:17 -06:00
|
|
|
const auto time = measurement{1.2, 0.1} * s;
|
2024-09-10 10:15:24 +02:00
|
|
|
const QuantityOf<isq::velocity> auto velocity = acceleration * time;
|
2025-12-13 13:18:59 +01:00
|
|
|
std::cout << "Velocity calculation: V = " << acceleration << " * " << time << " = " << velocity << " = "
|
|
|
|
|
<< velocity.in(km / h) << '\n';
|
2019-12-06 12:18:39 +01:00
|
|
|
|
2023-02-14 12:58:54 +01:00
|
|
|
const auto length = measurement{123., 1.} * m;
|
2025-12-13 13:18:59 +01:00
|
|
|
std::cout << "Scalar multiplication: d = 10 * " << length << " = " << 10 * length << '\n';
|
|
|
|
|
|
|
|
|
|
const auto radius = measurement{5.0, 0.1} * m;
|
2025-12-28 13:35:54 +01:00
|
|
|
const auto circumference = radius * (mag<2> * π);
|
|
|
|
|
const auto area = pow<2>(radius) * π;
|
2025-12-13 13:18:59 +01:00
|
|
|
std::cout << "Radius: r = " << radius << '\n';
|
|
|
|
|
std::cout << "Circular circumference: 2πr = " << circumference << " = " << circumference.in(m) << '\n';
|
|
|
|
|
std::cout << "Circular area: πr² = " << area << " = " << area.in(m2) << '\n';
|
|
|
|
|
|
2025-12-28 13:35:54 +01:00
|
|
|
const auto area_measured = measurement{25.0, 1.0} * π * m2;
|
|
|
|
|
const auto radius_from_area = sqrt(area_measured / π);
|
2025-12-13 13:18:59 +01:00
|
|
|
std::cout << "Radius from area: A = " << area_measured << " -> r = √(A/π) = " << radius_from_area << '\n';
|
2019-11-16 18:30:44 +01:00
|
|
|
}
|
2019-12-06 12:18:39 +01:00
|
|
|
|
2019-12-17 09:12:11 +01:00
|
|
|
} // namespace
|
2019-12-06 12:18:39 +01:00
|
|
|
|
|
|
|
|
int main()
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
example();
|
2019-12-17 09:12:11 +01:00
|
|
|
} catch (const std::exception& ex) {
|
2019-12-06 12:18:39 +01:00
|
|
|
std::cerr << "Unhandled std exception caught: " << ex.what() << '\n';
|
2019-12-17 09:12:11 +01:00
|
|
|
} catch (...) {
|
2019-12-06 12:18:39 +01:00
|
|
|
std::cerr << "Unhandled unknown exception caught\n";
|
|
|
|
|
}
|
|
|
|
|
}
|