Can overflow double to negative values?

Hi i am using g ++ compiler and i am experiencing (what i think is) not enough doubles, is it possible and if so how is the behavior defined

I downloaded the csv format of the covariance matrix (51x51) here: http://pastebin.com/r0fx1qsx

This is the code (boost is required in C ++) I use to calculate the determinant (I have since switched to long doubles and had no effect):

int determinant_sign(const boost::numeric::ublas::permutation_matrix<std ::size_t>& pm)
{
    int pm_sign=1;
    std::size_t size = pm.size();
    for (std::size_t i = 0; i < size; ++i)
        if (i != pm(i))
            pm_sign *= -1.0; // swap_rows would swap a pair of rows here, so we change sign
    return pm_sign;
}

long double determinant( boost::numeric::ublas::matrix<long double>& m ) {
    boost::numeric::ublas::permutation_matrix<std ::size_t> pm(m.size1());
    long double det = 1.0;

    if( boost::numeric::ublas::lu_factorize(m,pm) ) {
        det = 0.0;
    } else {
        for(int i = 0; i < (int)m.size1(); i++)
            det *= m(i,i); // multiply by elements on diagonal
        det = det * determinant_sign( pm );
    }
    return det;
}

      

As a result, I get the data -3.59916e-183

.

When I run the following matlab

code:

M = csvread('path/to/csv');
det(M)
the result I get is:

4.2014e-173

      

As you can see, one is (slightly) positive, whereas one is (slightly) negative

+3


source to share


1 answer


Assuming the floating point block is working correctly, it will not overflow to negative values ​​- the result will be "+ INF" (positive infinity) if the value is out of range. This can only happen with whole whole characters.



It is, of course, entirely possible to have various errors in the calculation that give a negative answer when a positive one is expected.

+4


source







All Articles