1<?xml version="1.0" encoding="ISO-8859-1"?> 2<!DOCTYPE html 3 PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 4 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 5 6<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 7<head> 8 <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> 9 <meta name="AUTHOR" content="pme@gcc.gnu.org (Phil Edwards)" /> 10 <meta name="KEYWORDS" content="HOWTO, libstdc++, GCC, g++, libg++, STL" /> 11 <meta name="DESCRIPTION" content="HOWTO for the libstdc++ chapter 26." /> 12 <meta name="GENERATOR" content="vi and eight fingers" /> 13 <title>libstdc++-v3 HOWTO: Chapter 26: Numerics</title> 14<link rel="StyleSheet" href="../lib3styles.css" type="text/css" /> 15<link rel="Start" href="../documentation.html" type="text/html" 16 title="GNU C++ Standard Library" /> 17<link rel="Prev" href="../25_algorithms/howto.html" type="text/html" 18 title="Algorithms" /> 19<link rel="Next" href="../27_io/howto.html" type="text/html" 20 title="Input/Output" /> 21<link rel="Copyright" href="../17_intro/license.html" type="text/html" /> 22<link rel="Help" href="../faq/index.html" type="text/html" title="F.A.Q." /> 23</head> 24<body> 25 26<h1 class="centered"><a name="top">Chapter 26: Numerics</a></h1> 27 28<p>Chapter 26 deals with building block abstractions to aid in 29 numerical computing: 30</p> 31<ul> 32 <li>Template data structures such as <code>valarray<></code> 33 and <code>complex<></code>. 34 </li> 35 <li>Template numerical functions such as <code>accumulate</code>, 36 <code>inner_product</code>, <code>partial_sum</code>, and 37 <code>adjacent_difference</code>. 38 </li> 39</ul> 40<p>All of the Standard C math functions are of course included in C++, 41 and overloaded versions for <code>long</code>, <code>float</code>, and 42 <code>long double</code> have been added for all of them. 43</p> 44 45<!-- ####################################################### --> 46<hr /> 47<h1>Contents</h1> 48<ul> 49 <li><a href="#1">Complex Number Processing</a></li> 50 <li><a href="#2">Array Processing</a></li> 51 <li><a href="#3">Numerical Functions</a></li> 52 <li><a href="#4">C99</a></li> 53</ul> 54 55<hr /> 56 57<!-- ####################################################### --> 58 59<h2><a name="1">Complex Number Processing</a></h2> 60 <p>Using <code>complex<></code> becomes even more comple- er, sorry, 61 <em>complicated</em>, with the not-quite-gratuitously-incompatible 62 addition of complex types to the C language. David Tribble has 63 compiled a list of C++98 and C99 conflict points; his description of 64 C's new type versus those of C++ and how to get them playing together 65 nicely is 66<a href="http://david.tribble.com/text/cdiffs.htm#C99-complex">here</a>. 67 </p> 68 <p><code>complex<></code> is intended to be instantiated with a 69 floating-point type. As long as you meet that and some other basic 70 requirements, then the resulting instantiation has all of the usual 71 math operators defined, as well as definitions of <code>op<<</code> 72 and <code>op>></code> that work with iostreams: <code>op<<</code> 73 prints <code>(u,v)</code> and <code>op>></code> can read <code>u</code>, 74 <code>(u)</code>, and <code>(u,v)</code>. 75 </p> 76 <p>Return <a href="#top">to top of page</a> or 77 <a href="../faq/index.html">to the FAQ</a>. 78 </p> 79 80<hr /> 81<h2><a name="2">Array Processing</a></h2> 82 <p>One of the major reasons why FORTRAN can chew through numbers so well 83 is that it is defined to be free of pointer aliasing, an assumption 84 that C89 is not allowed to make, and neither is C++98. C99 adds a new 85 keyword, <code>restrict</code>, to apply to individual pointers. The 86 C++ solution is contained in the library rather than the language 87 (although many vendors can be expected to add this to their compilers 88 as an extension). 89 </p> 90 <p>That library solution is a set of two classes, five template classes, 91 and "a whole bunch" of functions. The classes are required 92 to be free of pointer aliasing, so compilers can optimize the 93 daylights out of them the same way that they have been for FORTRAN. 94 They are collectively called <code>valarray</code>, although strictly 95 speaking this is only one of the five template classes, and they are 96 designed to be familiar to people who have worked with the BLAS 97 libraries before. 98 </p> 99 <p>Some more stuff should go here once somebody has time to write it. 100 </p> 101 <p>Return <a href="#top">to top of page</a> or 102 <a href="../faq/index.html">to the FAQ</a>. 103 </p> 104 105<hr /> 106<h2><a name="3">Numerical Functions</a></h2> 107 <p>There are four generalized functions in the <numeric> header 108 that follow the same conventions as those in <algorithm>. Each 109 of them is overloaded: one signature for common default operations, 110 and a second for fully general operations. Their names are 111 self-explanatory to anyone who works with numerics on a regular basis: 112 </p> 113 <ul> 114 <li><code>accumulate</code></li> 115 <li><code>inner_product</code></li> 116 <li><code>partial_sum</code></li> 117 <li><code>adjacent_difference</code></li> 118 </ul> 119 <p>Here is a simple example of the two forms of <code>accumulate</code>. 120 </p> 121 <pre> 122 int ar[50]; 123 int someval = somefunction(); 124 125 // ...initialize members of ar to something... 126 127 int sum = std::accumulate(ar,ar+50,0); 128 int sum_stuff = std::accumulate(ar,ar+50,someval); 129 int product = std::accumulate(ar,ar+50,1,std::multiplies<int>()); 130 </pre> 131 <p>The first call adds all the members of the array, using zero as an 132 initial value for <code>sum</code>. The second does the same, but uses 133 <code>someval</code> as the starting value (thus, <code>sum_stuff == sum + 134 someval</code>). The final call uses the second of the two signatures, 135 and multiplies all the members of the array; here we must obviously 136 use 1 as a starting value instead of 0. 137 </p> 138 <p>The other three functions have similar dual-signature forms. 139 </p> 140 <p>Return <a href="#top">to top of page</a> or 141 <a href="../faq/index.html">to the FAQ</a>. 142 </p> 143 144<hr /> 145<h2><a name="4">C99</a></h2> 146 <p>In addition to the other topics on this page, we'll note here some 147 of the C99 features that appear in libstdc++-v3. 148 </p> 149 <p>The C99 features depend on the <code>--enable-c99</code> configure flag. 150 This flag is already on by default, but it can be disabled by the 151 user. Also, the configuration machinery will disable it if the 152 necessary support for C99 (e.g., header files) cannot be found. 153 </p> 154 <p>As of GCC 3.0, C99 support includes classification functions 155 such as <code>isnormal</code>, <code>isgreater</code>, 156 <code>isnan</code>, etc. 157 The functions used for 'long long' support such as <code>strtoll</code> 158 are supported, as is the <code>lldiv_t</code> typedef. Also supported 159 are the wide character functions using 'long long', like 160 <code>wcstoll</code>. 161 </p> 162 <p>Return <a href="#top">to top of page</a> or 163 <a href="../faq/index.html">to the FAQ</a>. 164 </p> 165 166 167 168<!-- ####################################################### --> 169 170<hr /> 171<p class="fineprint"><em> 172See <a href="../17_intro/license.html">license.html</a> for copying conditions. 173Comments and suggestions are welcome, and may be sent to 174<a href="mailto:libstdc++@gcc.gnu.org">the libstdc++ mailing list</a>. 175</em></p> 176 177 178</body> 179</html> 180