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