Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check for numpy.float64(0)?

Tags:

python

numpy

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

like image 716
Burhan Avatar asked Apr 20 '26 10:04

Burhan


1 Answers

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
like image 115
Paddy Harrison Avatar answered Apr 21 '26 23:04

Paddy Harrison



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!