Link to home
Start Free TrialLog in
Avatar of jtk207
jtk207

asked on

Adding large numbers

I have the following c++ program:

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    double long i=0.0000000000000000000035;
    double long j=3.24433466545467;
   
    j+=i;
   
    cout<<setprecision(100)<<j<<endl;

    return 0;
}


why does it print: 3.24433466545467
instead of: 3.2443346654546700000035
ASKER CERTIFIED SOLUTION
Avatar of drichards
drichards

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of drichards
drichards

Here's a link to how floats and doubles are represented in binary form:

    http://www.fact-index.com/i/ie/ieee_floating_point_standard.html#Double%20Precision%2064%20bit
Avatar of jtk207

ASKER

So how would i fix it?
Directly, you can't.  You'd have to get or write an algorithm to deal with arbitrarily large/small numbers.  I've seen these for integers, but I haven't seen one for reals.  Is that amount of precision really necessary for what you're doing?