]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | <html> |
2 | <head> | |
3 | <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> | |
4 | <title>Floating-Point Classification: Infinities and NaNs</title> | |
5 | <link rel="stylesheet" href="../math.css" type="text/css"> | |
6 | <meta name="generator" content="DocBook XSL Stylesheets V1.77.1"> | |
7 | <link rel="home" href="../index.html" title="Math Toolkit 2.5.1"> | |
8 | <link rel="up" href="../utils.html" title="Chapter 2. Floating Point Utilities"> | |
9 | <link rel="prev" href="rounding/modf.html" title="Integer and Fractional Part Splitting (modf)"> | |
10 | <link rel="next" href="sign_functions.html" title="Sign Manipulation Functions"> | |
11 | </head> | |
12 | <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> | |
13 | <table cellpadding="2" width="100%"><tr> | |
14 | <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../boost.png"></td> | |
15 | <td align="center"><a href="../../../../../index.html">Home</a></td> | |
16 | <td align="center"><a href="../../../../../libs/libraries.htm">Libraries</a></td> | |
17 | <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> | |
18 | <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> | |
19 | <td align="center"><a href="../../../../../more/index.htm">More</a></td> | |
20 | </tr></table> | |
21 | <hr> | |
22 | <div class="spirit-nav"> | |
23 | <a accesskey="p" href="rounding/modf.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../utils.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="sign_functions.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a> | |
24 | </div> | |
25 | <div class="section"> | |
26 | <div class="titlepage"><div><div><h2 class="title" style="clear: both"> | |
27 | <a name="math_toolkit.fpclass"></a><a class="link" href="fpclass.html" title="Floating-Point Classification: Infinities and NaNs">Floating-Point Classification: Infinities | |
28 | and NaNs</a> | |
29 | </h2></div></div></div> | |
30 | <h5> | |
31 | <a name="math_toolkit.fpclass.h0"></a> | |
32 | <span class="phrase"><a name="math_toolkit.fpclass.synopsis"></a></span><a class="link" href="fpclass.html#math_toolkit.fpclass.synopsis">Synopsis</a> | |
33 | </h5> | |
34 | <pre class="programlisting"><span class="preprocessor">#define</span> <span class="identifier">FP_ZERO</span> <span class="comment">/* implementation specific value */</span> | |
35 | <span class="preprocessor">#define</span> <span class="identifier">FP_NORMAL</span> <span class="comment">/* implementation specific value */</span> | |
36 | <span class="preprocessor">#define</span> <span class="identifier">FP_INFINITE</span> <span class="comment">/* implementation specific value */</span> | |
37 | <span class="preprocessor">#define</span> <span class="identifier">FP_NAN</span> <span class="comment">/* implementation specific value */</span> | |
38 | <span class="preprocessor">#define</span> <span class="identifier">FP_SUBNORMAL</span> <span class="comment">/* implementation specific value */</span> | |
39 | ||
40 | <span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">></span> | |
41 | <span class="keyword">int</span> <span class="identifier">fpclassify</span><span class="special">(</span><span class="identifier">T</span> <span class="identifier">t</span><span class="special">);</span> | |
42 | ||
43 | <span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">></span> | |
44 | <span class="keyword">bool</span> <span class="identifier">isfinite</span><span class="special">(</span><span class="identifier">T</span> <span class="identifier">z</span><span class="special">);</span> <span class="comment">// Neither infinity nor NaN.</span> | |
45 | ||
46 | <span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">></span> | |
47 | <span class="keyword">bool</span> <span class="identifier">isinf</span><span class="special">(</span><span class="identifier">T</span> <span class="identifier">t</span><span class="special">);</span> <span class="comment">// Infinity (+ or -).</span> | |
48 | ||
49 | <span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">></span> | |
50 | <span class="keyword">bool</span> <span class="identifier">isnan</span><span class="special">(</span><span class="identifier">T</span> <span class="identifier">t</span><span class="special">);</span> <span class="comment">// NaN.</span> | |
51 | ||
52 | <span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">></span> | |
53 | <span class="keyword">bool</span> <span class="identifier">isnormal</span><span class="special">(</span><span class="identifier">T</span> <span class="identifier">t</span><span class="special">);</span> <span class="comment">// isfinite and not denormalised.</span> | |
54 | ||
55 | <span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">\</span><span class="identifier">math</span><span class="special">\</span><span class="identifier">special_functions</span><span class="special">\</span><span class="identifier">fpclassify</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> | |
56 | </pre> | |
57 | <p> | |
58 | to use these functions. | |
59 | </p> | |
60 | <h5> | |
61 | <a name="math_toolkit.fpclass.h1"></a> | |
62 | <span class="phrase"><a name="math_toolkit.fpclass.description"></a></span><a class="link" href="fpclass.html#math_toolkit.fpclass.description">Description</a> | |
63 | </h5> | |
64 | <p> | |
65 | These functions provide the same functionality as the macros with the same | |
66 | name in C99, indeed if the C99 macros are available, then these functions are | |
67 | implemented in terms of them, otherwise they rely on <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">numeric_limits</span><span class="special"><></span></code> to function. | |
68 | </p> | |
69 | <p> | |
70 | Note that the definition of these functions <span class="emphasis"><em>does not suppress the | |
71 | definition of these names as macros by math.h</em></span> on those platforms | |
72 | that already provide these as macros. That mean that the following have differing | |
73 | meanings: | |
74 | </p> | |
75 | <pre class="programlisting"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">;</span> | |
76 | ||
77 | <span class="comment">// This might call a global macro if defined,</span> | |
78 | <span class="comment">// but might not work if the type of z is unsupported</span> | |
79 | <span class="comment">// by the std lib macro:</span> | |
80 | <span class="identifier">isnan</span><span class="special">(</span><span class="identifier">z</span><span class="special">);</span> | |
81 | <span class="comment">//</span> | |
82 | <span class="comment">// This calls the Boost version</span> | |
83 | <span class="comment">// (found via the "using namespace boost::math" declaration)</span> | |
84 | <span class="comment">// it works for any type that has numeric_limits support for type z:</span> | |
85 | <span class="special">(</span><span class="identifier">isnan</span><span class="special">)(</span><span class="identifier">z</span><span class="special">);</span> | |
86 | <span class="comment">//</span> | |
87 | <span class="comment">// As above but with explicit namespace qualification.</span> | |
88 | <span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">isnan</span><span class="special">)(</span><span class="identifier">z</span><span class="special">);</span> | |
89 | <span class="comment">//</span> | |
90 | <span class="comment">// This will cause a compiler error if isnan is a native macro:</span> | |
91 | <span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">isnan</span><span class="special">(</span><span class="identifier">z</span><span class="special">);</span> | |
92 | <span class="comment">// So always use instead:</span> | |
93 | <span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">isnan</span><span class="special">)(</span><span class="identifier">z</span><span class="special">);</span> | |
94 | <span class="comment">//</span> | |
95 | <span class="comment">// You can also add a using statement,</span> | |
96 | <span class="comment">// globally to a .cpp file, or to a local function in a .hpp file.</span> | |
97 | <span class="keyword">using</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">isnan</span><span class="special">;</span> | |
98 | <span class="comment">// so you can write the shorter and less cluttered</span> | |
99 | <span class="special">(</span><span class="identifier">isnan</span><span class="special">)(</span><span class="identifier">z</span><span class="special">)</span> | |
100 | <span class="comment">// But, as above, if isnan is a native macro, this causes a compiler error,</span> | |
101 | <span class="comment">// because the macro always 'gets' the name first, unless enclosed in () brackets.</span> | |
102 | </pre> | |
103 | <p> | |
104 | Detailed descriptions for each of these functions follows: | |
105 | </p> | |
106 | <pre class="programlisting"><span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">></span> | |
107 | <span class="keyword">int</span> <span class="identifier">fpclassify</span><span class="special">(</span><span class="identifier">T</span> <span class="identifier">t</span><span class="special">);</span> | |
108 | </pre> | |
109 | <p> | |
110 | Returns an integer value that classifies the value <span class="emphasis"><em>t</em></span>: | |
111 | </p> | |
112 | <div class="informaltable"><table class="table"> | |
113 | <colgroup> | |
114 | <col> | |
115 | <col> | |
116 | </colgroup> | |
117 | <thead><tr> | |
118 | <th> | |
119 | <p> | |
120 | fpclassify value | |
121 | </p> | |
122 | </th> | |
123 | <th> | |
124 | <p> | |
125 | class of t. | |
126 | </p> | |
127 | </th> | |
128 | </tr></thead> | |
129 | <tbody> | |
130 | <tr> | |
131 | <td> | |
132 | <p> | |
133 | FP_ZERO | |
134 | </p> | |
135 | </td> | |
136 | <td> | |
137 | <p> | |
138 | If <span class="emphasis"><em>t</em></span> is zero. | |
139 | </p> | |
140 | </td> | |
141 | </tr> | |
142 | <tr> | |
143 | <td> | |
144 | <p> | |
145 | FP_NORMAL | |
146 | </p> | |
147 | </td> | |
148 | <td> | |
149 | <p> | |
150 | If <span class="emphasis"><em>t</em></span> is a non-zero, non-denormalised finite | |
151 | value. | |
152 | </p> | |
153 | </td> | |
154 | </tr> | |
155 | <tr> | |
156 | <td> | |
157 | <p> | |
158 | FP_INFINITE | |
159 | </p> | |
160 | </td> | |
161 | <td> | |
162 | <p> | |
163 | If <span class="emphasis"><em>t</em></span> is plus or minus infinity. | |
164 | </p> | |
165 | </td> | |
166 | </tr> | |
167 | <tr> | |
168 | <td> | |
169 | <p> | |
170 | FP_NAN | |
171 | </p> | |
172 | </td> | |
173 | <td> | |
174 | <p> | |
175 | If <span class="emphasis"><em>t</em></span> is a NaN. | |
176 | </p> | |
177 | </td> | |
178 | </tr> | |
179 | <tr> | |
180 | <td> | |
181 | <p> | |
182 | FP_SUBNORMAL | |
183 | </p> | |
184 | </td> | |
185 | <td> | |
186 | <p> | |
187 | If <span class="emphasis"><em>t</em></span> is a denormalised number. | |
188 | </p> | |
189 | </td> | |
190 | </tr> | |
191 | </tbody> | |
192 | </table></div> | |
193 | <pre class="programlisting"><span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">></span> | |
194 | <span class="keyword">bool</span> <span class="identifier">isfinite</span><span class="special">(</span><span class="identifier">T</span> <span class="identifier">z</span><span class="special">);</span> | |
195 | </pre> | |
196 | <p> | |
197 | Returns true only if <span class="emphasis"><em>z</em></span> is not an infinity or a NaN. | |
198 | </p> | |
199 | <pre class="programlisting"><span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">></span> | |
200 | <span class="keyword">bool</span> <span class="identifier">isinf</span><span class="special">(</span><span class="identifier">T</span> <span class="identifier">t</span><span class="special">);</span> | |
201 | </pre> | |
202 | <p> | |
203 | Returns true only if <span class="emphasis"><em>z</em></span> is plus or minus infinity. | |
204 | </p> | |
205 | <pre class="programlisting"><span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">></span> | |
206 | <span class="keyword">bool</span> <span class="identifier">isnan</span><span class="special">(</span><span class="identifier">T</span> <span class="identifier">t</span><span class="special">);</span> | |
207 | </pre> | |
208 | <p> | |
209 | Returns true only if <span class="emphasis"><em>z</em></span> is a <a href="http://en.wikipedia.org/wiki/NaN" target="_top">NaN</a>. | |
210 | </p> | |
211 | <pre class="programlisting"><span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">></span> | |
212 | <span class="keyword">bool</span> <span class="identifier">isnormal</span><span class="special">(</span><span class="identifier">T</span> <span class="identifier">t</span><span class="special">);</span> | |
213 | </pre> | |
214 | <p> | |
215 | Returns true only if <span class="emphasis"><em>z</em></span> is a normal number (not zero, infinite, | |
216 | NaN, or denormalised). | |
217 | </p> | |
218 | <h6> | |
219 | <a name="math_toolkit.fpclass.h2"></a> | |
220 | <span class="phrase"><a name="math_toolkit.fpclass.floating_point_format"></a></span><a class="link" href="fpclass.html#math_toolkit.fpclass.floating_point_format">Floating-point | |
221 | format</a> | |
222 | </h6> | |
223 | <p> | |
224 | If you wish to find details of the floating-point format for any particular | |
225 | processor, there is a program | |
226 | </p> | |
227 | <p> | |
228 | <a href="../../../example/inspect_fp.cpp" target="_top">inspect_fp.cpp</a> | |
229 | </p> | |
230 | <p> | |
231 | by Johan Rade which can be used to print out the processor type, endianness, | |
232 | and detailed bit layout of a selection of floating-point values, including | |
233 | infinity and NaNs. | |
234 | </p> | |
235 | </div> | |
236 | <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> | |
237 | <td align="left"></td> | |
238 | <td align="right"><div class="copyright-footer">Copyright © 2006-2010, 2012-2014 Nikhar Agrawal, | |
239 | Anton Bikineev, Paul A. Bristow, Marco Guazzone, Christopher Kormanyos, Hubert | |
240 | Holin, Bruno Lalande, John Maddock, Jeremy Murphy, Johan Råde, Gautam Sewani, | |
241 | Benjamin Sobotta, Thijs van den Berg, Daryle Walker and Xiaogang Zhang<p> | |
242 | Distributed under the Boost Software License, Version 1.0. (See accompanying | |
243 | file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) | |
244 | </p> | |
245 | </div></td> | |
246 | </tr></table> | |
247 | <hr> | |
248 | <div class="spirit-nav"> | |
249 | <a accesskey="p" href="rounding/modf.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../utils.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="sign_functions.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a> | |
250 | </div> | |
251 | </body> | |
252 | </html> |