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