Subtracting small numbers

Posted on 2006-05-19
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

Question by:Zeman4323
    LVL 86

    Expert Comment

    >>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;
    LVL 86

    Expert Comment

    LVL 14

    Accepted Solution

    > 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

    LVL 22

    Expert Comment

    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?


    Author Comment

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


    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Join & Write a Comment

    Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
      Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
    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.

    729 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

    19 Experts available now in Live!

    Get 1:1 Help Now