Skip to content
DevMeme
2283 of 7435
Realizing x != x for NaN triggers the cat-stare during code review
CS Fundamentals Post #2540, on Dec 27, 2020 in TG

Realizing x != x for NaN triggers the cat-stare during code review

Why is this CS Fundamentals meme funny?

Level 1: Common Sense vs Computers

The meme is funny because it shows a situation that seems to break common sense: a teacher asks, “When can something not be equal to itself?” Normally, we’d say “That’s impossible – everything is always equal to itself!” It’s like asking, “When is an apple not an apple?” – it sounds like a trick question. In real life, an apple is always the same apple, right? But in computer-land, there is this one strange exception. The computer has a special kind of “number” that actually means “Not a Number.” Imagine you try to do a very strange math problem, like dividing zero by zero – you don’t get a real answer. The computer’s answer to that is basically a big shrug called “NaN” (not a number). And here’s the weird part: since it’s not a real number at all, if you ask “Is this NaN equal to itself?”, the computer says “No, it isn’t.” It’s as if you had a placeholder that says “unknown” – asking “is unknown the same as unknown?” doesn’t make sense, so the computer just says “these are not equal.”

In the picture, the student (represented by the cute grey cat) is getting caught by the teacher. The student answered the teacher’s riddle by writing “NaN” on their paper, which was the correct idea (because that’s the special case where something isn’t equal to itself). But when the teacher asks the student out loud, the poor student guesses “1 or 2” instead – which is wrong and silly. The teacher then says, “Then why did you write NaN on the paper?” meaning “If you thought the answer was 1 or 2, why did you write this other thing down?” The cat’s big round eyes and stunned expression perfectly show how the student feels: busted and confused. It’s like the cat (student) is thinking, “Uh oh, I don’t actually know what I’m talking about, do I?”

Everyone finds this amusing because it captures that moment of complete confusion when you learn that computers can do something that sounds impossible. Even a cat can see something isn’t adding up! The humor is really about that bewildered feeling – the cat and the reader both go, “Huh, how can x not equal x? That’s crazy!” And then we realize it’s because of a special rule in how computers handle numbers. In simple terms: the meme is joking about a crazy exception to the normal rules – so strange that it gives you the same look as a cat staring at a piece of paper in disbelief. It’s a cute way to remember that computers sometimes play by different rules than everyday life, and it makes us laugh to see common sense get turned on its head, with a cat as our relatable stand-in for that “Wait… WHAT?!” reaction.

Level 2: Floating-Point Gotcha

Computers don’t do math exactly like we do on paper — they use binary floating-point arithmetic for most real numbers. A floating-point number is basically a way to represent a decimal or fractional number in binary form (in systems like IEEE-754, which nearly all modern computers follow). But not every result of a calculation can be a real number. Sometimes you get an undefined result. For example, what happens if you ask a computer to evaluate $0/0$ (zero divided by zero) or $\sqrt{-5}$ (square root of a negative number) in the context of real numbers? There isn’t a real number answer to those. Instead of crashing or throwing an error, the computer produces a special value called NaN, which stands for Not a Number.

NaN is a bit like a placeholder saying, “This isn’t a real numeric result.” It’s still of type ‘number’ as far as the computer is concerned (specifically a special value in the float format), but it signifies “no valid number here.” Now, here’s the funky part: because NaN isn’t a real number with a definite value, the normal rules for comparing values are tweaked when NaN is involved. In most programming languages, the expression x != x checks “is x not equal to x?”. Normally, if you plug in any ordinary number for x, that check is false — of course 5 equals 5, 3.14 equals 3.14, and so on. But if x is NaN, then x is not equal to x! In code, NaN != NaN comes out true. This is the only time a value isn’t equal to itself. Another way to say it: NaN == NaN is false, which is really weird at first glance.

Let’s look at a quick example in code to see this in action:

#include <stdio.h>
#include <math.h>

int main() {
    float x = 0.0f / 0.0f;         // 0.0/0.0 produces a NaN value
    printf("x == x ? %s\n", (x == x ? "true" : "false"));   // will print "false"
    printf("x != x ? %s\n", (x != x ? "true" : "false"));   // will print "true"
    return 0;
}

Here we deliberately did 0.0/0.0 which yields NaN in floating-point. The program will output that x == x is false and x != x is true. In plain language it’s saying: “x is not equal to x,” which seems like nonsense but is exactly how NaN behaves. This is a known edge case or pitfall in programming. In fact, most languages provide a more readable way to check for NaN, such as a function like isNaN(x), because writing if(x != x) can confuse readers. But under the hood, that function is effectively doing the same thing: it’s checking the bit pattern of x to see if it’s the NaN pattern (since only a NaN would fail to equal itself).

