How to calculate exponential of a function

In summary, the code is rounding errors. For a number as small as 10^-5, the answer is a large positive number (which is nonsenical).
  • #1
hasan_researc
170
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
  • #2
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:
  • #3
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.
 
  • #4
I understand that very well. Thank you! But I am wondering whether it would be possible to calculate the exponent of -10^5.
 
  • #5
// I used this percentage error to avoid infinite loop
cout << " the number of iterations is " << iterations << endl;
cout << " The answer is " << fsum << endl;
return 0;
}


I would suggest using the built-in function exp() from the cmath library to calculate the exponential of a function. It is a reliable and efficient method for calculating exponential values. However, it is important to note that the exp() function may return an answer of 0 for very large negative numbers, as shown in your code. This is due to the limitations of floating-point arithmetic and the fact that the exponential function grows exponentially.

To improve the accuracy of your code, you can try using a higher precision data type, such as a double or long double, to store the values. Additionally, you can try increasing the number of iterations in your while loop to improve the accuracy of the calculation.

Another option to calculate the exponential of a large negative number is to use the logarithmic identity, exp(x) = 1/exp(-x). This can be useful for avoiding the issue of getting an answer of 0 for large negative numbers. However, it is important to keep in mind that this approach may still have limitations due to floating-point arithmetic.

In conclusion, while your code may work for smaller negative numbers, it may not be accurate for larger negative numbers due to the limitations of floating-point arithmetic. I would recommend using the built-in exp() function or exploring other methods, such as the logarithmic identity, for calculating the exponential of a large negative number.
 

FAQ: How to calculate exponential of a function

How do you calculate the exponential of a function?

To calculate the exponential of a function, you need to raise the base of the exponential (usually denoted as "e") to the power of the function. This can be written as e^(function).

Can any function be written as an exponential function?

No, not every function can be written as an exponential function. Only functions where the independent variable appears as an exponent can be written as an exponential function.

How do you simplify an exponential function?

To simplify an exponential function, you can use the rules of exponents. For example, if you have e^(x+y), you can simplify it to e^x * e^y. Additionally, if the base of the exponential is raised to a power, you can use the power rule to simplify it.

What is the relationship between logarithms and exponential functions?

The relationship between logarithms and exponential functions is that they are inverse operations of each other. This means that if you take the logarithm of an exponential function, you will get the power that the base was raised to. Similarly, if you take the exponential of a logarithmic function, you will get the base raised to the power of the function.

How can you use exponential functions in real-life applications?

Exponential functions can be used to model growth and decay in various real-life situations, such as population growth, radioactive decay, and compound interest. They can also be used in statistics and economics to analyze data and make predictions.

Back
Top