Portable printing of exponent of a double to C++ iostreams

后端 未结 3 1614
鱼传尺愫
鱼传尺愫 2021-02-05 12:28

I want to print a double value to std::cout portably (GCC, clang, MSVC++) such that the output is the same on all platforms.

I have a problem with the forma

3条回答
  •  走了就别回头了
    2021-02-05 13:24

    There is no manipulator controlling the formatting of the exponent (I assume you mean the exponent rather than the mantissa; also, the "official" name used for the mantissa is significant). To make matters worse, I can't see any rule in the C standard which restricts the formatting of the exponent. I realize that this is about C++ but for the purpose of the formatting details, the C++ standard refers to the C standard.

    The only approach I'm aware of is to use an own std::num_put facet which formats the values as desired. This facet would then be put into a std::locale which in turn is imbue()ed into std::cout. A potential implementation could use the default std::num_put facet (or snprintf() which is, unfortunately, probably simpler) to format the floating point number and then strip leading zeros from the exponent.

提交回复
热议问题