?
Solved

Adding large numbers

Posted on 2004-09-17
4
Medium Priority
?
181 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 19

Accepted Solution

by:
drichards earned 150 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

770 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