Interest in chained relational operations
Hi, The code in question is a simple chained comparison implementation. It works with any type If relational operators for that type is defined. https://github.com/rszengin/chain May some people find it useful?
On 10/21/2016 5:43 PM, Rahman Salim Zengin wrote:
Hi,
The code in question is a simple chained comparison implementation. It works with any type If relational operators for that type is defined.
The syntax is baffling and needs to be explained in the doc. Why is it: 'cmp::chain < 5 <= inx < 15' as opposed to, let's say: 'cmp::chain > 5 <= inx < 15' ? In other words you are evidently using a class cmp::chain to do your magic but the operator you use for your chaining '<' seems arbitrary and a bit confusing.
https://github.com/rszengin/chain
May some people find it useful?
El 21/10/2016 a las 23:43, Rahman Salim Zengin escribió:
Hi,
The code in question is a simple chained comparison implementation. It works with any type If relational operators for that type is defined.
https://github.com/rszengin/chain
May some people find it useful?
I think this can be further evolved to be useful. A couple of comments: * Seconding Edward's answer, using operator < to initiate the comparison chain cmp::chain < 0 < x <= 5 looks arbitrary. Maybe operator << cmp::chain << 0 < x <= 5 or something even less smart cmp::chain{0} < x <= 5 is better. * As it stands, you're not detecting foul-smelling comparisons such as cmp::chain < x <= y >= z // not exactly wrong, but not the mathematical custom or (more importantly) cmp::chain < 5 <= y <= 0 // always false The first can be made not to compile by making Comparator::operator< (and <=) return a type different than and incompatible with the type returned by Comparator::operator> (and >=). As for the second I *think* it can be detected with some constexpr machinery. Joaquín M López Muñoz
Am 21.10.2016 um 23:43 schrieb Rahman Salim Zengin:
Hi,
The code in question is a simple chained comparison implementation. It works with any type If relational operators for that type is defined.
https://github.com/rszengin/chain
May some people find it useful?
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Considering that the actual use-cases are things like "x < y <= z" I'd rather think about this as a set of ternary operators. They would be: < < < <= <= < <= <=
= >
= = >=
Things like x < y > z wouldn't make that sense from a logical point of view - I'd just not allow them. I think the right approach would be something like that: x < ternary(y) < z That way you can limit the amount of operators available, so what is writtern there actually makes sense. I'd actually return bool for the ternary example, but you have one with five values. I wouldn't do it, but you could let the ternary operation return a bool-convertible proxy object, so you could chain up five: 0.4 < ternary(a <= ternary(b) <= c) < 0.6 But I'm not sure there are actually that much use cases; I'd think the main would be, to check if y is in the right range. And then - to top it off - we could add comparisions with a tolerance (should work according to the operator precedence rules): x == y +- ternary(z) and it could be made a relative tolerance. x == y +- ~ ternary(z) Well - those are basically the useful ternary operations I can think of.
Repository: https://github.com/rszengin/chain Project structure is corrected. On 22.10.2016 01:36, Edward Diener wrote:
The syntax is baffling ... Now, there are stricter rules. Only Ascending or Descending ordered chaining is possible. Syntax is simply explained.
On 22.10.2016 11:53, Joaquin M López Muñoz wrote:
... using operator < to initiate the comparison chain looks arbitrary. Maybe operator <<
cmp::chain << 0 < x <= 5 ... Now, operator "<<" is used to initiate Ascending comparison and operator ">>" is used to initiate Descending comparison. * As it stands, you're not detecting foul-smelling comparisons such as
cmp::chain < x <= y >= z // not exactly wrong, but not the mathematical custom Mixed ordered chaining is not allowed anymore. or (more importantly)
cmp::chain < 5 <= y <= 0 // always false ... For now, I could only achieved runtime detection. Working on it. Please see the branch below.
https://github.com/rszengin/chain/tree/detect_always_false On 22.10.2016 13:01, Klemens Morgenstern wrote:
Considering that the actual use-cases are things like "x < y <= z" I'd rather think about this as a set of ternary operators. ... I will consider that. Things like x < y > z wouldn't make that sense from a logical point of view - I'd just not allow them. ... Not allowed anymore. ... But I'm not sure there are actually that much use cases; I'd think the main would be, to check if y is in the right Perhaps. And then - to top it off - we could add comparisions with a tolerance ... In SPICE simulators ABSTOL and RELTOL are parameters used to describe tolerances. It may be:
x == y + abstol(eps) for absolute tolerance and x == y + reltol(eps) for relative tolerance, which eps is epsilon.
participants (4)
-
Edward Diener
-
Joaquin M López Muñoz
-
Klemens Morgenstern
-
Rahman Salim Zengin