Skip to content

Cross-kind casting in asarray() #264

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

Closed
asmeurer opened this issue Sep 13, 2021 · 5 comments · Fixed by #290
Closed

Cross-kind casting in asarray() #264

asmeurer opened this issue Sep 13, 2021 · 5 comments · Fixed by #290
Labels
topic: Creation Array creation. topic: Type Promotion Type promotion.
Milestone

Comments

@asmeurer
Copy link
Member

Should asarray() allow arbitrary casting between dtypes if you call asarray(x, dtype=dtype)? It isn't really clear in the spec if, e.g., asarray(x, dtype=bool) should be required to work if x is an integer or float array, or asarray(x, dtype=float64) if x is integer, and so on. My feeling is that it definitely should work if dtype is a promotable type from x.dtype, but I don't know if casts that aren't required to promote should also be required to work.

@rgommers
Copy link
Member

I'd expect this to work, because it's explicit. Unless there's an obvious alternative, like x.astype() (which we don't have).

@asmeurer
Copy link
Member Author

This is also relevant for the *_like creation functions, which all accept a dtype parameter. https://data-apis.org/array-api/latest/API_specification/creation_functions.html

@asmeurer
Copy link
Member Author

I guess there's actually three levels of dtype casting:

  • Promotable casting (x.dtype can be promoted to dtype in the type promotion table
  • Same-kind casting (x.dtype is the same kind as dtype but may be a downcast, like int64 -> int8 or uint64 -> int64)
  • Cross-kind casting (x.dtype and dtype are different kinds (integer, floating-point, boolean, eventually complex)).

Presumably at the very least promotion should be allowed, otherwise the dtype parameter is useless, especially for the *_like functions. The other two involve losing information for some values (except for bool -> other).

@kgryte
Copy link
Contributor

kgryte commented Sep 18, 2021

@asmeurer I am not seeing how the *_like functions are affected by cross-kind casting, as discussed in this issue. Maybe I am missing something, but the *_like functions don't perform casting; the data of the input array is not touched.

@asmeurer
Copy link
Member Author

Oh yeah you are right about the _like functions (those always confuse me tbh because they seem rather pointless, but that's neither here nor there).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: Creation Array creation. topic: Type Promotion Type promotion.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants