Skip to content

gh-135853: add math.fmax and math.fmin #135888

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

picnixz
Copy link
Member

@picnixz picnixz commented Jun 24, 2025

I've added math.fmin and math.fmax as I think it'd be nice to have the NaN handling as per C99. I didn't make those functions generic or accept any iterable argument.

WDYT of this proposal @skirpichev @rhettinger?


📚 Documentation preview 📚: https://cpython-previews--135888.org.readthedocs.build/

@picnixz picnixz changed the title gh-135853: add math.fmax and math.fmin gh-135853: add math.fmax and math.fmin [PoC] Jun 24, 2025
@picnixz picnixz force-pushed the feat/math/fmin-fmax-135853 branch from 0831229 to afb0a91 Compare June 24, 2025 14:30
@picnixz picnixz changed the title gh-135853: add math.fmax and math.fmin [PoC] gh-135853: add math.fmax and math.fmin Jun 24, 2025
@picnixz picnixz marked this pull request as ready for review June 24, 2025 15:10
@picnixz picnixz requested a review from skirpichev June 24, 2025 15:10
@picnixz picnixz requested a review from rhettinger June 24, 2025 15:10
Comment on lines +648 to +656
with self.subTest("math.fmax(INF, x)", x=x):
self.assertEqual(math.fmax(INF, x), INF)
with self.subTest("math.fmax(x, INF)", x=x):
self.assertEqual(math.fmax(x, INF), INF)

with self.subTest("math.fmax(NINF, x)", x=x):
self.assertEqual(math.fmax(NINF, x), x)
with self.subTest("math.fmax(x, NINF)", x=x):
self.assertEqual(math.fmax(x, NINF), x)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The traceback contains the line causing the assertion failure, I don't think that it's worth it to repeat it it subTest():

Suggested change
with self.subTest("math.fmax(INF, x)", x=x):
self.assertEqual(math.fmax(INF, x), INF)
with self.subTest("math.fmax(x, INF)", x=x):
self.assertEqual(math.fmax(x, INF), INF)
with self.subTest("math.fmax(NINF, x)", x=x):
self.assertEqual(math.fmax(NINF, x), x)
with self.subTest("math.fmax(x, NINF)", x=x):
self.assertEqual(math.fmax(x, NINF), x)
with self.subTest(x=x):
self.assertEqual(math.fmax(INF, x), INF)
self.assertEqual(math.fmax(x, INF), INF)
self.assertEqual(math.fmax(NINF, x), x)
self.assertEqual(math.fmax(x, NINF), x)

with self.subTest("math.fmax(x, NINF)", x=x):
self.assertEqual(math.fmax(x, NINF), x)

@requires_IEEE_754
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Python requires IEEE 754 and NaN support to build: https://docs.python.org/dev/using/configure.html#build-requirements We should maybe start getting rid of this decorator?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other Python implementations may use this test suite as well.

Comment on lines +694 to +702
with self.subTest("math.fmin(INF, x)", x=x):
self.assertEqual(math.fmin(INF, x), x)
with self.subTest("math.fmin(x, INF)", x=x):
self.assertEqual(math.fmin(x, INF), x)

with self.subTest("math.fmin(NINF, x)", x=x):
self.assertEqual(math.fmin(NINF, x), NINF)
with self.subTest("math.fmin(x, NINF)", x=x):
self.assertEqual(math.fmin(x, NINF), NINF)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
with self.subTest("math.fmin(INF, x)", x=x):
self.assertEqual(math.fmin(INF, x), x)
with self.subTest("math.fmin(x, INF)", x=x):
self.assertEqual(math.fmin(x, INF), x)
with self.subTest("math.fmin(NINF, x)", x=x):
self.assertEqual(math.fmin(NINF, x), NINF)
with self.subTest("math.fmin(x, NINF)", x=x):
self.assertEqual(math.fmin(x, NINF), NINF)
with self.subTest(x=x):
self.assertEqual(math.fmin(INF, x), x)
self.assertEqual(math.fmin(x, INF), x)
self.assertEqual(math.fmin(NINF, x), NINF)
self.assertEqual(math.fmin(x, NINF), NINF)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants