Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
342 views
in Technique[技术] by (71.8m points)

Why is float('nan') not equal to itself in python


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

There are two reasons, one practical and one historical. The historical reason is that this makes it trivial to test for a NaN. If the value isn't equal to itself, it's a NaN.

The practical reason is that NaN is used to indicate various different cases where the value is not defined, and not all undefined values are equal.

Should NaN/NaN equal 1? Think about how crazy that would be. Arguing that NaN should equal itself is only a bit less crazy.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...