Skip to content

ENH: __trunc__ for floating and integer types #28949

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 17 commits into
base: main
Choose a base branch
from

Conversation

ganesh-k13
Copy link
Member

@ganesh-k13 ganesh-k13 commented May 13, 2025

Changes

Add __trunc__ for floating and integer types

Testing

>>> np.float32(np.pi).__trunc__()
3
>>> import math
>>> math.trunc(np.float32(-np.pi))
-3
>>> math.trunc(np.float16(127))
127

Related

part of #13375

Todo

  • release notes
  • Float
    • UT
    • typing
  • int
    • typing
    • UT
  • Run a benchmark to check if gentype_int is slower than an explicit PyLong_FromDouble, etc for floats

@ganesh-k13 ganesh-k13 added 01 - Enhancement 62 - Python API Changes or additions to the Python API. Mailing list should usually be notified. labels May 13, 2025
@ganesh-k13 ganesh-k13 self-assigned this May 13, 2025
@ganesh-k13 ganesh-k13 changed the title __trunc__ for floating types ENH: __trunc__ for floating types May 13, 2025
Copy link
Member

@jorenham jorenham left a comment

Choose a reason for hiding this comment

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

don't forget about the stubs :)

@ganesh-k13 ganesh-k13 added the 56 - Needs Release Note. Needs an entry in doc/release/upcoming_changes label May 14, 2025
@jorenham
Copy link
Member

jorenham commented May 14, 2025

I think it would make sense to also do this for np.integer:

>>> float.__trunc__
<method '__trunc__' of 'float' objects>
>>> int.__trunc__
<method '__trunc__' of 'int' objects>

The other rounding dunders are currently also present in both floating and integer.
Thoughts?

@jorenham
Copy link
Member

the type-test at numpy/typing/tests/data/fail/scalars.pyi:86 should be removed now

@ganesh-k13
Copy link
Member Author

I think it would make sense to also do this for np.integer:

Oh for sure, since the implementation is small, I'll squeeze it in here. Thanks for the info!

the type-test at numpy/typing/tests/data/fail/scalars.pyi:86 should be removed now

Ah that makes sense.

@ganesh-k13 ganesh-k13 changed the title ENH: __trunc__ for floating types ENH: __trunc__ for floating and integer types May 15, 2025
Co-authored-by: Joren Hammudoglu <jhammudoglu@gmail.com>
@ganesh-k13
Copy link
Member Author

@jorenham a basic query on integer's typing: How did it get added? There is no diff indicating it's added:

Help on method_descriptor:

__trunc__(self, /) unbound numpy.integer method
    int8.__trunc__() -> int

@ganesh-k13 ganesh-k13 marked this pull request as ready for review May 19, 2025 04:01
@jorenham
Copy link
Member

@jorenham a basic query on integer's typing: How did it get added? There is no diff indicating it's added:

Help on method_descriptor:

__trunc__(self, /) unbound numpy.integer method
    int8.__trunc__() -> int

I'm not sure I understand you question. Are you asking how to verify that the method you added in the stubs actually works? Because if so, you could do that by adding a type-test in typing/tests/data/pass.

@ganesh-k13
Copy link
Member Author

you could do that by adding a type-test in typing/tests/data/pass

Ah ok, got it. I was actually curious on how the typing for int.trunc got added to begin with. There is no change in int typing, but yet, it got added.

@ganesh-k13 ganesh-k13 removed the 56 - Needs Release Note. Needs an entry in doc/release/upcoming_changes label May 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
01 - Enhancement 62 - Python API Changes or additions to the Python API. Mailing list should usually be notified.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy