3

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

1 Answer 1

5

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.