GCien
GCien

Reputation: 2349

Dealing with -0 values in a numpy array

For some reason, my data reduction for a fits image has given me -0 values, which I would like to set to 0.

I've attempted to use:

my_array[~numpy.isfinite(my_array)] = 0

All I would like to do is set the -0 values in the corrfact_um2_ext1 array to 0. Just to keep everything in the same format, as I think this may cause a problem in subsequent data reduction steps.

But this just deals with NaN values and sets them to 0. So, I'm halfway there!

Upvotes: 1

Views: 198

Answers (1)

meltdown90
meltdown90

Reputation: 261

Simply adding 0.0 to each floating point number should fix this:

a = np.array([-0.0, 0.0, 1.0, -1.0])
Out[1]: array([-0.,  0.,  1., -1.]

a += 0.0
a
Out[2]: array([ 0.,  0.,  1., -1.])

But note:

np.array(-0.) == np.array(+0.)
Out[3]: True

Does this solve your problem?

Upvotes: 1

Related Questions