设置double的精度,不使用stream(ios_base :: precision) [英] Setting the precision of a double without using stream (ios_base::precision)

查看:162
本文介绍了设置double的精度,不使用stream(ios_base :: precision)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法不使用流?例如,像这样:

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);

而不是:

double a = 6.352356663353535;
std::cout.precision(5);
std::cout << a << std::endl;

我是C ++的新手,我很好奇。先谢谢。

I am new to C++ and I am curious. Thanks, in advance.

推荐答案

double IEEE浮点数。它们的精度取决于数量大小(在 double 的情况下,这是双精度浮点数,它是53位)。没有手动设置浮点数精度的方法。

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.

这篇关于设置double的精度,不使用stream(ios_base :: precision)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