# Python float vs numpy.float32

463
May 15, 2017, at 07:40 AM

Using `numpy.float32`.

``````t = numpy.float32(.3)
x = numpy.float32(1)
r = numpy.float32(-.3)
_t = t+x+r
_t == 1 # -> False
``````

Using regular Python `float`.

``````t = .3
x = 1
r = -.3
_t = t+x+r
_t == 1 # -> True
``````

Why?

Python float is a C double type: documentation:

Floating point numbers are usually implemented using double in C; information about the precision and internal representation of floating point numbers for the machine on which your program is running is available in `sys.float_info`.

Therefore, you are comparing 32 and 64 precision floating point numbers. The following will work:

``````t = numpy.float64(.3)
x = numpy.float64(1)
r = numpy.float64(-.3)
_t = t+x+r
_t == 1
``````

Floating point values are inherently non-exact on computers. The python default `float` is a what's called a double precision floating point number on most machines according to https://docs.python.org/2/tutorial/floatingpoint.html. `numpy.float32` is a single precision float. It's double precision counterpart is `numpy.float64`. This could explain the difference in this case.

In general floating point numbers shouldn't be compared directly using `==`. You can use `numpy.isclose` to deal with the small errors caused by non-exact floating point representations.

POPULAR ONLINE

### Why I'm getting TypeError

I'm getting 'TypeError: coercing to Unicode: need string or buffer, tuple found' when trying to send data back to serverHere is my code:

245

### Reverse a defaultdict(dict)

If I have a defaultdict(dict):

252

### Support for Enum arguments in argparse

Is there a better way of supporting Enums as types of argparse arguments than this pattern?

433

### Rotate around defined center of rotation image in Python using PyQt5

I am making a speedmeter for GUI window

511