Lots of positive exponentials leading to huge exponents

Hello there,

This is my first post on this forum so bear with me if I don't know the rules or I am posting in the wrong area.

I am coding up an of equation for a scattering rate which involve quite a few positive (and a couple of negative) exponentials which often have terms that go into hundreds leading to results that have huge exponents. I originally wrote up this scattering rate in Mathematica and it didn't complain, but when I looked in detail at the equation some terms were reaching exponents of over a 1000!

I am writing the scattering rate in Fortran 95 for a simulator and this is where my problems occur. In Fortan95 I can use double precision numbers which have a max. exponent range of 10^+/-308 and I am overflowing. I am currently using the atomic unit system which should help as it normalizes a lot of the units to 1. The equation itself should produce a value between 0 and 1.

I have tried splitting up the equation into components, then taking the natural logarithm to try and simplify the problem but this has not helped. I started to think about trying to scale the numbers I am using in a manner similar to changing unit system, but I don't have any notes/references on where to start.

Any help/ideas on this would be a great help, thanks.

Ben Niehoff
Gold Member
What does the equation look like?

Have not had much experience with Latex typesetting but here goes:

$$\begin{multline*} f_{dbl}(\Theta)= \frac{ \exp^{-2(L_c+Z_i)A_Q(\Theta)} } { (\exp^{ 2L_c A_Q(\Theta) } - 1)^2 } \\ \{ E_C(\Theta)^2 + E_D(\Theta)^2 +E_S(\Theta)^2 \\ + 2 E_C(\Theta) ( E_D(\Theta) \cos{ (Q_Z(\Theta) L_c) } - E_S(\Theta) \cos{ (Q_Z(\Theta) Z_i) } ) \\ - 2 E_D(\Theta) E_S(\Theta) \cos{ ( Q_Z(\Theta) (L_c - Z_i) ) } \} \end{multline*}$$

where
$$E_C(\Theta)= \exp^{(L_c + 2Z_i) A_Q(\Theta)} ( \exp^{2(L_c - Z_i) A_Q(\Theta)} - 1)$$
$$E_D(\Theta)= \exp^{2 L_c A_Q(\Theta)} ( \exp^{2 Z_i A_Q(\Theta)} - 1)$$
$$E_S(\Theta)= \exp^{(L_c + Z_i) A_Q(\Theta)} ( \exp^{2 L_c A_Q(\Theta)} - 1)$$
$$A_Q(\Theta)= \sqrt{k^2 \sin^2(\theta) + k_c^2}$$
$$Q_Z(\Theta)= k(1-\cos(\Theta))$$

- The function $f_{dbl}$ should return a value between 0 and 1.
- $Z_i$ and $L_c$ are in metres and roughly within deca-nanometre dimension.
- $k , k_c$ are in inverse metre dimensions and can vary from $10^6$ to $10^9$

I am guessing that whoever asked me to give this equation may be regretting it now! Still I have a had a thorough crash course in latex! I seem to be having a problem with getting the linebreaks to work, but I guess you can copy the code from the pop-up window!

edit:: I have tried to fix the latex src for the main equation, but I can't seem to find the error. Worked OK in the preview message window, so I am at a loss.

Last edited:
Ha harr, got it to work after all! :)

Ben Niehoff
Gold Member
What's a deca-nanometer? Do you mean they vary in range from decameters to nanometers (i.e., 10^1 down to 10^-9)? Or did you mean decimeters (10^-1 to 10^-9)? Or did you mean tens of nanometers (10^-8 to 10^-9)?

One thing that might help is this: For very large x,

$$(e^x - 1) \approx e^x$$

So, first find some x that that puts this approximation within the acceptable bounds of error. For example,

$$\frac{e^{10} - (e^{10} - 1)}{e^{10} - 1} = 0.0000454$$

So that's already quite accurate. If you chose x = 100, the error is less than 10^-43. It seems some cutoff between 10 and 100 is good.

Once you choose a cutoff, do this: For all values of x less than the cutoff, calculate the formula as written. For x greater, simplify the formula by substituting e^x for (e^x - 1). Then you can simplify the exponents in the numerator and denominator to get them within an acceptable range, before actually calculating exp(*).

That ought to do it.

Yeah, I am not surprised you haven't heard of the 'deca-nanometre' term, it is one of these fancy buzz words used to make a particular dimension sound more interesting than it actually is! It is used to denote dimensions of tens of nanometres, so the 10^-8 to 10^-9 range.

Thanks for the suggestion of approximating the (e^x - 1) terms, that might really do the trick. I will give it a try and get back to you.

CRGreathouse
Homework Helper
So, first find some x that that puts this approximation within the acceptable bounds of error. For example,

$$\frac{e^{10} - (e^{10} - 1)}{e^{10} - 1} = 0.0000454$$

So that's already quite accurate. If you chose x = 100, the error is less than 10^-43. It seems some cutoff between 10 and 100 is good.
If you're working in IEEE double-precision (as evidenced by the "10^+/-308" in the original post), e^x - 1 = e^x for x greater than roughly $53\ln2\approx36.7.$

Thanks for the help, that has solved the problem. I am now able to calculate the function without it overflowing and under-flowing all over the place!