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
###### Who is Participating?

Commented:
A double has, if I remember correctly, about 17 significant mantissa digits.  This is due to its binary representation.  You have exceeded that length so the extra 000035 is truncated off.  The value 'i' is 3.5 x 10^-21, which has only 2 significant digits in the mantissa, so it is OK.  3.24433466545467 os 15 digits, so only values in the next two places would be displayed (10^-15 and 10^-16).  'i' is beyond the representable range of 'j'.
0

Commented:
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
0

Author Commented:
So how would i fix it?
0

Commented:
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?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.