Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

max(a,b) sometimes depends on order of args #117200

Closed
alex77g2 opened this issue Mar 24, 2024 · 6 comments
Closed

max(a,b) sometimes depends on order of args #117200

alex77g2 opened this issue Mar 24, 2024 · 6 comments
Labels
type-bug An unexpected behavior, bug, or error

Comments

@alex77g2
Copy link

alex77g2 commented Mar 24, 2024

Bug report

Bug description:

# Python 3.11.8 (tags/v3.11.8:db85d51, Feb  6 2024, 22:03:32) [MSC v.1937 64 bit (AMD64)] on win32
>>> max(3.5, float('nan'))
3.5
>>> max(float('nan'), 3.5)
nan
>>> f = float('nan')
>>> f == f
False # NAN per definition does equal itself, so this is the test for nan without functions. Same for other compares.

As this is not obvious behaviour (and python wants to be easy to understand), I suggest to mention this here:
https://docs.python.org/3/library/functions.html#max
I'm not sure about considering this a bug, but is is on the edge between bug and confusing behaviour.

CPython versions tested on:

3.11

Operating systems tested on:

Linux, Windows

@alex77g2 alex77g2 added the type-bug An unexpected behavior, bug, or error label Mar 24, 2024
@Eclips4
Copy link
Member

Eclips4 commented Mar 24, 2024

NAN per definition does equal itself, so this is the test for nan without functions. Same for other compares.

IEEE-754 says the opposite:

Any comparison with a NaN is treated as unordered.

@hugovk
Copy link
Member

hugovk commented Mar 24, 2024

Looks like a duplicate of #88536.

If math.nan is the first argument for either max() or min(), the result is always nan, regardless of the other values in the results. However, if nan is in any other position in the arguments list, the result is always what you would expect if nan was not in the arguments list.
...

@hugovk hugovk closed this as not planned Won't fix, can't repro, duplicate, stale Mar 24, 2024
@alex77g2
Copy link
Author

yes, but this is not obvious to each python user. and python is intended to be as friendly or comfortable or easy to understand as possible. No hint in documentation?

@Eclips4
Copy link
Member

Eclips4 commented Mar 24, 2024

yes, but this is not obvious to each python user. and python is intended to be as friendly or comfortable or easy to understand as possible. No hint in documentation?

There's already a hint in documentation: https://docs.python.org/3/library/math.html#math.nan:

Due to the requirements of the IEEE-754 standard, math.nan and float('nan') are not considered to equal to any other numeric value, including themselves.

@alex77g2
Copy link
Author

I mean a hint in max(), min() docu. As not everybody ready everything. I assume many users dont even know about NAN.

@Eclips4
Copy link
Member

Eclips4 commented Mar 24, 2024

I mean a hint in max(), min() docu. As not everybody ready everything. I assume many users dont even know about NAN.

This behaviour currently is under discussion, please read the issue found by Hugo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

3 participants