This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: float('nan') is True
Type: enhancement Stage: resolved
Components: Versions: Python 3.9
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: Dennis Sweeney, tim.peters, vpjtqwv0101
Priority: normal Keywords:

Created on 2021-07-29 01:06 by vpjtqwv0101, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (3)
msg398448 - (view) Author: Marty (vpjtqwv0101) Date: 2021-07-29 01:06
I know that there is numpy.isnan() for checking if a value is float('nan') but I think that native python should logically return False in bool(float('nan')).
msg398450 - (view) Author: Dennis Sweeney (Dennis Sweeney) * (Python committer) Date: 2021-07-29 01:57
Do you have a particular use case for this? This is a backwards-incompatible change, and the existing behavior (the only false-y float being 0.0) is very old and well-established.

If this change was implemented, I suspect almost every use of it would benefit from a clarifying comment like

    if x: # check for NaNs
        ...

...so that is a hint to me that using the existing math.isnan() (or numpy.isnan() if you have numpy) is more explicit and therefore more readable.

I also think it would be confusing to have two different floats (0.0 and NaN) both be false-y.
msg398451 - (view) Author: Tim Peters (tim.peters) * (Python committer) Date: 2021-07-29 02:21
Sorry, I'm just going to close this. For values of all numeric types now, `bool(x)` returns the same as `x != type(x)(0)`. Besides being backward-incompatible, making an exception for NaN would be jarringly inconsistent.

Note that you don't need numpy to conveniently check for a NaN anyway; Python's math.isnan() does the same.

If you want to persist, please bring it up on the python-ideas mailing list. If it gains traction there, feel free to re-open this report.
History
Date User Action Args
2022-04-11 14:59:48adminsetgithub: 88933
2021-07-29 02:21:28tim.peterssetstatus: open -> closed

nosy: + tim.peters
messages: + msg398451

resolution: rejected
stage: resolved
2021-07-29 01:57:59Dennis Sweeneysetnosy: + Dennis Sweeney
messages: + msg398450
2021-07-29 01:06:36vpjtqwv0101create