Now think back to the meme scenario. The teacher’s question “When is x != x?” is essentially asking, “When does a value not equal itself?” The concept you needed to know to answer is exactly this: when that value is NaN. It’s a classic teaching question in computer science to see if students learned about this quirky rule of floating-point numbers. The student should answer: “when x is NaN (Not a Number).” In the meme, the student instead answers “when x equals 1 or 2,” which is completely wrong — there’s nothing special about 1 or 2; if x=1, then x==x (1==1) would be true. The joke here is that the student either panicked or had no clue, so they just threw out a random answer. Yet apparently, on the written test, this student wrote “NaN” as the answer. The teacher is confronting them, basically saying “If you thought the answer was 1 or 2, why did you write NaN on your paper?” It suggests the student might have written the correct answer by accident or cheating, without understanding it.

This mix-up is funny to developers and CS students because it exaggerates a common learning experience: sometimes you memorize the fact that NaN is not equal to itself, but until you truly understand it, it feels bizarre. Seeing NaN written down might also be humorous because in a normal math class, writing “Not a Number” as an answer would look absurd. Imagine a math test asking “when does x not equal x?” and someone literally writes “Not a Number” – a teacher unfamiliar with programming would be totally bewildered! Here though, it’s a computer science context, and the teacher expected the term NaN as the correct answer.

Let’s clarify a couple of terms for newcomers:

  • Floating-point: This refers to how computers represent real numbers (numbers with fractional parts). Common examples are float or double types in languages like C, Java, or Python’s float. They’re called floating-point because the decimal (or binary) point “floats” – the format uses a fixed number of binary digits to represent a number in scientific notation. This lets computers handle a wide range of values (very large or very small), but it also introduces quirks like rounding errors and special values.
  • NaN (Not a Number): A special value defined by the IEEE-754 floating-point standard. It’s the result you get from operations that don’t produce a meaningful numeric answer (like 0/0, infinity minus infinity, etc.). Think of it as an official “error value” for numbers. Once a calculation turns into NaN, it tends to propagate – e.g., NaN plus 5 is NaN, NaN times 100 is NaN, etc. It’s the computer’s way of saying “I can’t give you a real number here, so I’ll give you this token that signifies undefined.”
  • Equality operator (==) and Inequality operator (!=): These are basic comparison operators in programming. == checks if two values are equal. != checks if two values are not equal. Under normal circumstances, any value compared to itself with == would return true (because any number equals itself). The inequality != would return false for the same thing (because any number is not not equal to itself). NaN is the one big exception in the case of floating-point numbers: NaN == NaN is false, and NaN != NaN is true. This is explicitly defined in the rules governing floating-point computations.

For a junior developer or student, the first encounter with this can be really confusing. You might be debugging a program and see that a variable x isn’t passing a check like if(x == x). You scratch your head: “How on earth could x not equal x?!” Then you learn about NaN and it clicks — oh, x must be some invalid number. In fact, this peculiarity is often used as a quick-and-dirty way to detect NaN if you don’t have a function for it: just test if(x != x). If it’s true, you’ve got a NaN on your hands. To avoid confusion, higher-level languages implement clearer ways to check, but understanding this low-level truth helps you appreciate why those functions exist.

The meme’s imagery with the cat is a lighthearted representation of that “mind blown” moment. The cat’s dramatic stare is basically saying “What kind of crazy rule is that?!” As a newcomer, you empathize with the cat: it’s exactly how you feel when a teacher or mentor reveals this rule. And as you learn, you also realize it’s a bug prevention lesson: if you ever see x != x in code, don’t hastily delete it or assume it’s a mistake — it might be intentionally handling the NaN case! Conversely, if you need to account for invalid numeric results, remember that check (or use the proper isNaN function) so you aren’t caught off guard.

So, in summary: this meme uses a funny Q&A with a teacher and a shocked cat to highlight the one scenario in programming where a value isn’t equal to itself – when that value is NaN. It’s a quintessential floating-point surprise that every developer eventually encounters, usually accompanied by a moment of wide-eyed disbelief (just like that cat!). Once you learn it, you won’t forget it, and you’ll likely tease others with the same question down the line: “Hey, did you know there’s a thing in programming where x != x is true?…”. It’s a bit of computer science trivia that doubles as a cautionary tale for coding with numeric data.

