55

What is the difference between the built in float and numpy.float32?

Example

a = 58682.7578125
print type(a)
print a
print type(numpy.float32(a))
print numpy.float32(a)

Output:

<type 'float'>
58682.7578125
<type 'numpy.float32'>
58682.8

I've found here that numpy.float32 is:

float32 Single precision float: sign bit, 8 bits exponent, 23 bits mantissa

didn't find what the built in float format is.

TheMeaningfulEngineer
  • 15,679
  • 27
  • 85
  • 143

2 Answers2

60

Python's standard float type is a C double: http://docs.python.org/2/library/stdtypes.html#typesnumeric

NumPy's standard numpy.float is the same, and is also the same as numpy.float64.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • 28
    Note that `numpy.float` is just an alias to Python's `float` type. It is not a numpy scalar type like `numpy.float64`. The name is only exposed for backwards compatibility with a very early version of numpy that inappropriately exposed `numpy.float64` as `numpy.float`, causing problems when people did `from numpy import *`. – Robert Kern Jun 06 '13 at 15:54
  • 2
    note that a = np.float32(1), b = np.float64(1), c = np.float(1) then type(a) is numpy.float32, type(b) is numpy.float64, type(c) is float and isinstance(a, float) is False, isinstance(b, float) is True and isinstance(c) is also True. – comte May 24 '18 at 08:47
  • The page you link to currently says “Floating point numbers are **usually** implemented using double in C…” (emphasis added), not that Python’s `float` type **is** a C `double`. Did it say something different when you wrote this? – Eric Postpischil Jun 03 '19 at 11:20
  • 2
    @EricPostpischil: Very old versions of Python literally said "Floating point numbers are implemented using double in C." Now they say "usually implemented using double in C," which I take to mean something like "Most people use a Python implementation written in C, and `double` is the type used there." As for CPython, it always uses `double`, and the source code says "PyFloatObject represents a (double precision) floating point number." Do you know of an implementation that uses something substantively different? – John Zwinck Jun 03 '19 at 15:02
  • Im confused as to why this is accepted, the question is about numpy.float32 – 9 Guy Sep 21 '22 at 17:42
  • 2
    @9Guy: The question says what `numpy.float32` is, and asks to compare it with the builtin `float`. So this answer explains what the built `float` is. – John Zwinck Sep 26 '22 at 18:00
0

Data type-wise numpy floats and built-in Python floats are the same, however boolean operations on numpy floats return np.bool_ objects, which always return False for val is True. Example below:

In [1]: import numpy as np
   ...: an_np_float = np.float32(0.3)
   ...: a_normal_float = 0.3
   ...: print(a_normal_float, an_np_float)
   ...: print(type(a_normal_float), type(an_np_float))

0.3 0.3
<class 'float'> <class 'numpy.float32'>

Numpy floats can arise from scalar output of array operations. If you weren't checking the data type, it is easy to confuse numpy floats for native floats.

In [2]: criterion_fn = lambda x: x <= 0.5
   ...: criterion_fn(a_normal_float), criterion_fn(an_np_float)

Out[2]: (True, True)

Even boolean operations look correct. However the result of the numpy float isn't a native boolean datatype, and thus can't be truthy.


In [3]: criterion_fn(a_normal_float) is True, criterion_fn(an_np_float) is True
Out[3]: (True, False)

In [4]: type(criterion_fn(a_normal_float)), type(criterion_fn(an_np_float))
Out[4]: (bool, numpy.bool_)

According to this github thread, criterion_fn(an_np_float) == True will evaluate properly, but that goes against the PEP8 style guide.

Instead, extract the native float from the result of numpy operations. You can do an_np_float.item() to do it explicitly (ref: this SO post) or simply pass values through float().

tnwei
  • 860
  • 7
  • 15