Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
ganesh-k13 wants to merge17 commits intonumpy:main
base:main
Choose a base branch
Loading
fromganesh-k13:trunc

Conversation

ganesh-k13
Copy link
Member

@ganesh-k13ganesh-k13 commentedMay 13, 2025
edited
Loading

Changes

Add__trunc__ for floating and integer types

Testing

>>>np.float32(np.pi).__trunc__()3>>>importmath>>>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 ifgentype_int is slower than an explicitPyLong_FromDouble, etc for floats

@ganesh-k13ganesh-k13 added 01 - Enhancement 62 - Python APIChanges or additions to the Python API. Mailing list should usually be notified. labelsMay 13, 2025
@ganesh-k13ganesh-k13 self-assigned thisMay 13, 2025
@ganesh-k13ganesh-k13 changed the title__trunc__ for floating typesENH:__trunc__ for floating typesMay 13, 2025
Copy link
Member

@jorenhamjorenham 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 reacted with laugh emoji
@ganesh-k13ganesh-k13 added the 56 - Needs Release Note.Needs an entry in doc/release/upcoming_changes labelMay 14, 2025
@jorenham
Copy link
Member

jorenham commentedMay 14, 2025
edited
Loading

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

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

The other rounding dunders are currently also present in bothfloating andinteger.
Thoughts?

@jorenham
Copy link
Member

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

@ganesh-k13
Copy link
MemberAuthor

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-k13ganesh-k13 changed the titleENH:__trunc__ for floating typesENH:__trunc__ for floating and integer typesMay 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 onfloating andinteger, like__round__?

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

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

jorenham reacted with thumbs up emoji
Copy link
Member

Choose a reason for hiding this comment

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

note thatinteger also includestimedelta64

Copy link
Member

@jorenhamjorenhamMay 16, 2025
edited
Loading

Choose a reason for hiding this comment

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

oh like infloatingtype_methods? I was actually very curious on how it's getting picked up as it's not referenced anywhere else innumpy/. 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
MemberAuthor

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!

jorenham reacted with thumbs up emoji
Copy link
MemberAuthor

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 reacted with rocket emoji
@ganesh-k13
Copy link
MemberAuthor

@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-k13ganesh-k13 marked this pull request as ready for reviewMay 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 intyping/tests/data/pass.

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@jorenhamjorenhamjorenham left review comments

Assignees

@ganesh-k13ganesh-k13

Labels
01 - Enhancement56 - Needs Release Note.Needs an entry in doc/release/upcoming_changes62 - Python APIChanges or additions to the Python API. Mailing list should usually be notified.
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

2 participants
@ganesh-k13@jorenham

[8]ページ先頭

©2009-2025 Movatter.jp