2000-04-22 04:33:34 +08:00
|
|
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
|
2001-09-18 07:24:40 +08:00
|
|
|
<html>
|
|
|
|
<head>
|
2001-10-10 04:18:14 +08:00
|
|
|
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
|
|
|
<meta name="AUTHOR" content="pme@gcc.gnu.org (Phil Edwards)">
|
|
|
|
<meta name="KEYWORDS" content="HOWTO, libstdc++, GCC, g++, libg++, STL">
|
|
|
|
<meta name="DESCRIPTION" content="HOWTO for the libstdc++ chapter 26.">
|
|
|
|
<meta name="GENERATOR" content="vi and eight fingers">
|
2001-09-18 07:24:40 +08:00
|
|
|
<title>libstdc++-v3 HOWTO: Chapter 26</title>
|
2001-10-12 02:41:47 +08:00
|
|
|
<link rel="StyleSheet" href="../lib3styles.css">
|
2001-09-18 07:24:40 +08:00
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
|
2001-10-10 04:18:14 +08:00
|
|
|
<h1 class="centered"><a name="top">Chapter 26: Numerics</a></h1>
|
2001-09-18 07:24:40 +08:00
|
|
|
|
|
|
|
<p>Chapter 26 deals with building block abstractions to aid in
|
2000-07-08 05:13:28 +08:00
|
|
|
numerical computing:
|
2001-09-18 07:24:40 +08:00
|
|
|
<ul>
|
|
|
|
<li>Template data structures such as <code>valarray<></code>
|
|
|
|
and <code>complex<></code>.
|
2001-10-10 04:18:14 +08:00
|
|
|
<li>Template numerical functions such as <code>accumulate</code>,
|
|
|
|
<code>inner_product</code>, <code>partial_sum</code>, and
|
2001-09-18 07:24:40 +08:00
|
|
|
<code>adjacent_difference</code>.
|
|
|
|
</ul>
|
2000-07-08 05:13:28 +08:00
|
|
|
All of the Standard C math functions are of course included in C++,
|
2001-09-18 07:24:40 +08:00
|
|
|
and overloaded versions for <code>long</code>, <code>float</code>, and
|
|
|
|
<code>long double</code> have been added for all of them.
|
|
|
|
</p>
|
2000-04-22 04:33:34 +08:00
|
|
|
|
|
|
|
<!-- ####################################################### -->
|
2001-09-18 07:24:40 +08:00
|
|
|
<hr>
|
|
|
|
<h1>Contents</h1>
|
|
|
|
<ul>
|
|
|
|
<li><a href="#1">Complex Number Processing</a>
|
|
|
|
<li><a href="#2">Array Processing</a>
|
|
|
|
<li><a href="#3">Numerical Functions</a>
|
|
|
|
<li><a href="#4">C99</a>
|
|
|
|
</ul>
|
2000-04-22 04:33:34 +08:00
|
|
|
|
2001-09-18 07:24:40 +08:00
|
|
|
<hr>
|
2000-04-22 04:33:34 +08:00
|
|
|
|
|
|
|
<!-- ####################################################### -->
|
|
|
|
|
2001-09-18 07:24:40 +08:00
|
|
|
<h2><a name="1">Complex Number Processing</a></h2>
|
|
|
|
<p>Using <code>complex<></code> becomes even more comple- er, sorry,
|
|
|
|
<em>complicated</em>, with the not-quite-gratuitously-incompatible
|
2000-07-08 05:13:28 +08:00
|
|
|
addition of complex types to the C language. David Tribble has
|
2000-08-29 02:41:24 +08:00
|
|
|
compiled a list of C++98 and C99 conflict points; his description of
|
2000-07-08 05:13:28 +08:00
|
|
|
C's new type versus those of C++ and how to get them playing together
|
|
|
|
nicely is
|
2001-10-10 04:18:14 +08:00
|
|
|
<a href="http://david.tribble.com/text/cdiffs.htm#C99-complex">here</a>.
|
2001-09-18 07:24:40 +08:00
|
|
|
</p>
|
|
|
|
<p><code>complex<></code> is intended to be instantiated with a
|
2000-07-08 05:13:28 +08:00
|
|
|
floating-point type. As long as you meet that and some other basic
|
|
|
|
requirements, then the resulting instantiation has all of the usual
|
2001-09-18 07:24:40 +08:00
|
|
|
math operators defined, as well as definitions of <code>op<<</code>
|
|
|
|
and <code>op>></code> that work with iostreams: <code>op<<</code>
|
|
|
|
prints <code>(u,v)</code> and <code>op>></code> can read <code>u</code>,
|
|
|
|
<code>(u)</code>, and <code>(u,v)</code>.
|
|
|
|
</p>
|
|
|
|
<p>Return <a href="#top">to top of page</a> or
|
|
|
|
<a href="../faq/index.html">to the FAQ</a>.
|
|
|
|
</p>
|
|
|
|
|
|
|
|
<hr>
|
|
|
|
<h2><a name="2">Array Processing</a></h2>
|
|
|
|
<p>One of the major reasons why FORTRAN can chew through numbers so well
|
2000-07-08 05:13:28 +08:00
|
|
|
is that it is defined to be free of pointer aliasing, an assumption
|
2001-10-10 04:18:14 +08:00
|
|
|
that C89 is not allowed to make, and neither is C++98. C99 adds a new
|
|
|
|
keyword, <code>restrict</code>, to apply to individual pointers. The
|
|
|
|
C++ solution is contained in the library rather than the language
|
2000-07-08 05:13:28 +08:00
|
|
|
(although many vendors can be expected to add this to their compilers
|
|
|
|
as an extension).
|
2001-09-18 07:24:40 +08:00
|
|
|
</p>
|
|
|
|
<p>That library solution is a set of two classes, five template classes,
|
2000-07-08 05:13:28 +08:00
|
|
|
and "a whole bunch" of functions. The classes are required
|
|
|
|
to be free of pointer aliasing, so compilers can optimize the
|
|
|
|
daylights out of them the same way that they have been for FORTRAN.
|
2001-09-18 07:24:40 +08:00
|
|
|
They are collectively called <code>valarray</code>, although strictly
|
2000-07-08 05:13:28 +08:00
|
|
|
speaking this is only one of the five template classes, and they are
|
|
|
|
designed to be familiar to people who have worked with the BLAS
|
|
|
|
libraries before.
|
2001-09-18 07:24:40 +08:00
|
|
|
</p>
|
|
|
|
<p>Some more stuff should go here once somebody has time to write it.
|
|
|
|
</p>
|
|
|
|
<p>Return <a href="#top">to top of page</a> or
|
|
|
|
<a href="../faq/index.html">to the FAQ</a>.
|
|
|
|
</p>
|
|
|
|
|
|
|
|
<hr>
|
|
|
|
<h2><a name="3">Numerical Functions</a></h2>
|
|
|
|
<p>There are four generalized functions in the <numeric> header
|
2000-07-08 05:13:28 +08:00
|
|
|
that follow the same conventions as those in <algorithm>. Each
|
|
|
|
of them is overloaded: one signature for common default operations,
|
|
|
|
and a second for fully general operations. Their names are
|
|
|
|
self-explanatory to anyone who works with numerics on a regular basis:
|
2001-09-18 07:24:40 +08:00
|
|
|
<ul>
|
|
|
|
<li><code>accumulate</code>
|
|
|
|
<li><code>inner_product</code>
|
|
|
|
<li><code>partial_sum</code>
|
|
|
|
<li><code>adjacent_difference</code>
|
|
|
|
</ul>
|
|
|
|
</p>
|
|
|
|
<p>Here is a simple example of the two forms of <code>accumulate</code>.
|
2001-10-10 04:18:14 +08:00
|
|
|
<pre>
|
2000-07-08 05:13:28 +08:00
|
|
|
int ar[50];
|
|
|
|
int someval = somefunction();
|
|
|
|
|
|
|
|
// ...initialize members of ar to something...
|
|
|
|
|
|
|
|
int sum = std::accumulate(ar,ar+50,0);
|
|
|
|
int sum_stuff = std::accumulate(ar,ar+50,someval);
|
|
|
|
int product = std::accumulate(ar,ar+50,1,std::multiplies<int>());
|
2001-10-10 04:18:14 +08:00
|
|
|
</pre>
|
2000-07-08 05:13:28 +08:00
|
|
|
The first call adds all the members of the array, using zero as an
|
2001-09-18 07:24:40 +08:00
|
|
|
initial value for <code>sum</code>. The second does the same, but uses
|
|
|
|
<code>someval</code> as the starting value (thus, <code>sum_stuff == sum +
|
|
|
|
someval</code>). The final call uses the second of the two signatures,
|
2000-07-08 05:13:28 +08:00
|
|
|
and multiplies all the members of the array; here we must obviously
|
|
|
|
use 1 as a starting value instead of 0.
|
2001-09-18 07:24:40 +08:00
|
|
|
</p>
|
|
|
|
<p>The other three functions have similar dual-signature forms.
|
|
|
|
</p>
|
|
|
|
<p>Return <a href="#top">to top of page</a> or
|
|
|
|
<a href="../faq/index.html">to the FAQ</a>.
|
|
|
|
</p>
|
|
|
|
|
|
|
|
<hr>
|
|
|
|
<h2><a name="4">C99</a></h2>
|
|
|
|
<p>In addition to the other topics on this page, we'll note here some
|
2001-05-31 05:55:05 +08:00
|
|
|
of the C99 features that appear in libstdc++-v3.
|
2001-09-18 07:24:40 +08:00
|
|
|
</p>
|
|
|
|
<p>The C99 features depend on the <code>--enable-c99</code> configure flag.
|
2001-05-31 05:55:05 +08:00
|
|
|
This flag is already on by default, but it can be disabled by the
|
|
|
|
user. Also, the configuration machinery will disable it if the
|
|
|
|
neccessary support for C99 (e.g., header files) cannot be found.
|
2001-09-18 07:24:40 +08:00
|
|
|
</p>
|
|
|
|
<p>As of GCC 3.0, C99 support includes classification functions
|
2001-10-10 04:18:14 +08:00
|
|
|
such as <code>isnormal</code>, <code>isgreater</code>,
|
|
|
|
<code>isnan</code>, etc.
|
2001-09-18 07:24:40 +08:00
|
|
|
The functions used for 'long long' support such as <code>strtoll</code>
|
|
|
|
are supported, as is the <code>lldiv_t</code> typedef. Also supported
|
2001-05-31 05:55:05 +08:00
|
|
|
are the wide character functions using 'long long', like
|
2001-09-18 07:24:40 +08:00
|
|
|
<code>wcstoll</code>.
|
|
|
|
</p>
|
|
|
|
<p>Return <a href="#top">to top of page</a> or
|
|
|
|
<a href="../faq/index.html">to the FAQ</a>.
|
|
|
|
</p>
|
2001-05-31 05:55:05 +08:00
|
|
|
|
2000-04-22 04:33:34 +08:00
|
|
|
|
|
|
|
|
|
|
|
<!-- ####################################################### -->
|
|
|
|
|
2001-09-18 07:24:40 +08:00
|
|
|
<hr>
|
2001-10-10 04:18:14 +08:00
|
|
|
<p class="fineprint"><em>
|
2001-10-05 04:03:22 +08:00
|
|
|
See <a href="../17_intro/license.html">license.html</a> for copying conditions.
|
2000-04-22 04:33:34 +08:00
|
|
|
Comments and suggestions are welcome, and may be sent to
|
2001-10-10 04:18:14 +08:00
|
|
|
<a href="mailto:libstdc++@gcc.gnu.org">the libstdc++ mailing list</a>.
|
2001-09-18 07:24:40 +08:00
|
|
|
</em></p>
|
2000-04-22 04:33:34 +08:00
|
|
|
|
|
|
|
|
2001-09-18 07:24:40 +08:00
|
|
|
</body>
|
|
|
|
</html>
|