On 12/29/19 10:39 PM, David Mertz wrote:
On Sun, Dec 29, 2019 at 10:18 PM Richard Damon <rich...@damon-family.org <mailto:rich...@damon-family.org>> wrote:

    IEEE total_order puts NaN as bigger than infinity, and -NaN as
    less than -inf.

You mean like this?

>>> def total_order(x):
...     if math.isnan(x):
...         return (math.copysign(1, x), x)
...     return (0, x)
...
...
>>> nums = [1, 2, float('-inf'), float('nan'), float('inf'), float('-nan')]
>>> nums
[1, 2, -inf, nan, inf, nan]
>>> sorted(nums, key=total_order)
[nan, -inf, 1, 2, inf, nan]

It's a little weird that -nan has a repr of 'nan', but bracketing that, my implementation is EXACTLY what you describe.

The difference probably is in places that Python doesn't show, and maybe isn't that important. (like all nans regardless of sign are just NaN)

In the IEEE total order, +0 and -0 are distinct, which your order doesn't handle,

For NaNs, the issue is that NaN is NOT a single representation, but each combination of the sign bit and the 52 mantissa bits (except all zeros which signify infinity) when the exponent field is all ones is a different NaN (and the MSB of the mantissa indicates quiet or signalling). you code makes all these values unordered with respect to each other as opposed to having a distinct strict order. That probably isn't that important of a distinction.

Thus your total_order, while not REALLY a total order, is likely good enough for most purposes. Unless the code has done something to create specific NaNs, and then does something to recover that information, the fact that the NaNs aren't properly sorted within themselves probably isn't an issue.  There is a possible issue that depending on the exact sort algorithm, if the data includes two (or more) NaNs with the same sign, then their lack of order might give the sort difficulties.

--
Richard Damon
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/BGCBDQIVLZD4NQ75QNALN65KNVKOR75O/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to