【问题标题】:Why does boost numeric library give different results by using debug or release mode?为什么 boost 数值库使用调试或发布模式会给出不同的结果?
【发布时间】:2017-09-14 14:40:31
【问题描述】:

我遇到过这样的现象,当我使用调试模式或发布模式时,我的代码给了我不同的结果。我已将问题简化为下面的代码。我正在使用 Microsoft Visual Studio Professional 2013 和 libeary boost 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;
}

在调试模式下的结果是 1.#QNAN 1.#QNAN

在释放模式下的结果是 0 0

我想知道导致此问题的原因以及如何解决此问题。因为如果我不能依赖结果,这会给我的项目带来严重的问题。

【问题讨论】:

    标签: c++ boost intervals boost-numeric


    【解决方案1】:

    sqrt 的负数是一个艰难的命题。问题是Interval(-2, -2)。产生 0、0 仍然是 VisualStudio 的魔力。:)。 nan 是对sqrt(-x) 最合适的答案。你可以sqrtstd::complex&lt;T&gt;

    【讨论】:

    • std::complex&lt;T&gt;的问题是我需要使用boost库的interval。根据文档,这是不可能的。以下是引用:Firstly, due to the definition of an interval, the base numbers have to be totally ordered so, for instance, complex&lt;T&gt; is not usable as base number type for intervals. (...) Given all this, one may want to limit the template argument T of the class template interval to the floating point types float, double, and long double, as defined by the IEEE-754 Standard.
    猜你喜欢
    • 2011-09-19
    • 2012-06-09
    • 2017-02-16
    • 2014-02-01
    • 2021-08-26
    • 2011-01-05
    • 2021-10-01
    • 1970-01-01
    • 2015-11-16
    相关资源
    最近更新 更多