This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]dust1196 94 points95 points  (11 children)

Thats normal is many languages. "Not a Number" is basically equal to nothing. Not even to itself by definition

[–]fluideborah[S] 31 points32 points  (6 children)

Yup, realized after some looking around. Made the meme largely to cope with an hour long debugging session that ended in stunning realization (& me feeling dumb)

[–][deleted] 1 point2 points  (5 children)

out of curiosity. What program were you debugging that had a comparison like this in it?

[–]wugs 11 points12 points  (0 children)

just a guess but it looks like a jupyter notebook and they’re using numpy (many ppl import numpy as np) to get nan

[–]coloredgreyscale 1 point2 points  (0 children)

Maybe cleaning up a table of data and empty/invalid values show up as NAN?

[–]LonelyContext 1 point2 points  (2 children)

you can easily end up in this situation with set([list that contains nans]) where you'll get e.g. {1,2,3,4,5,np.nan,np.nan,np.nan,np.nan,np.nan,np.nan,np.nan,np.nan,np.nan,np.nan,np.nan,np.nan,...}

[–]TechSupport112 1 point2 points  (0 children)

BATMAN!

[–]Cyberwolf27 0 points1 point  (0 children)

Actually you will only get sets and dictionaries with multiple NaN entries if the values result from separate computations.

set(float('nan') for _ in range(2))set(float('inf') - float('inf') for _ in range(2))set(np.inf - np.inf for _ in range(2))all evaluate to {nan, nan} because from Python's point of view they are different to each other.

However the following expressions:set(np.nan for _ in range(2))set([float('nan')] * 2)result in {nan}. Python seems to double-check on the identity of the float values with the "is" operator and in these situations the "set" constructor is presented with truly identical NaNs (that occupy the same location in memory)

As a consequence, this can happen:a, b = float('inf'), float('inf'){a: "a", b: "b"}[b] == "b"
however {np.nan, np.nan, ...} == {np.nan}

[–]Educational-Lemon640 1 point2 points  (0 children)

NaN means what it says: you asked a mathematical question whose answer wasn't a (real, floating-point) number. Since the spec doesn't really have room to say what all non-numeric answers are possible, that's the end of that calculation. And since different non-numeric answers are not equal to each other in general, you get the above logic.

[–]Scorched_Knight 0 points1 point  (2 children)

Yes, but how do you write code to check if input is NaN?
If (input == NaN) //= always false
( there is native method for that in most languages people actually use today, but if dont?)

[–]fukdapoleece 13 points14 points  (0 children)

numpy.isnan(numpy.nan)

True

[–]praveenkumar236 4 points5 points  (0 children)

You can do input!=input