?
Solved

Subtracting small numbers

Posted on 2006-05-19
5
Medium Priority
?
180 Views
Last Modified: 2010-04-01
I think this is really simple and trivial.  

I have a program with 10.0 as a double value.

I want to subtract 4e-12 from 10.

I am not sure how to do this.  When I convert this number to decimal form and try subtracting it it also doesn't work.  The number always remains 10.

I am guessing that I am loosing precision or something to this effect.

If anyone knows how to do this that would be extremely helpful.


Thanks a lot

~Matthew
0
Comment
Question by:Zeman4323
5 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 16719961
>>I want to subtract 4e-12 from 10.

That's *way* below the numeric resolution your PC can handle, the minimum is described as 'numeric_limits::epsilon
()', e.g.

#include <iostream>
#include <limits>
using namespace std;

    cout << "The epsilon for double is          " <<
        numeric_limits<double>::epsilon() << endl;
 
0
 
LVL 86

Expert Comment

by:jkr
ID: 16719977
0
 
LVL 14

Accepted Solution

by:
wayside earned 400 total points
ID: 16720180
> That's *way* below the numeric resolution your PC can handle, the minimum is described as 'numeric_limits::epsilon

On my pc, numeric_limits<double>::epsilon() is 2.22045e-16, which is much smaller than 4e-12.

My guess is you are not printing with enough precision:

#include <iostream>
#include <limits>
#include <iomanip>

using namespace std;

int main()
{
    double d = 10.0;
    double e = 4e-12;
    cout << "The epsilon for double is " << setprecision(17) <<
        numeric_limits<double>::epsilon() << endl;
         
    cout << "10.0 - 4e-12 is " << setprecision(17) << d - e << endl;

    return 0;
}

> ./prog
The epsilon for double is 2.2204460492503131e-016
10.0 - 4e-12 is 9.9999999999959996

0
 
LVL 22

Expert Comment

by:grg99
ID: 16724493
Also think about whether this is of any use.  There are very few places where numbers are known to be accurate to 4 parts  in 10 million million.  Perhaps you don't need all that resolution?

0
 

Author Comment

by:Zeman4323
ID: 16727481
well i needed it for a school assignment (doesn't really need to be precise)  But setprecision(17) works perfect.

Thanks
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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 …
Article by: evilrix
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 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 goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

862 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