(adsbygoogle = window.adsbygoogle || []).push({});

I have to calculate the exponetial of a large (on the order of 10^5) negative number. I tried using exp(), the exponential function from the cmath library, but I get an answer of 0. So, I tried to use the power series for the exponential function. For numbers from -1 to -14, I get answers which are accurate (within the percentage error set in the while statement). But for any number above -14, the answer diverges from the true value. For a number as small as 10^-5, the answer is a large positive number (which is nonsenical).

Please help me understand what's wrong with the code, if anything, and how it can be improved. (Or is there another better way to calculate the exp of a large -ve number?)

Code (Text):

#include <iostream>

#include <cmath>

using namespace std;

int main()

{

double t = - 16; // t is the argument of the exponential function.

double fsum = 1.0; // fsum is the cumulative sum in the calc of the answer.

double fbefore = fsum; // to be used for the while statement

int n = 1; // n is the denominator of the power series

double comparing; // to be used in the while statement

int iterations = 0;

double term = 1; // each additional term in the series.

do

{

iterations = iterations + 1;

cout << iterations << endl;

term = term * ( t/n );

fsum = fsum + term;

n = n + 1;

double fafter = fsum;

comparing = (fbefore - fafter)/fbefore;

fbefore = fafter;

cout << fsum << endl;

}

while ( abs(comparing) > 0.0000000001);

**Physics Forums | Science Articles, Homework Help, Discussion**

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

# Homework Help: How to calculate exponential of a function

**Physics Forums | Science Articles, Homework Help, Discussion**