20390, "damianmoz", "Maximum/Minimum of Two Real Numbers", "2022-08-05T12:04:51Z"
The minimum of two real numbers is currently very elegantly coded as
inline proc min(x: real(?w), y: real(w)) return if (x < y) | isnan(x) then x else y;
The following might be better (and that is MIGHT)
inline proc min(x: real(?w), y: real(w)) return if (x < y) | (x != x) then x else y; ccc as it is technically more correct and more elegant. The only reason that the isnan function was introduced into the IEEE 754 interface is that some overly aggressive optimizing compilers (mainly in the past) have deluded themselves that the statement ```chapel x != x
is always false when it is actually true (by definition) when the variable x is a Not-a-Number or NaN in IEEE 754 speak.
The Chapel compiler is certainly not one of delusional compilers when it comes to floating point. It understands the concept properly and handles the statement correctly.
On some hardware, it may be slightly slower, on some slightly faster. On my limited testing on current Intel, it is much the same. On older Intel it was slightly slower as it demanded an extra memory access. I will try and do some testing on ARM later and might test some equivalernt C on IBM Power 8 unless someone tells me that Chapel will compile on that architecture.
And there is a similar case for the maximum.
I am curious what makes more sense in the long term.
With the 2019 IEEE 754 standard mandating several minimum/maximum routines, some based on magnitude alone, some handling NaN properly and some, in the name of backwards compatability, handling it wrongly. This programming style issue should at least be bounced around. Just a thought.