4 ms·
Does NaN have an "order" in the set of reals or integers or whatever? I would have no idea what to expect from `min(NaN, x)` or max same. But is it specified by
by jrochkind1 6y ago
Does NaN have an "order" in the set of reals or integers or whatever? I would have no idea what to expect from `min(NaN, x)` or max same. But is it specified by an IEEE standard or something?
- loeg 6y agoNo, the only valid result for NaN is NaN. Saturating range bounds are for real numbers.
- xxs 6y agoBoth min and max should return NaN, if any of their parameters is NaN. Sorting can be defined where to place the NaNs (head/tail) but it's largely irrelevant in this case as simply the substitution won't be permitted by any compiler. NaN is part of IEEE754 but of course it's not a 'real' number (integer numbers don't have NaNs) Edit: you can consider NaN (and to a degree both infinities) as an exception, once it occurs - it has to be propagated. Any operation involving NaN should be returning NaN, any operation comparing NaN to anything has to return 'false'. That includes "if (NaN == NaN)". boolean isNaN(double d) is effectively "return d != d;"
- noctune 6y ago> Both min and max should return NaN, if any of their parameters is NaN. That's not a given, though. IEEE 754-2008 defined min and max as returning the non-NaN parameter. They have been removed in IEEE 754-2019 though.
- xxs 6y agoThe reference to IEEE 754 is made later on, mostly to answer the question posted. I meant regular functions in C alike languages - Math.min/max - java/javascript, fmin/fmax - C++. They do the "right" thing to propagate the NaN
- thaumasiotes 6y agoNo, it breaks the ordering requirements. NaN compares greater than and less than every number. You can say that a call to max should return NaN if any argument is NaN, but you can't say the same about sorting. (For one thing... sorting an array doesn't return a scalar value.) Sorting is done with comparisons, and what happens if a NaN gets into the list of values will depend on which specific comparisons happen to be done.
- monktastic1 6y ago> Does NaN have an "order" in the set of reals or integers or whatever? By definition, something that is not a number (real, integer, etc.) cannot be compared to something that is a number.
- shakow 6y agoIt depends on what space you're working on (e.g. the https://en.wikipedia.org/wiki/Extended_real_number_line https://en.wikipedia.org/wiki/Extended_real_number_line define an order on the real field union {-∞, +∞}).
- monktastic1 6y agoYes, but in that context, ∞ is a number. We often interpret "NaN" to mean "infinity," but it only means "not a number." Maybe I'm being pedantic, but if we want a token representing infinity as a number, it ought not be called "not a number."
- pmiller2 6y agoIEEE754 has both infinity and NaN. They are different. NaN is always the result of an invalid operation, such as trying to take the square root of a negative number. Infinity is for when the result would be valid, but is too large in magnitude to represent. There is both positive and negative infinity.
- Zecc 6y agotypeof NaN
- hxtk 6y agoThe only reason I know what to expect is because Suckerpinch on YouTube made a video in which he managed to define a logic system using NaN and +∞, and does so by abusing min and max, among other expressions: https://youtu.be/5TFDG-y-EHs https://youtu.be/5TFDG-y-EHs