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>
Copy link
Member

Choose a reason for hiding this comment

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

any reason in particular why you defined it on the individual subclasses, and not directly on floating and integer, like __round__?

Copy link
Member Author

Choose a reason for hiding this comment

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

oh like in floatingtype_methods? I was actually very curious on how it's getting picked up as it's not referenced anywhere else in numpy/. How does that work?

Copy link
Member

Choose a reason for hiding this comment

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

note that integer also includes timedelta64

Copy link
Member

@jorenham jorenham May 16, 2025

Choose a reason for hiding this comment

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

oh like in floatingtype_methods? I was actually very curious on how it's getting picked up as it's not referenced anywhere else in numpy/. How does that work?

I have minimal knowledge of the C codebase actually haha. Maybe some macro magic, or in some generated C sources?

Copy link
Member Author

Choose a reason for hiding this comment

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

It works magically :). Let me ask in slack, it feels like magic as I'm not finding it in the generated source as well. Thanks for catching it though!

Copy link
Member Author

Choose a reason for hiding this comment

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

Ok so it's in

Py@NAME@ArrType_Type.tp_methods = @name@type_methods;
lol. Thanks to Sebastian for pointing it out.

Comment on lines 373 to 386
f"""
{float_name}.__trunc__() -> int

Return the floating point number with the fractional part removed,
leaving the integer part.
{'\n .. versionadded:: 2.3\n' if float_name != 'double' else ''}
Examples
--------
>>> np.{float_name}(np.pi).__trunc__()
3
>>> import math
>>> math.trunc(np.{float_name}(-np.pi))
-3
"""))
Copy link
Member

Choose a reason for hiding this comment

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

a classic .format might be a bit cleaner here, also for the triple {float_name}

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
01 - Enhancement 56 - Needs Release Note. Needs an entry in doc/release/upcoming_changes 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