News for hackers. Stuff that matters.
Re:Math.min(Math.max(num, min), max) by xxs on Thursday August 20, 2020 @01:33PM (#24225963)
this doesn't work for NaN, e.g {0d, Double.NaN, 1d} returns 1d.
Re:Math.min(Math.max(num, min), max) by jrochkind1 on Thursday August 20, 2020 @01:38PM (#24226039)
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?
Re:Math.min(Math.max(num, min), max) by xxs on Thursday August 20, 2020 @01:41PM (#24226073)
Both 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;"

Re:Math.min(Math.max(num, min), max) by noctune on Thursday August 20, 2020 @05:12PM (#24229045)
> 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.

Re:Math.min(Math.max(num, min), max) by xxs on Thursday August 20, 2020 @05:31PM (#24229256)
The 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
Re:Math.min(Math.max(num, min), max) by loeg on Thursday August 20, 2020 @01:41PM (#24226069)
No, the only valid result for NaN is NaN. Saturating range bounds are for real numbers.
Re:Math.min(Math.max(num, min), max) by thaumasiotes on Thursday August 20, 2020 @02:48PM (#24227033)
No, 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.

Re:Math.min(Math.max(num, min), max) by hxtk on Friday August 21, 2020 @12:33AM (#24231991)
The 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

Re:Math.min(Math.max(num, min), max) by monktastic1 on Thursday August 20, 2020 @02:50PM (#24227051)
> 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.

Re:Math.min(Math.max(num, min), max) by shakow on Thursday August 20, 2020 @03:09PM (#24227369)
It depends on what space you're working on (e.g. the https://en.wikipedia.org/wiki/Extended_real_number_line define an order on the real field union {-∞, +∞}).
Re:Math.min(Math.max(num, min), max) by monktastic1 on Thursday August 20, 2020 @07:07PM (#24230064)
Yes, 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."
Re:Math.min(Math.max(num, min), max) by pmiller2 on Thursday August 20, 2020 @08:46PM (#24230717)
IEEE754 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.
Re:Math.min(Math.max(num, min), max) by Zecc on Friday August 21, 2020 @02:35AM (#24232604)
typeof NaN
Re:Math.min(Math.max(num, min), max) by im3w1l on Thursday August 20, 2020 @02:38PM (#24226869)
So I tried a thing in python3.

    >>> max(float('nan'), 0)
    nan
    >>> max(0, float('nan'))
    0
Numpy works though

    >>> np.maximum(float('nan'), 0)
    nan
    >>> np.maximum(0, float('nan'))
    nan
Edit: Fixed numpy example.
Re:Math.min(Math.max(num, min), max) by rrobukef on Thursday August 20, 2020 @03:37PM (#24227767)
The functions minNum and maxNum ([IEEE 754-2008, 5.3.1, p19]) take two arguments and return the min and max, respectively. They have the special, distinguished property that “if exactly one argument is NaN, they return the other. If both are NaN they return NaN.”

Source: http://tom7.org/nand/nand.pdf

Edit: As of 2019, the formerly required minNum, maxNum, minNumMag, and maxNumMag in IEEE 754-2008 are now deleted due to their non-associativity. [https://en.wikipedia.org/wiki/IEEE_754#2019]

Re:Math.min(Math.max(num, min), max) by dr_zoidberg on Thursday August 20, 2020 @03:13PM (#24227423)
And for this particular use case, numpy has .clip(), that takes:

    np.clip(ndarray, lower_bound, upper_bound)
And handles NaNs correctly.
Re:Math.min(Math.max(num, min), max) by taberiand on Thursday August 20, 2020 @05:12PM (#24229047)
Having a NaN at that point feels like a bug anyway, the solution is probably to check the arguments and throw an exception if NaN is provided (or use an input type that doesn't allow invalid values)
Re:Math.min(Math.max(num, min), max) by xxs on Friday August 21, 2020 @02:58AM (#24232718)
That would depend on what you do with the NaNs. For instance I have been using them extensively in time series data representation to denote a specific entry has no value - think of Saturday and stock/forex markets.
Re:Math.min(Math.max(num, min), max) by moron4hire on Friday August 21, 2020 @08:30AM (#24234318)
That's not what NaN means. It means there is a value, and that value is... Check this out... "Not a Number".

Recording the lack of a value is what null is for.

Re:Math.min(Math.max(num, min), max) by xxs on Friday August 21, 2020 @11:47AM (#24235837)
Null does not pertain to primitive types in java and in C - it'd be zero when applied to a 'double'. (note: you want double[] as backing storage in java and you absolutely do not indirections). Aside that I have quite a good idea how NaN is represented internally and what it does, e.g. you can have several different NaNs that have different representation bitwise. Baring that - NaNs are pretty decent to represent lack of value as all operations with them result into a NaN. In the end NaN is just a composition of bits that the hardware can optimize for.