Setting the precision of a double without using stream (ios_base::precision)

后端 未结 3 1934
半阙折子戏
半阙折子戏 2020-12-19 09:40

Is there a way to do this without using the stream? For example, something like this:

double a = 6.352356663353535;
double b = a.precision(5);
3条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-19 10:39

    doubles are almost universally implemented as IEEE floating point numbers. Their precision depends on the number size only (in the case of double, which is short for “double-precision floating point number”, it’s 53 bits). There is no way of manually setting the precision of a floating point number.

    The display precision is always a property of the output formatting, never of the number. Don’t try to change the number via rounding, the operation makes no sense. You don’t need to reduce a number’s precision, except for display purposes.

提交回复
热议问题