Level 3: NaN-sanity Check

For experienced developers, this meme hits on a classic floating-point gotcha that’s equal parts educational and absurd. The teacher’s trick question – “When is x != x?” – is something you might hear in a computer science class or even during a code review for a numerical algorithm. The expected answer is “when x is NaN.” In other words, the only time a value is not equal to itself in code is when that value is Not a Number. It’s a clever test of fundamental knowledge: anyone who has dealt with IEEE-754 floating-point knows that NaN is this special beast that breaks normal logic.

The humor in the meme comes from the student completely missing the hint and blurting out “When x equals 1 or 2.” 😅 That answer is nonsense (there’s that NaN-sense pun for you). If x=1, then x==x is just 1==1, which is true — so 1 or 2 are certainly not cases where x isn’t equal to itself. The student clearly didn’t understand the question, yet, hilariously, the paper shows they wrote “NaN” as the answer. This implies they might have guessed or copy-pasted the term NaN without truly grasping it. The teacher’s retort, “Then why did you write NaN on the paper?”, calls out that disconnect. It’s a scenario many of us recognize: someone gets the right answer by luck or rote, but their confused explanation reveals they don’t know why it’s right. The cat’s wide-eyed, mildly panicked stare perfectly captures that “uh-oh, I’ve been found out” feeling. It’s the stare of NaN-recognition. 🐱💻

This relates to real coding experiences. Imagine reviewing a colleague’s code and spotting a condition if (x != x) return error;. At first glance, you might do a double-take – it looks like a pointless check because we’re conditioned to think “x is always equal to x.” A junior dev might even try to remove it, thinking it’s a bug or typo. But a senior engineer will immediately recognize this as an is-NaN check – a succinct way to detect the presence of a Not-a-Number value. In fact, before high-level languages provided clear helpers (like isnan(x) or Float.isNaN(x)), this was the idiomatic way to catch NaNs. It’s a bit of quirky code that says, “if x is so invalid that it doesn’t even equal itself, handle that case!” Many a code review comment has gently educated a newbie about this exact line, likely accompanied by an expression not unlike the meme’s cat.

The meme also taps into the shared bewilderment developers experience when first learning about floating-point quirks. We all expect computers to follow strict logic, so discovering that NaN != NaN is true feels almost like programming folklore. It’s a rite-of-passage moment in CS fundamentals: the day you learn that floating-point arithmetic has special values and rules that sometimes defy “common sense.” Senior devs chuckle because they remember being that student or junior — confused at why their if(x == x) check failed or why a value mysteriously wasn’t equal to itself. We’ve seen bugs where a rogue NaN silently propagates through calculations, causing comparisons to act oddly. For example, a sorting function might break if a NaN sneaks in, because suddenly nothing compares correctly with it. The proper handling of NaNs is a mark of robust code in numerically heavy applications.

The two-panel image brilliantly stages this comedic teaching moment. On the left, the “Teacher” (the woman with the paper) is scrutinizing the student’s answer. On the right, the camera zooms in on the “Student” – portrayed by a grey Scottish Fold cat – staring at the paper in wide-eyed alarm. The closed MacBook on the table subtly nudges that this could just as well be a code review scenario – perhaps a mentor reviewing a junior’s code while the junior (the cat) realizes their goof. The cat’s face says: “I wrote what? How did that get there?!” or “I can’t believe that’s actually the correct answer!”. It’s a mix of caught-in-the-act guilt and “mind blown” confusion that many developers know too well when confronted with a tricky bug or concept. In essence, the meme is poking fun at a classic computer science fundamental in a relatable way. It highlights how something as dry as floating-point comparison rules can lead to a genuinely funny scenario – one that has both the teacher and the student (and the cat!) stunned for completely different reasons.

Level 4: Reflexive Rebellion

In pure mathematics, equality is reflexive – meaning any value x always satisfies x == x. However, in the realm of computer arithmetic (specifically the IEEE-754 floating-point standard), a special rebel breaks this rule. Enter NaN (short for Not a Number), a floating-point representation for “undefined” or unrepresentable numerical results. By design, a NaN is never equal to anything – not even itself. This deliberate violation of the reflexive axiom is how IEEE-754 signals “this value isn’t a real number at all.” It’s a bit like a logic puzzle: under IEEE rules, the statement x != x is only true if x is NaN. In formal terms, floating-point equality ceases to be an equivalence relation across all values because the reflexive property is broken for this one outlier. This non-reflexive behavior might sound mathematically heretical, but it was a conscious engineering choice.

