# Thread: Limits of long doubles

1. ## Limits of long doubles

Hi,

I wrote this little program to help with figure out convergence/divergance of series... However, for not very large values of a, the program gives the answer (run-time error?) -1.#IND

I'm assuming this is because I'm bumping into a limitation of long doubles... Does anyone know of a way I could modify this program so that it can handle larger values of a?

Thanks!

-Xanth

Code:
```#include <iostream>
#include <cmath>
#include <iomanip>
#define E 2.71828182846

using namespace std;

int main()
{
int a;
long double ans=0;
long double temp;
while (a != -2)
{
cout << "Enter A: ";
cin >> a;
for (a;a>0;a--)
{
temp = (pow(E, a) - 1)/(pow(E, (2*a)) - 1);
ans += temp;
}
cout << endl << "Ans is " << setprecision(15) << ans << endl;
ans=0;
}
return 0;
}```

2. Originally Posted by Xanth
Hi,

I wrote this little program to help with figure out convergence/divergance of series... However, for not very large values of a, the program gives the answer (run-time error?) -1.#IND

I'm assuming this is because I'm bumping into a limitation of long doubles... Does anyone know of a way I could modify this program so that it can handle larger values of a?

Thanks!

-Xanth

your term is equal to exp(a-1)/exp(2*a-1)

But isn't this true: exp(a-1)/exp(2a-1) = 1/(exp(a) + 1)

because (exp(a) - 1) * (exp(a) + 1) = exp(2a)- 1

So, this could be your term:
Code:
`term = 1.0/(exp(a) + 1.0);`
To prevent overflow, you could divide numerator and denominator by exp(a) so that

Code:
`term = exp(-a)/(1.0 + exp(-a));`
Now, this expression won't overflow for any positive value of a.

It will underflow (that is, the value of exp(-a) will be smaller than the smallest absolute value long double precision number), so that the terms beyond some value of a will be computationally zero, but at least it won't overflow.

Regards,

Dave

(By the way, if you are really doing long double arithmetic, it would make sense use a value of E that is as accurate as the number of significant digits that the computation supports. Your value only has 12 significant digits. If you are really into numerical computation, I think you should consider keeping as much precision as you can.)

3. marginally related question, but would there be a real value in using a global variable for E instead of a macro? I remember hearing something to that effect, but I couldn't follow the justification at the time.

4. Originally Posted by AH_Tze
marginally related question, but would there be a real value in using a global variable for E instead of a macro? I remember hearing something to that effect, but I couldn't follow the justification at the time.
For simple cases like defining a manifest constant I can't see any particular advantages of one over the other. In general I avoid macros whenever possible (that is, almost always), in favor of global const values. (But that's just me.) At the very least, use of const expressions instead of macros gives the compiler a way to do type checking, and sometimes that helps.

Regards,

Dave