ArticleslgStudy

computer science

NaN

NaN is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand NaN rather than just read about it. In short: In computing, NaN (), standing for Not a Number, is a particular value of a numeric data type (often a floating-point number) which is undefined as a number, such as the result of 0 0 {\displaystyle {\frac {0}{0}}} . Systematic use of NaNs was introduced by the IEEE 754 floating-point standard in 1985, along with the representation of other non-finite quantities such as infinities.

NaN — main illustration
NaN — illustration

Key takeaways

  • NaN belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect NaN to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of NaN from memory before moving on to harder problems.

Reference excerpt

In computing, NaN (), standing for Not a Number, is a particular value of a numeric data type (often a floating-point number) which is undefined as a number, such as the result of 0 0 {\displaystyle {\frac {0}{0}}} . Systematic use of NaNs was introduced by the IEEE 754 floating-point standard in 1985, along with the representation of other non-finite quantities such as infinities. In mathematics, the result of 0 0 {\displaystyle {\frac {0}{0}}} is typically not defined as a number and may therefore be represented by NaN in computing systems. The square root of a negative number is not a real number, and is therefore also represented by NaN in compliant computing systems. NaNs may also be used to represent missing values in computations. Two separate kinds of NaNs are provided, termed quiet NaNs and signaling NaNs. Quiet NaNs are used to propagate errors resulting from invalid operations or values. Signaling NaNs can support advanced features such as mixing numerical and symbolic computation or other extensions to basic floating-point arithmetic.

Floating point

In floating-point calculations, NaN is not the same as infinity, although both are typically handled as special cases in floating-point representations of real numbers as well as in floating-point operations. An invalid operation is also not the same as an arithmetic overflow (which would return an infinity or the largest finite number in magnitude) or an arithmetic underflow (which would return the smallest normal number in magnitude, a subnormal number, or zero). In the IEEE 754 binary interchange formats, NaNs are encoded with the exponent field filled with ones (like infinity values), and some non-zero number in the trailing significand field (to make them distinct from infinity values); this allows the definition of multiple distinct NaN values, depending on which bits are set in the trailing significand field, but also on the value of the leading sign bit (but applications are not required to provide distinct semantics for those distinct NaN values). For example, an IEEE 754 single precision (32-bit) NaN would be encoded as

where s is the sign (most often ignored in applications) and the x sequence represents a non-zero number (the value zero encodes infinities). In practice, the most significant bit from x is used to determine the type of NaN: "quiet NaN" or "signaling NaN" (see details in Encoding). The remaining bits encode a payload (most often ignored in applications). Floating-point operations other than ordered comparisons normally propagate a quiet NaN (qNaN). Most floating-point operations on a signaling NaN (sNaN) signal the invalid-operation exception; the default exception action is then the same as for qNaN operands and they produce a qNaN if producing a floating-point result. The propagation of quiet NaNs through arithmetic operations allows errors to be detected at the end of a sequence of operations without extensive testing during intermediate stages. For example, if one starts with a NaN and adds 1 five times in a row, each addition results in a NaN, but there is no need to check each calculation because one can just note that the final result is NaN. However, depending on the language and the function, NaNs can silently be removed from a chain of calculations where one calculation in the chain would give a constant result for all other floating-point values. For example, the calculation x0 may produce the result 1, even where x is NaN, so checking only the final result would obscure the fact that a calculation before the x0 resulted in a NaN. In general, then, a later test for a set invalid flag is needed to detect all cases where NaNs are introduced (see Function definition below for further details). In section 6.2 of the old IEEE 754-2008 standard, there are two anomalous functions (the maxNum and minNum functions, which return the maximum and the minimum, respectively, of two operands that are expected to be numbers) that favor numbers — if just one of the operands is a NaN then the value of the other operand is returned. The IEEE 754-2019 revision has replaced these functions as they are not associative (when a signaling NaN appears in an operand).

Comparison with NaN Comparisons are specified by the IEEE 754 standard to take into account possible NaN operands. When comparing two real numbers, or extended real numbers (as in the IEEE 754 floating-point formats), the first number may be either less than, equal to, or greater than the second number. This gives three possible relations. But when at least one operand of a comparison is NaN, this trichotomy does not apply, and a fourth relation is needed: unordered. In particular, two NaN values compare as unordered, not as equal. As specified, the predicates associated with the < {\displaystyle <} , ≤ {\displaystyle \leq } , = {\displaystyle =} , ≥ {\displaystyle \geq } , > {\displaystyle >} mathematical symbols (or equivalent notation in programming languages) return false on an unordered relation. So, for instance, NOT ( x < y ) {\displaystyle (x<y)} is not logically equivalent to x ≥ y {\displaystyle x\geq y} on unordered, i.e. when x {\displaystyle x} or y {\displaystyle y} is NaN, the former returns true while the latter returns false. However, ≠ {\displaystyle \neq } is defined as the negation of = {\displaystyle =} , thus it returns true on unordered.

… excerpt ends here. Continue reading the full article.

Illustrations

NaN: An example of NaN appearing as a calculation error for input N/A on an electronic weather forecast
An example of NaN appearing as a calculation error for input N/A on an electronic weather forecast

Worked examples

Example 1 — a first encounter with NaN

Start with the simplest possible case. Write down what NaN claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to NaN before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about NaN ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of NaN

In research
NaN appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses NaN in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
NaN is common in secondary-school and first-year university syllabi. It links to neighbouring topics Computer arithmetic, Software anomalies, so understanding it makes those chapters shorter.
In everyday life
Look for NaN outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “NaN” →

Affiliate

Preply — study more efficiently by working with a personal tutor. 50% off.

How to study NaN in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what NaN means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain NaN out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is NaN in simple terms?

In computing, NaN (), standing for Not a Number, is a particular value of a numeric data type (often a floating-point number) which is undefined as a number, such as the result of 0 0 {\displaystyle {\frac {0}{0}}} . Systematic use of NaNs was introduced by the IEEE 754 floating-point standard in 1…

Why does NaN matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study NaN?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on NaN.

Tags

  • Computer arithmetic
  • Software anomalies

Keep exploring