Sven Hager
Sven Hager

Reputation: 3204

Python create custom NaN

is it possible to create a custom NaN value in Python?

I know a bit about floating point representation and that NaN is a FP value with some special exponent. Therefore, each FP value with that special exponent is a NaN.

To be precise, what I want to do is:

x = make_my_special_NaN(int_seed)
assert math.isnan(x)
assert test_my_special_NaN(x, int_seed)
assert not test_my_special_NaN(float("nan"), int_seed)

The function which I called make_my_special_NaN should take an integer seed which can be used to identify the special NaN value I have (if this is possible).

Having this, it should be possible to test if a given NaN value is the special NaN given that integer seed.

Thanks in advance

Upvotes: 2

Views: 507

Answers (1)

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799210

The f and d format specifiers in struct will allow you to use any bit pattern you like.

Upvotes: 1

Related Questions