Why would engineers invent a value that defies such a basic law? The answer lies in handling invalid operations gracefully. In the early days of computing, an operation like $0 \div 0$ or $\sqrt{-1}$ might trigger exceptions or crash programs. The IEEE-754 committee (led by pioneers like William Kahan) introduced NaN in the 1980s as a way for calculations to continue even when a result is undefined. Instead of halting everything, the processor produces a quiet NaN that infects any further math (any operation involving a NaN typically results in another NaN) and makes comparisons unreliable. To prevent false assumptions, they decreed that any comparison involving NaN is false – except ironically “not equal,” which is defined as true for NaN vs NaN. This means if a program asks if a NaN equals itself, the answer is a resounding No! – a simple and efficient signal that “Hey, this value isn’t a real number.”

This little rule has far-reaching consequences in compilers and code optimization. Seasoned C/C++ developers know that an expression like if (x != x) cannot be blindly optimized away as “always false,” because a standards-compliant compiler must respect the possibility that x might be NaN. (Of course, enabling aggressive flags like -ffast-math can unsafe-ly assume No NaNs and break such checks.) The upshot: a snippet that looks logically absurd – if(x != x) – actually has a concrete purpose, and optimizing it incorrectly could introduce bugs. In a sense, NaN turned a fundamental property of equality on its head to serve a practical need. The cost is a weird corner of logic that surprises everyone at least once. It’s a beautiful quirk of computing that even a wide-eyed cat meme can immortalize: an embodiment of the moment theory collides with engineering pragmatism, leaving us with xx as a truth for one very special x.

Description

The meme is split into a text header and a two-panel photo. Header text reads: "Teacher: When is x!=x ?\nMe: When x equals to 1 or 2.\nTeacher: Then why you did you write NaN on the paper?\nMe:". In the left photo a person (face blurred) sits at a dining table holding printed papers while a grey Scottish-fold cat sits beside them, staring at the sheet. A closed space-grey MacBook rests on a red placemat in the foreground. The right photo zooms in on the cat’s wide-eyed, mildly panicked expression as it confronts the paper. The joke relies on IEEE-754 floating-point semantics where the special value NaN is the only value for which the comparison x != x evaluates to true, often surprising students and developers. It humorously captures the classic floating-point edge case and the confusion it causes during teaching or code reviews

Comments

17
Anonymous ★ Top Pick When I hit `if (x != x)` in the hottest loop: sure, it’s the IEEE-754-certified NaN check, but now both the branch predictor and every reviewer look exactly like that cat
  1. Anonymous ★ Top Pick

    When I hit `if (x != x)` in the hottest loop: sure, it’s the IEEE-754-certified NaN check, but now both the branch predictor and every reviewer look exactly like that cat

  2. Anonymous

    After 20 years in the industry, I've learned that explaining IEEE 754 floating-point edge cases to non-programmers is harder than explaining to the board why our microservices need another orchestration layer - at least the board pretends to understand

  3. Anonymous

    The beauty of NaN is that it's the only value in programming that's so special, it refuses to be equal to itself - a perfect metaphor for that one microservice in your architecture that never agrees with its own health checks. IEEE 754 basically said 'undefined behavior deserves undefined equality,' and now we all get to explain to junior devs why `NaN === NaN` returns false while they question their entire understanding of mathematics and reality itself

  4. Anonymous

    IEEE 754's eternal gift: NaN == NaN? False - because even bits need plausible deniability in prod

  5. Anonymous

    x != x - the day IEEE‑754 teaches you equality isn’t reflexive and your dedupe job quietly hoards NaNs

  6. Anonymous

    Math: x! = x -> {1,2}. JavaScript: x!=x -> NaN. Classic postmortem: notation leaked across bounded contexts

  7. @ImJmik 5y

    Never Oh, stop......

  8. @tarasssssssssssssss 5y

    JS jokes or what idk

  9. Deleted Account 5y

    i dont get the maths bit

    1. @bit69tream 5y

      x! is factorial of x

      1. Deleted Account 5y

        oh

      2. @metal_Dash 5y

        Гений

  10. @TTpocT 5y

    А, я думал не равен

  11. @null_po 5y

    If you say a number loudly, then its value will increase: 5 = 5 5! = 120

    1. @freeapp2014 5y

      But if you say it even more loudly, it will go back down 5!! = 15

      1. @x_Arthur_x 5y

        How is that so?

        1. @freeapp2014 5y

          https://en.m.wikipedia.org/wiki/Double_factorial

Use J and K for navigation