Burhan
Burhan

Reputation: 423

How to check for numpy.float64(0)?

I need to filter all values with 0. The data type is numpy.float64. I have tried numpy.float64(0.0001), but is there any way that below code gives me a True?

numpy.float64(0) == 0.0

Upvotes: 2

Views: 2350

Answers (1)

Paddy Harrison
Paddy Harrison

Reputation: 1992

Float values may not equate correctly due to rounding errors. There is a function numpy.isclose which can be used to check for equivalence within a certain tolerance.

import numpy as np

np.float64(0) == 0 # for me
>>> True

# however a small, almost zero, number gives False
np.float64(1e-19) == 0
>>> False

np.isclose(np.float64(0), 0)
>>> True

np.isclose(np.float64(1e-19), 0)
>>> True

Upvotes: 4

Related Questions