Skip to content

Add methods from the builtin float types to the numpy floating point types #13375

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
2 of 5 tasks
eric-wieser opened this issue Apr 20, 2019 · 8 comments
Open
2 of 5 tasks
Labels
01 - Enhancement Project Possible project, may require specific skills and long commitment

Comments

@eric-wieser
Copy link
Member

eric-wieser commented Apr 20, 2019

Asking for methods that exist on builtin float types but not numpy types gives:

>>> set(dir(float)) - set(dir(np.float32))
{'__getformat__',
 '__getnewargs__',
 '__setformat__',
 '__trunc__',
 'as_integer_ratio',
 'fromhex',
 'hex',
 'is_integer'}

Filtering these to the ones that are not CPython implementation details, we have:

For all of these, the best approach is probably to port the CPython implementations, taking care to handle the extended precision of long doubles.

Note that we get these methods for free on np.float64 due to inheriting from float

@eric-wieser eric-wieser changed the title Add np.longdouble.fromhex Add methods from the builtin float types to the numpy floating point types Apr 20, 2019
@eric-wieser eric-wieser added the Project Possible project, may require specific skills and long commitment label Apr 20, 2019
@eric-wieser
Copy link
Member Author

eric-wieser commented Oct 1, 2020

Cpython has int.is_integer now (python/cpython#6121), so we might want to add is_integer on all np.number subclasses.

@mw66
Copy link

mw66 commented Nov 17, 2020

Need to fix for all the np float types: np.float32, numpy.float16 / np.single, np.half:

$ python
Python 3.6.7 |Anaconda, Inc.| (default, Oct 23 2018, 19:16:44) 
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> a=np.array([1,2,3], dtype=np.float32)
>>> a[0].is_integer()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'numpy.float32' object has no attribute 'is_integer'

>>> a=np.array([1,2,3], dtype=np.half)
>>> type(a[0])
<class 'numpy.float16'>
>>> a[0].is_integer()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'numpy.float16' object has no attribute 'is_integer'

>>> np.__version__
'1.19.4'

@BvB93
Copy link
Member

BvB93 commented Jun 25, 2021

I think we can also add __ceil__ and __floor__ to the list.

@BvB93
Copy link
Member

BvB93 commented Sep 2, 2021

With the merging of #19803 is_integer can be crossed off the list.

@ganesh-k13
Copy link
Member

I'll take a stab at hex and fromhex by referring to python/cpython@65fe25e

@mdickinson
Copy link
Contributor

@ganesh-k13 You should be aware that there was a long-standing corner-case bug in the float.fromhex code, so apply some caution if you're copying the code directly from the original commit to the CPython source. Here's the relevant PR: python/cpython#27834

@ganesh-k13
Copy link
Member

Thanks for the heads up @mdickinson!

gmarkall added a commit to gmarkall/numba that referenced this issue Nov 30, 2022
See numpy/numpy#13375. `__trunc__` is not defined for NumPy scalars
other than `float64`, so we can't call `math.trunc(x)` in the CUDA
simulator for NumPy scalars `x` if they are not `float64` type.
@jorenham
Copy link
Member

Python 3.14 also adds a float.from_number, fyi.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
01 - Enhancement Project Possible project, may require specific skills and long commitment
Projects
None yet
Development

No branches or pull requests

6 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