question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

numpy.float() returns non-numpy python builtin float

See original GitHub issue

numpy.float() returns a non-numpy python builtin float type (same goes for numpy.int()):

>>> print type(np.float(0))
<type 'float'>
>>> print type(np.int(0))
<type 'int'>

This is different to behavior of dtype options in array initialization:

>>> print type(np.arange(2, dtype=np.float)[0])
<type 'numpy.float64'>
>>> print type(np.arange(2, dtype=np.int)[0])
<type 'numpy.int64'>

I find this unexpected and surprising. This may have little practical impact as it only affects initialization of single numbers, but it means that e.g. numpy floating point operation error handling is circumvented in this case (which is how I stumbled upon this):

>>> np.seterr(all="ignore")
{'over': 'ignore', 'divide': 'ignore', 'invalid': 'ignore', 'under': 'ignore'}
>>> np.geterr()
{'over': 'ignore', 'divide': 'ignore', 'invalid': 'ignore', 'under': 'ignore'}
>>> np.float(0) / np.float(0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ZeroDivisionError: float division by zero

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
eric-wiesercommented, Aug 1, 2017

#6103 suggests deprecating these, for those following the trail

1reaction
rkerncommented, Nov 1, 2013

Specifically, numpy.float and numpy.int are only present because of backwards compatibility with a very early version of numpy that exposed numpy.float64 and numpy.int32 (or np.int64 on appropriate 64-bit platforms) under those names. It was soon realized that from numpy import * caused the builtin float and int to be obscured. However, because some people had started using dtype=numpy.float explicitly, we kept the name aliased to float such that it would continue working.

Please do not use these aliases. If you see documentation using them, please let us know so we can correct it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Data types — NumPy v1.24 Manual
To convert the type of an array, use the .astype() method (preferred) or the type itself as ... Note that, above, we use...
Read more >
How to check the size of a float in python? - Stack Overflow
Properties of a Python float can be requested via sys.float_info . It returns information such as max/min value, max/min exp value, etc.
Read more >
Using NumPy to Convert Array Elements to Float Type
Method 1 : Here, we can utilize the astype() function that is offered by NumPy. This function creates another copy of the initial...
Read more >
NumPy Data Types - W3Schools
e.g. -1, -2, -3; float - used to represent real numbers. ... The NumPy array object has a property called dtype that returns...
Read more >
Python: Convert float to int in numpy array
I'm trying to convert floats to integer in python and store the integer in ... try: _password+=int(i) except: _password+=i return _password.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found