You are here:

C++/Complex exponents in C++


I am currently writing a program that calculates velocity of a rocket. one of the equations calls for "e to the power of -xt", or e^-xt. I found a solution for exponents in C++ using the pow(x,y) function in math.c.

My question is can this function be used to solve e^-x*t? x and t are both floating point decimals, and my main concern comes from putting -x*t into the y spot in pow(x,y).

Thank you!

Well other than the fact that if x*t were negative then -x*t would be positive, I presume you mean can you use negative powers with pow?

Yes. There is nothing to stop you in the specification of pow, as pow works with floating point values. The only things to watch out for of course is if you end up with very small or very large values that exceed the range of the floating point type you are using (a range error).

Two other situations may cause errors:

For pow(x, y) (that is for x to the y)

   - if x is negative and finite (i.e. not negative infinity)
     and y is finite and not an integer a domain error occurs.
   - if x is zero and y is less than or equal to zero a domain error may occur.
Note that errors here would be handled in the manner of the C library as C++ shares its maths function pretty much with those of the C90 (with the C95 amendment) library.

The C99 standard (as the original C standard documents the C++ standard refers to seem to be no longer easily available - doh!) states:

      "On a domain error, the function returns an implementation-defined value;
       if the integer expression math_errhandling & MATH_ERRNO is nonzero, the
       integer  expression errno acquires the value EDOM;
       if the integer expression math_errhandling & MATH_ERREXCEPT is nonzero, the
       ‘invalid’ floating-point exception is raised.
       Similarly, a range error occurs if the mathematical result of the function cannot
       be represented in an object of the specified type, due to extreme magnitude."

With additional text in the following two paragraphs specifying the behaviour of overflow and underflow. However I should check your specific C/C++ library implementation's documentation to check exactly how such things are handled.

Consider that you could have easily found this out for yourself just by trying using pow with values of interest, for example:

   #include <cmath>
   #include <iostream>

   int main()
       std::cout << std::pow(3.0, -2.0) << std::endl;

Which should output 1/(3*3), or 1/9 or 0.111111111..., which indeed it does (or did when I tried it with Visual C++ 2008). [Remember that x to the -y is equivalent to 1 / (x to the y)].

The C++ and C standard documents themselves are published in book form by Wiley or are available for download in PDF form from the ANSI web site for $30 US (price the last time I checked):

"Programming Languages - C++" and "Programming Languages - C"

the books are just titled:

"The C++ Standard" and "The C Standard"

These are _not_ easy reads, but are the word on how a C++ or C implementation should behave (note I say should behave, not do behave!), including such details as the language standard library. Note I find that those sections of the C++ standard library C++ 'inherits' from C is not fully documented in the C++ standard document - hence the requirement for having a copy of the C standard, for example for the numeric function support section of the C++ standard just states:

       "1 Tables 80 and 81 describe headers <cmath> and <cstdlib>
         (abs(), div(), rand(), srand()), respectively

        2 The contents of these headers are the same as the
         Standard C library headers <math.h> and <stdlib.h>
         respectively, with the following additions:"

And then just lists the additional overloads for certain functions. In the case of pow C++ adds overloads for the following variations:

       float pow (float, float);
       float pow (float, int);
       double pow(double, int);
       long double pow (long double, long double);
       long double pow (long double, int);

Hope this helps.  


All Answers

Answers by Expert:

Ask Experts


Ralph McArdell


I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.


My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at


©2016 All rights reserved.