Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

Posted on 2004-09-17
Medium Priority
182 Views
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
0
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
• 3

LVL 19

Accepted Solution

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

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

ID: 12089286
So how would i fix it?
0

LVL 19

Expert Comment

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

Question has a verified solution.

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

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…
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.
###### Suggested Courses
Course of the Month8 days, 17 hours left to enroll