Thread: numerical precision of a double

  1. #1
    Registered User
    Join Date
    Dec 2007

    numerical precision of a double

    Dear C experts,

    Could anyone tell me what the numerical precision of a double is in C? I read about the smallest and largest numbers I can store in a double, but what I really need to know is how many significant digits after the decimal point are "to be trusted"? Is it a 16-digit mantissa in scientific notation?

    Besides understanding this issue, I have a practical need to know this. I want to use a function to compare doubles. Something like

    #define Abs(x)    ((x) < 0 ? -(x) : (x))
    #define Max(a, b) ((a) > (b) ? (a) : (b))
    double RelativeDifference(double a, double b)
    	double c = Abs(a);
    	double d = Abs(b);
    	d = Max(c, d);
    	return d == 0.0 ? 0.0 : Abs(a - b) / d;
    So, a good condition for two doubles to be the same would be

    if ( RelativeDifference(x,y) < TOLERANCE )
    Thus, what is a good value for TOLERANCE? 1E(-16)?

    Thank you very much,


  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Farncombe, Surrey, England
    What you really want is "epsilon", which is the "smallest difference between two numbers in the current range". If you are working on numbers that are very large, epsilon would be a rather large number, 1E200 for example would have a epsilon of about 1E185 if your numbers are in the order of 1E-200, then 1E-16 is quite enormous, and epsilon would be around 1E-215.

    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Epsilon may be obtained as FLT_EPSILON (for float), DBL_EPSILON (for double), or LDBL_EPSILON (for long double). These macros may be found in the standard C header, <float.h>.

  4. #4
    Registered User
    Join Date
    Dec 2007

    I thought I was sorting out the issue of large vs small numbers by using the relative difference. In other words, when one divides by one of the numbers (assuming the other is of the same order), then the relative difference is of order 0 (i.e. 0.xxxx).

    You point out that something of order 200 (1E200) would have an epsilon of 185 (1E185), while something of order -200 would have an epsilon of -215. Thus, you are looking at an order difference of -15 in both cases. I think this means that in my definition of "relative difference" the tolerance would be 1E-15 (i.e. a fractional part of 15 digits in scientific notation). That makes sense.

    Thanks again for your help!


  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    The epsilons grumpy mentions are the mats' "epsilon" for the number 1; since you're worried about equality, you should be checking whether |(c/d)-1| < dbl_epsilon.

  6. #6
    Registered User
    Join Date
    Dec 2007
    matsp, grumpy, and tabstop,

    Sorry, my second post crossed paths with grumpy's and tabstop's...

    Thanks! Excellent!! You guys gave me exactly what I was looking for.

    Thanks again,


  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    New Zealand
    The D language has the best floating point equality testing routines I have ever seen.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Copying 2-d arrays
    By Holtzy in forum C++ Programming
    Replies: 11
    Last Post: 03-14-2008, 03:44 PM
  2. C++ to C Conversion
    By dicon in forum C Programming
    Replies: 7
    Last Post: 06-11-2007, 08:38 PM
  3. need some help with last part of arrays
    By Lince in forum C Programming
    Replies: 3
    Last Post: 11-18-2006, 09:13 AM
  4. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  5. Unknown Math Issues.
    By Sir Andus in forum C++ Programming
    Replies: 1
    Last Post: 03-06-2006, 06:54 PM