3

0.1 in base10 has no correspondance in a double representation.

Is there any guarantees in C++ that a base10 number with no fractional part and with a number of digits minus or equal to std::numeric_limits::digit10 has mandatory a correct/accurate double representation?

Guillaume Paris
  • 10,303
  • 14
  • 70
  • 145
  • 1
    I don't think there's a guarantee, but with IEEE doubles (by far the most common kind) you'll find it in practice. – Mark Ransom Jan 15 '14 at 12:47

2 Answers2

3

Per Dietmar Kühl’s answer, statements in the C++ standard imply that integers in [0, rd) can be represented exactly, where r is std::numeric_limits<type>::radix and d is std::numeric_limits<type>::digits.

This in turn seems to imply that an integer with no more than std::numeric_limits<type>::digits10 base-10 digits can be represented exactly.


Aside: There are some problems with the C++ standard’s definition of std::numeric_limits<type>::digits10.

The standard says this is the “Number of base 10 digits that can be represented without change.” Is that supposed to be just simple base 10 digits, i.e., integers, or is it a statement about precision throughout the range of the format? A footnote, which is not normative, says this is equivalent to FLT_DIG, DBL_DIG, and LDBL_DIG, which are defined by way of the C standard. The C standard gives two definitions in one statement:

number of decimal digits, q, such that any floating-point number with q decimal digits can be rounded into a floating-point number with p radix b digits and back again without change to the q decimal digits,

and:

p log10 b if b is a power of 10
floor((p-1) log10 b) otherwise

I do not believe the former is a good definition. The latter gives us 7 for IEEE-754 32-bit binary floating-point, but 1.5e-45 is a floating-point number with 2 decimal digits, and rounding it to IEEE-754 32-bit binary floating-point and back gives 1.401…e-45 (because it is in the subnormal interval). So it is not true that any floating-point number with 7 decimal digits can be rounded to floating-point and back again without change to the 7 decimal digits.

Community
  • 1
  • 1
Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
0

I believe there is such guarantee, by definition of std::numeric_limits::digit10 (as long as it's implemented correctly, of course). A related discussion (see top answer): What is the meaning of numeric_limits<double>::digits10

Community
  • 1
  • 1
Violet Giraffe
  • 32,368
  • 48
  • 194
  • 335