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

feat!: allow clip to have int min or max when x is floating-point #811

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

Conversation

asmeurer
Copy link
Member

@asmeurer asmeurer commented Jun 4, 2024

This is for consistency with operators, which allow combining an int with an array that has a floating-point data type.

See the discussion at #807.

I also mentioned this at the meeting last week.

This is for consistency with operators, which allow combining an int with an
array that has a floating-point data type.

See the discussion at data-apis#807.
This was somewhat implicit, but it should be clearer now that if all three
arguments are arrays that there is a three-way broadcast.
@asmeurer
Copy link
Member Author

asmeurer commented Jun 4, 2024

I also made it clearer that if all three arguments are arrays then there is a three-way broadcast.

@asmeurer
Copy link
Member Author

asmeurer commented Jun 4, 2024

Note that if we decide to fix #807 more generally we may want to just remove the specific language for promotion behavior for Python scalars from clip and fold it into the type promotion page (which would also imply this change).

@rgommers rgommers added the API change Changes to existing functions or objects in the API. label Jun 13, 2024
Copy link
Member

@rgommers rgommers left a comment

Choose a reason for hiding this comment

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

Thanks @asmeurer. A question about the dtype change inline.

I also made it clearer that if all three arguments are arrays then there is a three-way broadcast.

This change seems reasonable to me, and I verified that numpy behaves that way. It may be worth including this in the test suite to ensure this actually is the case for all tested implementations?

@@ -806,7 +806,7 @@ def clip(

- If both ``min`` and ``max`` are ``None``, the elements of the returned array must equal the respective elements in ``x``.
- If a broadcasted element in ``min`` is greater than a corresponding broadcasted element in ``max``, behavior is unspecified and thus implementation-dependent.
- If ``x`` and either ``min`` or ``max`` have different data type kinds (e.g., integer versus floating-point), behavior is unspecified and thus implementation-dependent.
- If ``x`` has an integer data type and either ``min`` or ``max`` is floating-point, behavior is unspecified and thus implementation-dependent.
Copy link
Member

Choose a reason for hiding this comment

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

This isn't just about scalars, as written. What if x is a floating-point array, and min an integer one? Seems ill-defined, and in numpy it upcasts right now:

>>> np.clip(np.ones((3,), dtype=np.float32), a_min=np.zeros((2, 1), dtype=np.int32), a_max=1.5).dtype
dtype('float64')

@kgryte kgryte added this to the v2024 milestone Sep 19, 2024
@kgryte kgryte changed the title Allow clip to have int min or max when x is floating-point feat!: allow clip to have int min or max when x is floating-point Sep 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API change Changes to existing functions or objects in the API.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants