-
Notifications
You must be signed in to change notification settings - Fork 87
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
Write a paper on a "number" concept #28
Comments
Maybe start with something for which numeric_limits<>::is_specialized==true
…On Fri 8 Nov 2019, 17:22 Mateusz Pusz, ***@***.***> wrote:
As a dependency to this project we need a std::number concept. It might
be something similar to:
template<typename T>
concept number = std::regular<T> &&
std::totally_ordered<T> &&
requires(T a, T b) {
{ a + b } -> std::same_as<T>;
{ a - b } -> std::same_as<T>;
{ a * b } -> std::same_as<T>;
{ a / b } -> std::same_as<T>;
{ +a } -> std::same_as<T>;
{ -a } -> std::same_as<T>;
{ a += b } -> std::same_as<T&>;
{ a -= b } -> std::same_as<T&>;
{ a *= b } -> std::same_as<T&>;
{ a /= b } -> std::same_as<T&>;
{ T{0} };
};
The above definition is probably wrong and for some operations we should
not verify the return type (a * b might yield a different type than a / b
).
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#28>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAFTGN32QGTCO2FGOITQKILQSWN4RANCNFSM4JK3AZOA>
.
|
Thanks John, this an interesting idea. However, I wouldn't like users to be forced to partially specialize quantity<metre, MyRep> d1(MyRep{1});
quantity<metre, MyRep> d2(MyRep{2});
auto d = d1 + d2; may not compile with instantiation errors inside the library implementation rather than by constraints check for template arguments. This is why I believe that we should try with the above idea for a concept. It does not have to be named
or whatever SG6 experts will decide to be viable here. Please also note that Physical Units Library is not the only customer for such a concept. Many other libraries and utilities might benefit from it (i.e. Linear Algebra). I am really open for feedback and am actively looking for experts' help here. |
BTW, before someone will raise it, please do not discuss here if it is sane for it to be a concept or not as the need for it does not come directly for algorithms. I will provide a dedicated policy paper on this and we will discuss this in Prague. Please, just scope help to make this concept(s) as good and as good for the generic usage as only possible. |
std::number
concept
One more important design point to mention here. The C++ concepts are able to verify syntactic requirements only. They cannot verify semantics by themselves. This is why we should include all possible operations in requires expression constraints. Things like |
Here are some thoughts on a Number concept for a physical quantity valuetype of type T In quantity<u,T> https://en.wikipedia.org/wiki/Magnitude_(mathematics) https://en.wikipedia.org/wiki/Scalar https://en.wikipedia.org/wiki/Vector_(mathematics_and_physics) https://en.wikipedia.org/wiki/Real_number https://en.wikipedia.org/wiki/Dimensionless_quantity A related issue is where one might want to use physical quantity as valuetype: complex<Quantity> or Quantity<unit,complex> (Personally prefer complex<Quantity> , quaternion<\Quantity> Have also found Quantity<unit, Angle> useful mainly for per_second per_second_squared type quantities eg https://github.com/kwikius/quan-trunk/blob/master/quan_matters/examples/angles2.cpp#L32 |
Some thoughts related to ops on Quantities with other types Take 2 types Q1, T2 where for example Q1 is a Quantity Usual issue is multiplicative op { *, /} Q1 op T2 I think there are specifically 3 cases generally in multiplication/division
Maybe there is an order that Universe<T1> <= Universe<T2> ? |
It seems it is impossible to define one number concept. This is why I decided to go with P1813. Maybe only something like this will be discussed in the SG6 numerics group: template<typename T, typename U = T>
concept basic_arithmetic =
std::magma<std::ranges::plus, T, U> &&
std::magma<std::ranges::minus, T, U> &&
std::magma<std::ranges::times, T, U> &&
std::magma<std::ranges::divided_by, T, U>; but time will tell... |
As a dependency to this project we need a "number" concept. By this I do not mean exactly this name or a concept that have all the constraints in one place. It might be divided to smaller concepts but as an aggregate should at least satisfy something along:
The above definition is probably wrong. For some operations we should not verify the return type (
a * b
might yield a different type thana / b
). We also did not account here for%
,++v
,v++
operations that work on integral types but not on a floating-point.The text was updated successfully, but these errors were encountered: