Skip to content

Comment on Demystifying NaN for the Working Programmerparent

Comments

My point was you can have multiple types of “Not a Number” used at the same time.

Defining just error, undefined, positive infinity, and negative infinity is far from an exhaustive list but they obviously should be treated differently in many contexts.

Well let's look at wikipedia's list of things that cause IEEE NaN, since they already split out Infinity.

0/0, ∞/∞, ∞%n, n%0, ∞-∞, results with imaginary components

The first five have no meaningful approximation or way to interact with anything. And there's no good way to pretend a single float is a complex number.

So those results get the "this doesn't exist" treatment. Coder's choice if NaN triggers errors or not.

Would you try to define any more behavior for any of those NaNs?

Only if you wanted to treat them as more severe types of NaN, Aka ∞-∞ - ∞ can evaluate to ∞-∞, but ∞-∞ / 0 = 0/0.

Though really that choice is just about what kind of errors you’re showing users.

AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.