Is `double` guaranteed by C++03 to represent small integers exactly?

前端 未结 2 1947
别那么骄傲
别那么骄傲 2020-12-20 15:26

Does the C++03 standard guarantee that sufficiently small non-zero integers are represented exactly in double? If not, what about C++11? Note, I am not assuming

相关标签:
2条回答
  • 2020-12-20 15:55

    Well, 3.9.1 [basic.fundamental] paragraph 8 states

    ... The value representation of floating-point types is implementation-defined. ...

    At least, the implementation has to define what representation it uses.

    On the other hand, std::numeric_limits<F> defines a couple of members which seem to imply that the representation is some in the form of significand radix exponent:

    • std::numeric_limits<F>::radix: the radix of the exponent
    • std::numeric_limtis<F>::digits: the number of radix digits

    I think these statements imply that you can represent integers in the range of 0 ... radix digits - 1 exactly.

    0 讨论(0)
  • 2020-12-20 16:02

    From the C standard, "Characteristics of floating types <float.h>", which is normative for C++, I would assume that you can combine FLT_RADIX and FLT_MANT_DIG into useful information: The number of digits in the mantissa and the base in which they are expressed.

    For example, for a single-precision IEEE754 float, this would be respectively 2 and 24, so you should be able to store integers of absolute value up to 224.

    0 讨论(0)
提交回复
热议问题