Full precision display of floating point numbers in C++?

后端 未结 3 1421
一个人的身影
一个人的身影 2021-01-19 12:48

I have read several topics about the display of floating point numbers display in C++ and I couldn\'t find a satisfying answer.

My question is: how

3条回答
  •  迷失自我
    2021-01-19 13:49

    Have you looked at std::max_digits10?

    From cppreference:

    The value of std::numeric_limits::max_digits10 is the number of base-10 digits that are necessary to uniquely represent all distinct values of the type T, such as necessary for serialization/deserialization to text. This constant is meaningful for all floating-point types.

    The implication of this (and is how I use it) is that the text output can be copy/pasted into another program and the number will represent the same number.

    Now, I must say that my work-horse format is always right-justified %23.16E, and I use engineering judgement for the last few digits. I like it because is is sufficient for the sign, the exponent, and sixteen digits.

    -----------------------
    -1.1234567812345678E+12
    

    Now, notice that digits of precision and decimal digits of precision are not necessarily the same thing.

提交回复
热议问题