Solved

Adding large numbers

Posted on 2004-09-17
4
177 Views
Last Modified: 2010-04-01
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
0
Comment
Question by:jtk207
  • 3
4 Comments
 
LVL 19

Accepted Solution

by:
drichards earned 50 total points
ID: 12089278
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
 
LVL 19

Expert Comment

by:drichards
ID: 12089284
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 Comment

by:jtk207
ID: 12089286
So how would i fix it?
0
 
LVL 19

Expert Comment

by:drichards
ID: 12089394
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

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question