2 <!DOCTYPE part PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN"
3 "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd"
6 <part id="manual.numerics" xreflabel="Numerics">
7 <?dbhtml filename="numerics.html"?>
20 <title>Numerics</title>
22 <!-- Chapter 01 : Complex -->
23 <chapter id="manual.numerics.complex" xreflabel="complex">
24 <title>Complex</title>
27 <sect1 id="numerics.complex.processing" xreflabel="complex Processing">
28 <title>complex Processing</title>
31 <para>Using <code>complex<></code> becomes even more comple- er, sorry,
32 <emphasis>complicated</emphasis>, with the not-quite-gratuitously-incompatible
33 addition of complex types to the C language. David Tribble has
34 compiled a list of C++98 and C99 conflict points; his description of
35 C's new type versus those of C++ and how to get them playing together
37 <ulink url="http://david.tribble.com/text/cdiffs.htm#C99-complex">here</ulink>.
39 <para><code>complex<></code> is intended to be instantiated with a
40 floating-point type. As long as you meet that and some other basic
41 requirements, then the resulting instantiation has all of the usual
42 math operators defined, as well as definitions of <code>op<<</code>
43 and <code>op>></code> that work with iostreams: <code>op<<</code>
44 prints <code>(u,v)</code> and <code>op>></code> can read <code>u</code>,
45 <code>(u)</code>, and <code>(u,v)</code>.
51 <!-- Chapter 02 : Generalized Operations -->
52 <chapter id="manual.numerics.generalized_ops" xreflabel="Generalized Ops">
53 <title>Generalized Operations</title>
57 <para>There are four generalized functions in the <numeric> header
58 that follow the same conventions as those in <algorithm>. Each
59 of them is overloaded: one signature for common default operations,
60 and a second for fully general operations. Their names are
61 self-explanatory to anyone who works with numerics on a regular basis:
64 <listitem><para><code>accumulate</code></para></listitem>
65 <listitem><para><code>inner_product</code></para></listitem>
66 <listitem><para><code>partial_sum</code></para></listitem>
67 <listitem><para><code>adjacent_difference</code></para></listitem>
69 <para>Here is a simple example of the two forms of <code>accumulate</code>.
73 int someval = somefunction();
75 // ...initialize members of ar to something...
77 int sum = std::accumulate(ar,ar+50,0);
78 int sum_stuff = std::accumulate(ar,ar+50,someval);
79 int product = std::accumulate(ar,ar+50,1,std::multiplies<int>());
81 <para>The first call adds all the members of the array, using zero as an
82 initial value for <code>sum</code>. The second does the same, but uses
83 <code>someval</code> as the starting value (thus, <code>sum_stuff == sum +
84 someval</code>). The final call uses the second of the two signatures,
85 and multiplies all the members of the array; here we must obviously
86 use 1 as a starting value instead of 0.
88 <para>The other three functions have similar dual-signature forms.
93 <!-- Chapter 03 : Interacting with C -->
94 <chapter id="manual.numerics.c" xreflabel="Interacting with C">
95 <title>Interacting with C</title>
97 <sect1 id="numerics.c.array" xreflabel="Numerics vs. Arrays">
98 <title>Numerics vs. Arrays</title>
100 <para>One of the major reasons why FORTRAN can chew through numbers so well
101 is that it is defined to be free of pointer aliasing, an assumption
102 that C89 is not allowed to make, and neither is C++98. C99 adds a new
103 keyword, <code>restrict</code>, to apply to individual pointers. The
104 C++ solution is contained in the library rather than the language
105 (although many vendors can be expected to add this to their compilers
108 <para>That library solution is a set of two classes, five template classes,
109 and "a whole bunch" of functions. The classes are required
110 to be free of pointer aliasing, so compilers can optimize the
111 daylights out of them the same way that they have been for FORTRAN.
112 They are collectively called <code>valarray</code>, although strictly
113 speaking this is only one of the five template classes, and they are
114 designed to be familiar to people who have worked with the BLAS
120 <sect1 id="numerics.c.c99" xreflabel="C99">
123 <para>In addition to the other topics on this page, we'll note here some
124 of the C99 features that appear in libstdc++.
126 <para>The C99 features depend on the <code>--enable-c99</code> configure flag.
127 This flag is already on by default, but it can be disabled by the
128 user. Also, the configuration machinery will disable it if the
129 necessary support for C99 (e.g., header files) cannot be found.
131 <para>As of GCC 3.0, C99 support includes classification functions
132 such as <code>isnormal</code>, <code>isgreater</code>,
133 <code>isnan</code>, etc.
134 The functions used for 'long long' support such as <code>strtoll</code>
135 are supported, as is the <code>lldiv_t</code> typedef. Also supported
136 are the wide character functions using 'long long', like
137 <code>wcstoll</code>.