-
Notifications
You must be signed in to change notification settings - Fork 91
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix #580 by using a fixed-point implementation for unit conversions using integer representations #615
base: master
Are you sure you want to change the base?
Fix #580 by using a fixed-point implementation for unit conversions using integer representations #615
Changes from 25 commits
cf9c05f
3635260
74f30da
2f54c76
e003a58
60c94da
d00b330
99d4315
653d3d2
ed2574f
e688ffc
55d8fd6
38dcf64
ad76149
95cc9f3
5f8eb5c
1b57404
f673619
f642d37
b6a6752
647ce6b
464ecd4
e933be7
6873c8b
65a0ee4
0c1971e
4ef0210
35ed472
329b9f5
7fa15d2
e464677
cc9ea9d
a51462c
f4c8e90
01f44c6
5713243
ff11878
b35e241
ef0e7b3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -70,15 +70,37 @@ class measurement { | |||||
[[nodiscard]] friend constexpr measurement operator+(const measurement& lhs, const measurement& rhs) | ||||||
{ | ||||||
using namespace std; | ||||||
return measurement(lhs.value() + rhs.value(), hypot(lhs.uncertainty(), rhs.uncertainty())); | ||||||
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(lhs.value() - rhs.value(), hypot(lhs.uncertainty(), rhs.uncertainty())); | ||||||
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(); | ||||||
|
@@ -127,15 +149,23 @@ class measurement { | |||||
private: | ||||||
value_type value_{}; | ||||||
value_type uncertainty_{}; | ||||||
|
||||||
// NOLINTNEXTLINE(bugprone-easily-swappable-parameters) | ||||||
constexpr measurement(std::in_place_t, value_type val, value_type err) : | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not the right way of using This is not the case here. We initialize two members with one argument each. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you would like to have in-place like construction for type then probably something like piecewise_construct is needed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I just wanted a tag to select the private constructor, indicating "I know what I'm doing - I guarantee that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can either remove it or use a new dedicated tag. Maybe something similar to: mp-units/example/include/validated_type.h Lines 43 to 44 in 727a898
|
||||||
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 { | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This
type_identity
usage here is quite novel. I understand the rationale, but maybe we can find a more traditional way of doing it? @JohelEGP, do you have any ideas here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The idea comes from
boost::hana::type
. Would be great if the standard had its own dedicated "type tag".