Jump to content
  • Advertisement
  • entries
  • comments
  • views

Standard C++ Library: Numerics Part 1

Sign in to follow this  


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 and along with a few new overloads, complex numbers functionality, numeric arrays for performing mass mathematical operations, and generalized numeric operations.

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: and . The standard library assimilates the C headers along with some new overloads into the headers and

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

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 header. This header provides a template class for representing complex numbers, along with numerous functions to manipulate and represent complex numbers. The functions include overloads of the common trigonometric, hyperbolic, exponential and power functions, along with special functions for accessing the real and imaginary portions, the magnitude, the phase angle, the norm, the complex conjugate, and a function for building a complex number from an angle and a magnitude (polar).

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::real() const method, and the imaginary part through the T complex::imag()const method. Both of these methods also have a functional equivalents T real(complex const&) and T imag(complex const&), both of which are extremely useful when doing operations that use the standard algorithms. The complex template class also implements the standard operators one might expect of a typical number, including multiplication, division, addition, subtraction, negation, and the equality and inequality operators. Since complex numbers do not have the ordering property, that is if we assume it does then we can say: i > 0 then -1 = i * i > 0, which is false, thus we must assume i < 0. But if we multiply through by -1, flipping the inequality we get -i<0. Then -1 = (-i)(-i)>0, which is clearly false. Since we've reached a contradiction both ways, the assumption that complex numbers have an ordering property is false. Also provided are operators to read from an basic_istream, which can accept input of the form u, or (u), or (u, v) and the ability to write to an basic_ostream, which will produce an output of the form (u,v).

Several utility functions are provided to simplify development with complex numbers, amongst those are: T abs(complex const&) which returns the square root of the norm of the complex number, T norm(complex const&) which returns a^2 + b^2 (think 2-vector inner product of a vector with its self), T arg(complex const&) which returns the phase angle of the complex number which is the same as atan2(imag(v), real(v)) where v is an instance of the complex type, complex conj(complex const&) which returns a + bi if the input number is of the form a - bi or returns a - bi if the input number is of the form a + bi, finally there is complex polar(const T& mag, const T& theta = 0) which returns a complex number of the form (mag * cos theta, mag * sin theta).

Finishing Up

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.

Sign in to follow this  


Recommended Comments

Interesting read - more please!

I'd forgotten about Howards articles, they were pretty damn good. Wondered why I hadn't seen them on the front-page yet...


Share this comment

Link to comment
I second the view of the D3D monkey up there, this is one of the parts of the standard lib I keep forgetting exists... [grin]

Share this comment

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!