1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
---|
2 | <html>
|
---|
3 | <!-- Copyright (C) 2022 Richard Stallman and Free Software Foundation, Inc.
|
---|
4 |
|
---|
5 | (The work of Trevis Rothwell and Nelson Beebe has been assigned or
|
---|
6 | licensed to the FSF.)
|
---|
7 |
|
---|
8 | Permission is granted to copy, distribute and/or modify this document
|
---|
9 | under the terms of the GNU Free Documentation License, Version 1.3 or
|
---|
10 | any later version published by the Free Software Foundation; with the
|
---|
11 | Invariant Sections being "GNU General Public License," with the
|
---|
12 | Front-Cover Texts being "A GNU Manual," and with the Back-Cover
|
---|
13 | Texts as in (a) below. A copy of the license is included in the
|
---|
14 | section entitled "GNU Free Documentation License."
|
---|
15 |
|
---|
16 | (a) The FSF's Back-Cover Text is: "You have the freedom to copy and
|
---|
17 | modify this GNU manual. Buying copies from the FSF supports it in
|
---|
18 | developing GNU and promoting software freedom." -->
|
---|
19 | <!-- Created by GNU Texinfo 6.7, http://www.gnu.org/software/texinfo/ -->
|
---|
20 | <head>
|
---|
21 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
---|
22 | <title>Handling NaN (GNU C Language Manual)</title>
|
---|
23 |
|
---|
24 | <meta name="description" content="Handling NaN (GNU C Language Manual)">
|
---|
25 | <meta name="keywords" content="Handling NaN (GNU C Language Manual)">
|
---|
26 | <meta name="resource-type" content="document">
|
---|
27 | <meta name="distribution" content="global">
|
---|
28 | <meta name="Generator" content="makeinfo">
|
---|
29 | <link href="index.html" rel="start" title="Top">
|
---|
30 | <link href="Symbol-Index.html" rel="index" title="Symbol Index">
|
---|
31 | <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
|
---|
32 | <link href="Floating-Point-in-Depth.html" rel="up" title="Floating Point in Depth">
|
---|
33 | <link href="Signed-Zeros.html" rel="next" title="Signed Zeros">
|
---|
34 | <link href="Handling-Infinity.html" rel="prev" title="Handling Infinity">
|
---|
35 | <style type="text/css">
|
---|
36 | <!--
|
---|
37 | a.summary-letter {text-decoration: none}
|
---|
38 | blockquote.indentedblock {margin-right: 0em}
|
---|
39 | div.display {margin-left: 3.2em}
|
---|
40 | div.example {margin-left: 3.2em}
|
---|
41 | div.lisp {margin-left: 3.2em}
|
---|
42 | kbd {font-style: oblique}
|
---|
43 | pre.display {font-family: inherit}
|
---|
44 | pre.format {font-family: inherit}
|
---|
45 | pre.menu-comment {font-family: serif}
|
---|
46 | pre.menu-preformatted {font-family: serif}
|
---|
47 | span.nolinebreak {white-space: nowrap}
|
---|
48 | span.roman {font-family: initial; font-weight: normal}
|
---|
49 | span.sansserif {font-family: sans-serif; font-weight: normal}
|
---|
50 | ul.no-bullet {list-style: none}
|
---|
51 | -->
|
---|
52 | </style>
|
---|
53 |
|
---|
54 |
|
---|
55 | </head>
|
---|
56 |
|
---|
57 | <body lang="en">
|
---|
58 | <span id="Handling-NaN"></span><div class="header">
|
---|
59 | <p>
|
---|
60 | Next: <a href="Signed-Zeros.html" accesskey="n" rel="next">Signed Zeros</a>, Previous: <a href="Handling-Infinity.html" accesskey="p" rel="prev">Handling Infinity</a>, Up: <a href="Floating-Point-in-Depth.html" accesskey="u" rel="up">Floating Point in Depth</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Symbol-Index.html" title="Index" rel="index">Index</a>]</p>
|
---|
61 | </div>
|
---|
62 | <hr>
|
---|
63 | <span id="Handling-NaN-1"></span><h3 class="section">28.14 Handling NaN</h3>
|
---|
64 | <span id="index-NaN-in-floating_002dpoint-arithmetic"></span>
|
---|
65 | <span id="index-not-a-number"></span>
|
---|
66 | <span id="index-floating_002dpoint-NaN"></span>
|
---|
67 |
|
---|
68 | <p>NaNs are not numbers: they represent values from computations that
|
---|
69 | produce undefined results. They have a distinctive property that
|
---|
70 | makes them unlike any other floating-point value: they are
|
---|
71 | <em>unequal to everything, including themselves</em>! Thus, you can
|
---|
72 | write a test for a NaN like this:
|
---|
73 | </p>
|
---|
74 | <div class="example">
|
---|
75 | <pre class="example">if (x != x)
|
---|
76 | printf ("x is a NaN\n");
|
---|
77 | </pre></div>
|
---|
78 |
|
---|
79 | <p>This test works in GNU C, but some compilers might evaluate that test
|
---|
80 | expression as false without properly checking for the NaN value.
|
---|
81 | A more portable way to test for NaN is to use the <code>isnan</code>
|
---|
82 | function declared in <code>math.h</code>:
|
---|
83 | </p>
|
---|
84 | <div class="example">
|
---|
85 | <pre class="example">if (isnan (x))
|
---|
86 | printf ("x is a NaN\n");
|
---|
87 | </pre></div>
|
---|
88 |
|
---|
89 | <p>See <a href="https://www.gnu.org/software/libc/manual/html_node/Floating-Point-Classes.html#Floating-Point-Classes">Floating Point Classes</a> in <cite>The GNU C Library Reference Manual</cite>.
|
---|
90 | </p>
|
---|
91 | <p>One important use of NaNs is marking of missing data. For
|
---|
92 | example, in statistics, such data must be omitted from
|
---|
93 | computations. Use of any particular finite value for missing
|
---|
94 | data would eventually collide with real data, whereas such data
|
---|
95 | could never be a NaN, so it is an ideal marker. Functions that
|
---|
96 | deal with collections of data that may have holes can be written
|
---|
97 | to test for, and ignore, NaN values.
|
---|
98 | </p>
|
---|
99 | <p>It is easy to generate a NaN in computations: evaluating <code>0.0 /
|
---|
100 | 0.0</code> is the commonest way, but <code>Infinity - Infinity</code>,
|
---|
101 | <code>Infinity / Infinity</code>, and <code>sqrt (-1.0)</code> also work.
|
---|
102 | Functions that receive out-of-bounds arguments can choose to return a
|
---|
103 | stored NaN value, such as with the <code>NAN</code> macro defined in
|
---|
104 | <code>math.h</code>, but that does not set the <em>invalid operand</em>
|
---|
105 | exception flag, and that can fool some programs.
|
---|
106 | </p>
|
---|
107 | <span id="index-NaNs_002dalways_002dpropagate-rule"></span>
|
---|
108 | <p>Like Infinity, NaNs propagate in computations, but they are even
|
---|
109 | stickier, because they never disappear in division. Thus, once a
|
---|
110 | NaN appears in a chain of numerical operations, it is almost
|
---|
111 | certain to pop out into the final results. The programmer
|
---|
112 | has to decide whether that is expected, or whether there is a
|
---|
113 | coding or algorithmic error that needs repair.
|
---|
114 | </p>
|
---|
115 | <p>In general, when function gets a NaN argument, it usually returns a
|
---|
116 | NaN. However, there are some exceptions in the math-library functions
|
---|
117 | that you need to be aware of, because they violate the
|
---|
118 | <em>NaNs-always-propagate</em> rule:
|
---|
119 | </p>
|
---|
120 | <ul>
|
---|
121 | <li> <code>pow (x, 0.0)</code> always returns <code>1.0</code>, even if <code>x</code> is
|
---|
122 | 0.0, Infinity, or a NaN.
|
---|
123 |
|
---|
124 | </li><li> <code>pow (1, y)</code> always returns <code>1</code>, even if <code>y</code> is a NaN.
|
---|
125 |
|
---|
126 | </li><li> <code>hypot (INFINITY, y)</code> and <code>hypot (-INFINITY, y)</code> both
|
---|
127 | always return <code>INFINITY</code>, even if <code>y</code> is a Nan.
|
---|
128 |
|
---|
129 | </li><li> If just one of the arguments to <code>fmax (x, y)</code> or
|
---|
130 | <code>fmin (x, y)</code> is a NaN, it returns the other argument. If
|
---|
131 | both arguments are NaNs, it returns a NaN, but there is no
|
---|
132 | requirement about where it comes from: it could be <code>x</code>, or
|
---|
133 | <code>y</code>, or some other quiet NaN.
|
---|
134 | </li></ul>
|
---|
135 |
|
---|
136 | <p>NaNs are also used for the return values of math-library
|
---|
137 | functions where the result is not representable in real
|
---|
138 | arithmetic, or is mathematically undefined or uncertain, such as
|
---|
139 | <code>sqrt (-1.0)</code> and <code>sin (Infinity)</code>. However, note that a
|
---|
140 | result that is merely too big to represent should always produce
|
---|
141 | an Infinity, such as with <code>exp (1000.0)</code> (too big) and
|
---|
142 | <code>exp (Infinity)</code> (truly infinite).
|
---|
143 | </p>
|
---|
144 |
|
---|
145 | <hr>
|
---|
146 | <div class="header">
|
---|
147 | <p>
|
---|
148 | Next: <a href="Signed-Zeros.html" accesskey="n" rel="next">Signed Zeros</a>, Previous: <a href="Handling-Infinity.html" accesskey="p" rel="prev">Handling Infinity</a>, Up: <a href="Floating-Point-in-Depth.html" accesskey="u" rel="up">Floating Point in Depth</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Symbol-Index.html" title="Index" rel="index">Index</a>]</p>
|
---|
149 | </div>
|
---|
150 |
|
---|
151 |
|
---|
152 |
|
---|
153 | </body>
|
---|
154 | </html>
|
---|