设置默认精度C ++ [英] set back default precision C++

查看:283
本文介绍了设置默认精度C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在比较期间控制双精度,然后使用C ++恢复默认精度。



我打算使用 setPrecision()设置精度。



我正在做这样的事情

  std :: setPrecision(math.log10(m_FTOL)); 

我做一些事情,我想回来默认双重比较。 p>

我这样修改,仍然有一些错误

  std: :streamsize prec = std :: ios_base :: precision(); 
std :: setprecision(cmath :: log10(m_FTOL)); cmath
在编译时为



std :: ios_base 在编译时也为false。您能帮忙吗?



感谢。

解决方案

在之前修改 ,然后使用 std :: ios_base :: precision
$ b

您可以在操作中看到此操作:

  #include< ios> 
#include< iostream>
#include< iomanip>

int main(void){
double d = 3.141592653589;
std :: streamsize ss = std :: cout.precision();
std :: cout<< Initial precision =<< ss << '\\\
';

std :: cout<< Value =<< d<< '\\\
';

std :: cout.precision(10);
std :: cout<< Longer value =< d<< '\\\
';

std :: cout.precision(ss);
std :: cout<< Original value =<< d<< '\\\
';

std :: cout<< Longer and original value =
<< std :: setprecision(10)<< d<< ''
<< std :: setprecision(ss)<< d<< '\\\
';

std :: cout<< Original value =<< d<< '\\\
';

return 0;
}

其输出:



< pre class =lang-none prettyprint-override> 初始精度= 6
值= 3.14159
更长值= 3.141592654
原始值= 3.14159
和原始值= 3.141592654 3.14159
原始值= 3.14159

上面的代码显示了两种方法设置精度,首先通过调用 std :: cout.precision(N),然后使用流操纵符 std :: setprecision(N)






但是你需要记住的精度是 / em>值,它不会直接影响值本身与以下代码的比较:

  if(val1 == val2)... 

换句话说,即使 3.14159 ,该值本身仍然是完整的 3.141592653589 (当然,受到正常的浮点数限制)

  if((fabs(val1-val2) 0.0001)... 


I want to control the precision for a double during a comparison, and then come back to default precision, with C++.

I intend to use setPrecision() to set precision. What is then syntax, if any, to set precision back to default?

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?

Thanks.

解决方案

You can get the precision before you change it, with std::ios_base::precision and then use that to change it back later.

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


But you need to keep in mind that the precision is for outputting values via streams, it does not directly affect comparisons of the values themselves with code like:

if (val1== val2) ...

In other words, even though the output may be 3.14159, the value itself is still the full 3.141592653589 (subject to normal floating point limitations, of course).

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

这篇关于设置默认精度C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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