简体   繁体   中英

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

#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

The result while in release mode is 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. The problem is Interval(-2, -2) . It remains the magic of VisualStudio to produce 0, 0. :). nan is the most appropriate answer to sqrt(-x) . you may sqrt of std::complex<T> .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM