How to calculate exponential of a function

Click For Summary

Homework Help Overview

The discussion revolves around calculating the exponential of a large negative number, specifically focusing on the challenges faced when using the exponential function from the cmath library and alternative approaches such as power series.

Discussion Character

  • Exploratory, Mathematical reasoning, Assumption checking

Approaches and Questions Raised

  • Participants discuss the use of the exp() function and the limitations encountered with large negative inputs. There is an exploration of using power series for approximation and suggestions for alternative methods, including calculating the reciprocal of the exponential function.

Discussion Status

Some participants have offered alternative methods for calculating the exponential of large negative numbers, while others have raised questions about the accuracy and behavior of the existing approaches. The discussion is ongoing, with various interpretations and methods being explored.

Contextual Notes

Participants are considering the implications of rounding errors and the behavior of numerical methods when dealing with very small values. There is also a mention of specific constraints related to the size of the numbers being calculated.

hasan_researc
Messages
166
Reaction score
0


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:
#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 news on Phys.org
It seems like this is just a rounding problem.

Why not try calculating 1/e^x , where x is very small (i.e. -16). Taking n steps in the sum you would obtain

[tex]\frac{1}{e^x} = \frac{1}{x^n} \cdot \left( 1/\left[\frac{1}{x^n} + \frac{1}{x^{n-1}} + ... + 1\right]\right)[/tex]. The idea being that [tex]\frac{1}{x^n}[/tex] is very small for sufficiently large n, and so has little effect on[tex]\left[\frac{1}{x^n} + \frac{1}{x^{n-1}} + ... + 1\right][/tex].
 
Last edited:
For example you want to find
[tex]y=e^{-16}[/tex]
[tex]lg(y)=-16lg(e)=-16*0.4343=-6.949=-7+0.051[/tex]
[tex]y=10^{-7}10^{0.051}=1.125*10^{-7}[/tex]
You may calculate with any accuracy.
 
I understand that very well. Thank you! But I am wondering whether it would be possible to calculate the exponent of -10^5.
 

Similar threads

Replies
1
Views
2K
Replies
1
Views
2K
Replies
4
Views
1K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
2
Views
4K
Replies
8
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 6 ·
Replies
6
Views
12K
  • · Replies 2 ·
Replies
2
Views
2K