Solved

Adding large numbers

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

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

Suggested Solutions

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

734 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