Does the C++ standard specify anything on the representation of floating point numbers?

前端 未结 4 1356
梦毁少年i
梦毁少年i 2020-12-09 01:43

For types T for which std::is_floating_point::value is true, does the C++ standard specify anything on the way that T

4条回答
  •  星月不相逢
    2020-12-09 02:00

    No particular implementation is required. The C++ standard doesn't talk about it much at all. The C standard goes into quite a bit of detail about the conceptual model assumed for floating point numbers, with a sign, exponent, significand in some base b, and so on. It, however, specifically states that this is purely descriptive, not a requirement on the implementation (C11, footnote 21):

    The floating-point model is intended to clarify the description of each floating-point characteristic and does not require the floating-point arithmetic of the implementation to be identical.

    That said, although the details can vary, at least offhand it seems to me that producing (for example) a conforming implementation of double that didn't fit fairly closely with the usual model (i.e., a significand and exponent) would be difficult (or at least difficult to do with competitive performance, anyway). It wouldn't be particularly difficult to have it vary in other ways though, such as rearranging the order, or using a different base.

    The definition of std::numeric_limits::digits (and std::numeric_limits::digits10) imply fairly directly that what's listed as a floating point type must retain (at least approximately) the same precision for all numbers across a fairly wide range of magnitudes. By far the most obvious way to accomplish that is to have some number of bits/digits devoted to a significand, and some other (separate) set of bits devoted to an exponent.

提交回复
热议问题