Standard C++ Library: Numerics
I'm thinking that I might just dedicate a few journal posts each to the various sections of the Standard C++ Library. Now, many people would probably start off covering the input/output libraries, or perhaps even the containers library. I, however, will not start there for two primary reasons; first off, those are really the most boring parts of the Standard C++ Library (hence forth called the standard library). Secondly, there are a few articles oh so slowly working their way through the ERB for publication that deal with the containers library. As such I would rather not write about something that another excellent set of articles should hopefully cover. Hopefully they will get published soon *hint, hint, Dave [grin]*.
The standard library is actually a collection of separate libraries that are only loosely coupled. Their interfaces are designed to be compatible with each other, for the most part, but are suitable abstracted such that two separate libraries need not know anything about each other to properly interact. Each library is geared towards a separate aspect of development. The algorithms library, for instance, is geared towards efficient searching, sorting, and various other operations. It is self contained in that it requires only iterators that meet certain requirements, depending on the particular algorithm, and nothing further.
Each library is divided up into various sections, based upon the functionality they provide. Sometimes that division is just a logical division, as is the case with the algorithms library. In other libraries it is a physical division as well, where each component lives in separate headers, a good example of this would be the containers library. In the case of the numerics library, it is divided into four sections: the Standard C math functions provided by
The Standard C Math Operations and Overloads
The Standard C Library comes with a minimal set of mathematical functionality, which includes the majority of the trigonometric functions, hyperbolic functions, exponentials and powers. It also provides some composite operations that help to simplify certain operations that might commonly be found in a program. These functions are provided by two headers:
The functions seen in the table below are the ones imported from the standard C library. The standard also dictates several overloads that are to be provided, primarily float and long double versions of the majority of the functions, but also a div function that deals with longs. The div function takes in a numerator and a denominator and returns either a div_t or a ldiv_t depending on which overload is called (the int version returns a div_t, while the long version delegates to ldiv and returns a ldiv_t). The div_t structure has two members, int div_t::quot and int div_t::rem, which are the quotient and remainder of the division, respectively. The long version is similar except the members have the long integral type. The div function nicely provides you with a succinct way to obtain both the result of a division, and the modulus in the same operation. Most compilers will interpret the div function, along with most of the other math functions as being intrinsic instructions, replacing them with their assembly counterparts, if applicable.
Complex numbers are numbers of the form a + bi, where a and b are real numbers, and i is the square root of -1. These numbers can also be represented as an ordered pair (a, b). The standard library provides complex number functionality through the
The complex template class has a single template argument which indicates the type that will be used to store the real and imaginary portions of the complex number. You can access the real portion through the use of the T complex
Several utility functions are provided to simplify development with complex numbers, amongst those are: T abs(complex
In the next entry we'll be covering the rest of the numerics library. For those wondering what the answers to the quiz were you'll just have to keep waiting [grin]. Only one person got them all right, and he referenced the standard. As such he should be recognized for his dedication, his name was string.