[英]Why does boost numeric library give different results by using debug or release mode?
I've encountered the phenomena that my code gives me different results when i use debug mode or release mode. 当我使用调试模式或释放模式时,我遇到了我的代码给我不同结果的现象。 I've stripped the problem down to the code below. 我把问题解决了下面的代码。 I am using Microsoft Visual Studio Professional 2013 and the libeary boost 1.62 我正在使用Microsoft Visual Studio Professional 2013和自由增强1.62
#include "stdafx.h"
#include <iostream>
#include <math.h>
#include <boost/numeric/interval.hpp>
#include <boost/numeric/interval/rounded_arith.hpp>
using namespace std;
using namespace boost::numeric::interval_lib;
using namespace boost::numeric;
typedef interval<double, policies<save_state<rounded_transc_std<double> >,
checking_base<double> > > Interval;
int _tmain(int argc, _TCHAR* argv[])
{
Interval result = (Interval(3.15, 4.6) - Interval(-0.6, 2.1))*sqrt(Interval(2, 2) + Interval(-2, -2)*Interval(10.022631612535406, 10.031726559552226));
cout << "result: " << result.lower() << " " << result.upper();
return 0;
}
The result while in debug mode is 1.#QNAN 1.#QNAN 在调试模式下的结果是1.#QNAN 1.#QNAN
The result while in release mode is 0 0 在释放模式下的结果是0 0
I would like to know what causes this problem and how to fix this. 我想知道导致此问题的原因以及如何解决此问题。 Since this causes serious problems in my project if I cannot rely on the results. 如果我不能依赖结果,这会导致我的项目出现严重问题。
sqrt
of a negative number is a tough proposition. 负数的sqrt
是一个艰难的命题。 The problem is Interval(-2, -2)
. 问题是Interval(-2, -2)
。 It remains the magic of VisualStudio to produce 0, 0. :). VisualStudio的神奇之处还在于产生0,0。:)。 nan
is the most appropriate answer to sqrt(-x)
. nan
是sqrt(-x)
最合适的答案。 you may sqrt
of std::complex<T>
. 你可以sqrt
std::complex<T>
sqrt
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.