Click here to Skip to main content
15,887,822 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
In 'float.h' from MS VS Community 2013 Update 5:
Quote:
#define LDBL_DIG DBL_DIG /* # of decimal digits of precision */
Quote:
#define DBL_DIG 15 /* # of decimal digits of precision */
I tested it using sprintf, it shows 17 meaningful digits.

It also shows 17 digits ('0.14285714285714285') in the editor ('autos' window), when i running test code in the debug mode.

I also opened 'float.h' in the MS VS 2015 Community Update 2 RC, it still same,
but now it has also a new constant:
Quote:
#define DBL_DECIMAL_DIG 17 // # of decimal digits of rounding precision


What I have tried:

Tets code:

const auto oneSeventh = 1.0L / 7.0L;
sprintf(buf, "%.*Lf", 30U, oneSeventh); // 0.142857142857142850000000000000
Posted
Updated 17-Mar-16 6:16am
Comments
Richard MacCutchan 17-Mar-16 11:35am    
Very interesting, do you have a question?
Shvetsov Evgeniy 17-Mar-16 12:12pm    
What the point of your meaningless comment and your negative vote?
The question is in the very first line and it is "MS VS 2013 'LDBL_DIG' const. From 'float.h' is incorrect?"

1 solution

A double floating point number has 53 mantissa bits (DBL_MANT_DIG) where the MSB is implicit set and 52 bits are stored. The corresponding number of decimal digits is
log10(2^53) = 53 * log10(2) = 15.95

Rounding that value down is used as DBL_DIG. So there is nearly one more decimal digit of precision.

DBL_DECIMAL_DIG specifies the number of decimal digits that can be rounded to a double precision number and back again without change to the value. It must be one more than the uprounded precision of 16 to satisfy this requirement. See also the standard at <float.h>[^].

So printing a value with 17 significant decimal digits and converting that back to double precision will result in the same value. When printing only 16 digits, the converted value might be not identical.

Microsoft has no native support for long double precision:
The representation of long double and double is identical. However, long double and double are separate types.
Therefore the LDBL_xxx definitions are using the corresponding DBL_xxx definitions.
 
Share this answer
 
Comments
Shvetsov Evgeniy 17-Mar-16 12:54pm    
Yes, i know that MS VS didn't support long double, but i am developing a portable code, so it doesn't matters. Any way, your answer is very clear, informative and usefull, thanks a lot, you completely solve my problem.
Jochen Arndt 17-Mar-16 13:21pm    
Thank you for you feedback and accepting the solution.

I guessed that you know that about long double but your question was about LDBL_DIG. So I had to mention that after describing double.

Regarding VS 2013:
MS added more C99 features with each new version but is probably not adding them with updates. However, this is just a definition that can be simply added when required:
#ifndef DBL_DECIMAL_DIG
#define DBL_DECIMAL_DIG 17
#endif
Shvetsov Evgeniy 17-Mar-16 12:58pm    
But still, MS VS 2013, even fully updated (Community Update 5) miss that 'DBL_DECIMAL_DIG' constant, which isn't good. I guess that modern GCC or Intel compiler has no such problem.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900