Thread: Limits of long doubles

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    20

    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. #2
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote 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
    How about something like this:

    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.)
    Last edited by Dave Evans; 04-03-2005 at 08:44 PM.

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    82
    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. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote 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
    Last edited by Dave Evans; 04-04-2005 at 06:24 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Having Buffer Problems With Overlapped I/O --
    By Sargera in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 04:46 PM
  2. Problem in Converting Unsigned long to String
    By cprogrammer_18 in forum C Programming
    Replies: 8
    Last Post: 01-14-2006, 08:57 AM
  3. Dev-cpp - compiler options
    By tretton in forum C Programming
    Replies: 7
    Last Post: 01-06-2006, 06:20 PM
  4. Insertion Sort Problem
    By silicon in forum C++ Programming
    Replies: 1
    Last Post: 05-08-2005, 12:30 PM
  5. Sorting Algorithms with Time
    By silicon in forum C++ Programming
    Replies: 3
    Last Post: 05-03-2005, 11:27 AM