forked from mpusz/mp-units
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmeasurement.cpp
More file actions
201 lines (166 loc) · 6.68 KB
/
measurement.cpp
File metadata and controls
201 lines (166 loc) · 6.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// 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.
#include <mp-units/bits/hacks.h>
#include <mp-units/compat_macros.h>
#ifdef MP_UNITS_IMPORT_STD
import std;
#else
#include <cmath>
#include <compare> // IWYU pragma: export
#include <exception>
#include <iostream>
#include <utility>
#endif
#ifdef MP_UNITS_MODULES
import mp_units;
#else
#include <mp-units/framework.h>
#include <mp-units/ostream.h>
#include <mp-units/systems/isq/space_and_time.h>
#include <mp-units/systems/si.h>
#endif
namespace {
template<typename T>
class measurement {
public:
using value_type = T;
measurement() = default;
// NOLINTNEXTLINE(bugprone-easily-swappable-parameters)
constexpr explicit measurement(value_type val, const value_type& err = {}) :
value_(std::move(val)), uncertainty_([&] {
using namespace std;
return abs(err);
}())
{
}
[[nodiscard]] constexpr const value_type& value() const { return value_; }
[[nodiscard]] constexpr const value_type& uncertainty() const { return uncertainty_; }
[[nodiscard]] constexpr value_type relative_uncertainty() const { return uncertainty() / value(); }
[[nodiscard]] constexpr value_type lower_bound() const { return value() - uncertainty(); }
[[nodiscard]] constexpr value_type upper_bound() const { return value() + uncertainty(); }
[[nodiscard]] constexpr measurement operator-() const { return measurement(-value(), uncertainty()); }
[[nodiscard]] friend constexpr measurement operator+(const measurement& lhs, const measurement& rhs)
{
using namespace std;
return measurement{std::in_place, lhs.value() + rhs.value(), hypot(lhs.uncertainty(), rhs.uncertainty())};
}
[[nodiscard]] friend constexpr measurement operator-(const measurement& lhs, const measurement& rhs)
{
using namespace std;
return measurement{std::in_place, lhs.value() - rhs.value(), hypot(lhs.uncertainty(), rhs.uncertainty())};
}
template<typename To, mp_units::Magnitude M>
[[nodiscard]] constexpr measurement<To> scale(std::type_identity<measurement<To>>, M scaling_factor) const
{
constexpr std::type_identity<To> to_value_type;
return measurement<To>{
std::in_place,
mp_units::scale(to_value_type, scaling_factor, value()),
mp_units::scale(to_value_type, scaling_factor, value()),
};
}
template<mp_units::Magnitude M>
[[nodiscard]] constexpr auto scale(M scaling_factor) const
{
return measurement{
std::in_place,
mp_units::scale(scaling_factor, value()),
mp_units::scale(scaling_factor, value()),
};
}
[[nodiscard]] friend constexpr measurement operator*(const measurement& lhs, const measurement& rhs)
{
const auto val = lhs.value() * rhs.value();
using namespace std;
return measurement(val, val * hypot(lhs.relative_uncertainty(), rhs.relative_uncertainty()));
}
[[nodiscard]] friend constexpr measurement operator*(const measurement& lhs, const value_type& value)
{
const auto val = lhs.value() * value;
return measurement(val, val * lhs.relative_uncertainty());
}
[[nodiscard]] friend constexpr measurement operator*(const value_type& value, const measurement& rhs)
{
const auto val = rhs.value() * value;
return measurement(val, val * rhs.relative_uncertainty());
}
[[nodiscard]] friend constexpr measurement operator/(const measurement& lhs, const measurement& rhs)
{
const auto val = lhs.value() / rhs.value();
using namespace std;
return measurement(val, val * hypot(lhs.relative_uncertainty(), rhs.relative_uncertainty()));
}
[[nodiscard]] friend constexpr measurement operator/(const measurement& lhs, const value_type& value)
{
const auto val = lhs.value() / value;
return measurement(val, val * lhs.relative_uncertainty());
}
[[nodiscard]] friend constexpr measurement operator/(const value_type& value, const measurement& rhs)
{
const auto val = value / rhs.value();
return measurement(val, val * rhs.relative_uncertainty());
}
[[nodiscard]] constexpr auto operator<=>(const measurement&) const = default;
friend std::ostream& operator<<(std::ostream& os, const measurement& v)
{
return os << v.value() << " ± " << v.uncertainty();
}
private:
value_type value_{};
value_type uncertainty_{};
// NOLINTNEXTLINE(bugprone-easily-swappable-parameters)
constexpr measurement(std::in_place_t, value_type val, value_type err) :
value_(std::move(val)), uncertainty_(std::move(err))
{
}
};
} // namespace
template<typename T>
constexpr bool mp_units::is_scalar<measurement<T>> = true;
template<typename T>
constexpr bool mp_units::is_vector<measurement<T>> = true;
static_assert(mp_units::RepresentationOf<measurement<int>, mp_units::quantity_character::scalar>);
static_assert(mp_units::RepresentationOf<measurement<double>, mp_units::quantity_character::scalar>);
namespace {
void example()
{
using namespace mp_units;
using namespace mp_units::si::unit_symbols;
const auto acceleration = isq::acceleration(measurement{9.8, 0.1} * m / s2);
const auto time = measurement{1.2, 0.1} * s;
const QuantityOf<isq::velocity> auto velocity = acceleration * time;
std::cout << acceleration << " * " << time << " = " << velocity << " = " << velocity.in(km / h) << '\n';
const auto length = measurement{123., 1.} * m;
std::cout << "10 * " << length << " = " << 10 * length << '\n';
std::cout << "Mass of the Sun: " << measurement{19884, 2} * (mag_power<10, 26> * kg) << '\n';
}
} // namespace
int main()
{
try {
example();
} catch (const std::exception& ex) {
std::cerr << "Unhandled std exception caught: " << ex.what() << '\n';
} catch (...) {
std::cerr << "Unhandled unknown exception caught\n";
}
}