1

There are several different types of NaN possible in most floating point representations (e.g. quiet NaNs, signalling NaNs, etc.). I assume this is also true in numpy. I have a specific bit representation of a NaN, defined in C and imported into python. I wish to test whether an array contains entirely this particular floating point bit pattern. Is there any way to do that?

Note that I want to test whether the array contains this particular NaN, not whether it has NaNs in general.

0

1 Answer 1

1

Numpy allows you to have direct access to the bytes in your array. For a simple case you can view nans directly as integers:

quiet_nan1 = np.uint64(0b0111111111111000000000000000000000000000000000000000000000000000)

x = np.arange(10, dtype=np.float64)
x.view(np.uint64)[5] = quiet_nan1

x.view(np.uint64)

Now you can just compare the elements for the bit-pattern of your exact NaN. This version will preserve shape since the elements are the same size.

A more general solution, which would let you with with types like float128 that don't have a corresponding integer analog on most systems, is to use bytes:

quiet_nan1l = np.frombuffer((0b01111111111111111000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000).to_bytes(16, 'big'))

x = np.arange(3 * 4 * 5, dtype=np.float128).reshape3, 4, 5)
x.view(np.uint8).reshape(*x.shape, 16)[2, 2, 3, :] = quiet_nan1l

x.view(np.uint8).reshape(*x.shape, 16)

The final reshape is not strictly necessary, but it is very convenient, since it isolates the original array elements along the last dimension.

In both cases, modifying the view modifies the original array. That's the point of a view.

And if course it goes without saying (which is why I'm saying it), that this applies to any other bit pattern you may want to assign or test for, not just NaNs.

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.