From: Steven D'Aprano <st...@pearwood.info> 
Sent: 29 December 2019 14:47
To: python-ideas@python.org
Subject: [Python-ideas] Re: Testing for NANs [was Re: Fix statistics.median()?]

On Sun, Dec 29, 2019 at 08:22:49AM -0500, David Mertz wrote:
> Signalling NaN's are a pain because I'd want:
> 
> is_nan(snan) == True
> 
> But statistics.median([1, 2, snan], on_nan='ignore') to raise an exception.

So you want to skip quiet NANs and raise on signalling NANs?

I'd like to understand the use-case here. I guess it is "signalling NANs are an 
error, quiet NANs are missing data". Am I right?

How are the signalling NANs going to get into your data?

As far as I know, there are no operations in Python which will naturally return 
a signalling NAN, you have to inject them into the data yourself:

(1) The float() constructor doesn't accept "snan"; no arithmetic operations or 
math module libraries return signalling NANs, so the only way to create a sNAN 
is using the struct module. But that's not guaranteed to succeed: as per the 
link I gave earlier

https://mail.python.org/archives/list/python-...@python.org/thread/35NECLGFIVAHWTIPAYDBJOJJX3FSY233/

that's platform-dependent. So there's no platform-independent way to get a 
float sNAN, or guarantee that it will work the way you want.

(2) The Decimal constructor does accept "snan", and it it platform- 
independent. But as far as I can tell there's no way to get a sNAN out as the 
result of any Decimal operation. Invalid operations will either return a quiet 
NAN, or raise.

Either way, float or Decimal, the only way to get a sNAN in your data is to put 
it there yourself. Why not just raise at the point of inserting the 
~~landmine~~ sNAN instead?

[Steve Barnes] 
I  do have to disagree here as it is entirely possible, in the world of 
hardware interfacing, that an external hardware device could possibly supply an 
sNaN  as a something was seriously wrong flag, (as opposed to a I haven't got 
any data at the moment). Both CANBus & some variants of MODBUS support various 
sizes (32 & 64 bit) of floating point numbers and some manufacturers may decide 
to use sNaN given that the hardware often has no way of raising exceptions at 
the point of origin. So if you have code that is reading a buffer of values 
from external hardware then it could potentially contain Signalling NaN. I 
personally am not aware of any CAN implementations using this but it is a huge 
and varied field and with IoT getting more so.  I do know that the can 
specifications include other, much better, ways of signalling errors but some 
manufacturers have an "interesting" take on the specifications.
_______________________________________________
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/PH44VWPROPUSCSZL7NEJYTJHRHMWWA4V/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to