source: public/doc/gnu-c/Handling-NaN.html@ 02598c2

Last change on this file since 02598c2 was 02598c2, checked in by Mikhail Kirillov <w96k@…>, on Oct 6, 2022 at 12:36:29 PM

Add gnu-c

  • Property mode set to 100644
File size: 7.1 KB
Line 
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
6licensed to the FSF.)
7
8Permission is granted to copy, distribute and/or modify this document
9under the terms of the GNU Free Documentation License, Version 1.3 or
10any later version published by the Free Software Foundation; with the
11Invariant Sections being "GNU General Public License," with the
12Front-Cover Texts being "A GNU Manual," and with the Back-Cover
13Texts as in (a) below. A copy of the license is included in the
14section entitled "GNU Free Documentation License."
15
16(a) The FSF's Back-Cover Text is: "You have the freedom to copy and
17modify this GNU manual. Buying copies from the FSF supports it in
18developing 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<!--
37a.summary-letter {text-decoration: none}
38blockquote.indentedblock {margin-right: 0em}
39div.display {margin-left: 3.2em}
40div.example {margin-left: 3.2em}
41div.lisp {margin-left: 3.2em}
42kbd {font-style: oblique}
43pre.display {font-family: inherit}
44pre.format {font-family: inherit}
45pre.menu-comment {font-family: serif}
46pre.menu-preformatted {font-family: serif}
47span.nolinebreak {white-space: nowrap}
48span.roman {font-family: initial; font-weight: normal}
49span.sansserif {font-family: sans-serif; font-weight: normal}
50ul.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>
60Next: <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> &nbsp; [<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
69produce undefined results. They have a distinctive property that
70makes them unlike any other floating-point value: they are
71<em>unequal to everything, including themselves</em>! Thus, you can
72write a test for a NaN like this:
73</p>
74<div class="example">
75<pre class="example">if (x != x)
76 printf (&quot;x is a NaN\n&quot;);
77</pre></div>
78
79<p>This test works in GNU C, but some compilers might evaluate that test
80expression as false without properly checking for the NaN value.
81A more portable way to test for NaN is to use the <code>isnan</code>
82function declared in <code>math.h</code>:
83</p>
84<div class="example">
85<pre class="example">if (isnan (x))
86 printf (&quot;x is a NaN\n&quot;);
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
92example, in statistics, such data must be omitted from
93computations. Use of any particular finite value for missing
94data would eventually collide with real data, whereas such data
95could never be a NaN, so it is an ideal marker. Functions that
96deal with collections of data that may have holes can be written
97to test for, and ignore, NaN values.
98</p>
99<p>It is easy to generate a NaN in computations: evaluating <code>0.0 /
1000.0</code> is the commonest way, but <code>Infinity - Infinity</code>,
101<code>Infinity / Infinity</code>, and <code>sqrt (-1.0)</code> also work.
102Functions that receive out-of-bounds arguments can choose to return a
103stored 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>
105exception 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
109stickier, because they never disappear in division. Thus, once a
110NaN appears in a chain of numerical operations, it is almost
111certain to pop out into the final results. The programmer
112has to decide whether that is expected, or whether there is a
113coding or algorithmic error that needs repair.
114</p>
115<p>In general, when function gets a NaN argument, it usually returns a
116NaN. However, there are some exceptions in the math-library functions
117that 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
1220.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
127always 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
131both arguments are NaNs, it returns a NaN, but there is no
132requirement 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
137functions where the result is not representable in real
138arithmetic, or is mathematically undefined or uncertain, such as
139<code>sqrt (-1.0)</code> and <code>sin (Infinity)</code>. However, note that a
140result that is merely too big to represent should always produce
141an 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>
148Next: <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> &nbsp; [<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>
Note: See TracBrowser for help on using the repository browser.