在 C++ 中设置回默认浮点打印精度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12560291/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set back default floating point print precision in C++
提问by kiriloff
I want to control the precision for a double during a comparison, and then come back to default precision, with C++.
我想在比较期间控制 double 的精度,然后使用 C++ 返回默认精度。
I intend to use setPrecision()
to set precision. What is then syntax, if any, to set precision back to default?
我打算用来setPrecision()
设置精度。如果有的话,将精度设置回默认值的语法是什么?
I am doing something like this
我正在做这样的事情
std::setPrecision(math.log10(m_FTOL));
I do some stuff, and I would like to come back to default double comparison right afterwards.
我做了一些事情,之后我想回到默认的双重比较。
I modified like this, and I still have some errors
我是这样修改的,还是有一些错误
std::streamsize prec = std::ios_base::precision();
std::setprecision(cmath::log10(m_FTOL));
with cmath
false at compilation, and std::ios_base
also false at compilation. Could you help?
cmath
编译时为假,编译时std::ios_base
也为假。你能帮忙吗?
回答by paxdiablo
You can get the precision beforeyou change it, with std::ios_base::precision
and then use that to change it back later.
您可以在更改之前获得精度std::ios_base::precision
,然后使用它稍后将其更改回来。
You can see this in action with:
您可以通过以下方式看到这一点:
#include <ios>
#include <iostream>
#include <iomanip>
int main (void) {
double d = 3.141592653589;
std::streamsize ss = std::cout.precision();
std::cout << "Initial precision = " << ss << '\n';
std::cout << "Value = " << d << '\n';
std::cout.precision (10);
std::cout << "Longer value = " << d << '\n';
std::cout.precision (ss);
std::cout << "Original value = " << d << '\n';
std::cout << "Longer and original value = "
<< std::setprecision(10) << d << ' '
<< std::setprecision(ss) << d << '\n';
std::cout << "Original value = " << d << '\n';
return 0;
}
which outputs:
输出:
Initial precision = 6
Value = 3.14159
Longer value = 3.141592654
Original value = 3.14159
Longer and original value = 3.141592654 3.14159
Original value = 3.14159
The code above shows two ways of setting the precision, first by calling std::cout.precision (N)
and second by using a stream manipulator std::setprecision(N)
.
上面的代码显示了两种设置精度的方法,第一种是调用std::cout.precision (N)
,第二种是使用流操纵器std::setprecision(N)
。
But you need to keep in mind that the precision is for outputtingvalues via streams, it does not directly affect comparisons of the values themselves with code like:
但您需要记住,精度用于通过流输出值,它不会直接影响值本身与以下代码的比较:
if (val1== val2) ...
In other words, even though the outputmay be 3.14159
, the value itself is still the full 3.141592653590
(subject to normal floating point limitations, of course).
换句话说,即使输出可能是3.14159
,值本身仍然是完整的3.141592653590
(当然,受正常浮点限制)。
If you want to do that, you'll need to check if it's close enough rather than equal, with code such as:
如果你想这样做,你需要检查它是否足够接近而不是相等,代码如下:
if ((fabs (val1 - val2) < 0.0001) ...
回答by Alok Save
You need to keep track of your current precison and then reset back to the same once done with your operations with required modified precison. For this you can use std::ios_base::precision:
您需要跟踪您当前的精确度,然后在使用所需的修改过的精确度完成操作后重置为相同的精确度。为此,您可以使用std::ios_base::precision:
streamsize precision ( ) const;
streamsize precision ( streamsize prec );
The first syntax returns the value of the current floating-point precision field for the stream.
The second syntax also sets it to a new value.
第一个语法返回流的当前浮点精度字段的值。
第二种语法还将其设置为一个新值。
回答by premprakash
setprecision() can be used only for output operations and cannot be used for comparisons
setprecision() 只能用于输出操作,不能用于比较
To compare floats say a and b , you have to do it explicitly like this:
要比较浮点数 a 和 b ,您必须明确地这样做:
if( abs(a-b) < 1e-6) {
}
else {
}
回答by David
You can use cout << setprecision(-1)
您可以使用 cout << setprecision(-1)