Solved

Adding large numbers

Posted on 2004-09-17
4
175 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

760 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now