mirror of
https://gitlab.com/libeigen/eigen.git
synced 2025-04-20 08:39:37 +08:00
Replace norm() with squaredNorm() to address integer overflows
For random matrices with integer coefficients, many of the tests here lead to integer overflows. When taking the norm() of a row/column, the squaredNorm() often overflows to a negative value, leading to domain errors when taking the sqrt(). This leads to a crash on some systems. By replacing the norm() call by a squaredNorm(), the values still overflow, but at least there is no domain error. Addresses https://gitlab.com/libeigen/eigen/-/issues/1856
This commit is contained in:
parent
9dda5eb7d2
commit
8e875719b3
@ -431,22 +431,22 @@ void test_stl_iterators(int rows=Rows, int cols=Cols)
|
|||||||
{
|
{
|
||||||
RowVectorType row = RowVectorType::Random(cols);
|
RowVectorType row = RowVectorType::Random(cols);
|
||||||
A.rowwise() = row;
|
A.rowwise() = row;
|
||||||
VERIFY( std::all_of(A.rowwise().begin(), A.rowwise().end(), [&row](typename ColMatrixType::RowXpr x) { return internal::isApprox(x.norm(),row.norm()); }) );
|
VERIFY( std::all_of(A.rowwise().begin(), A.rowwise().end(), [&row](typename ColMatrixType::RowXpr x) { return internal::isApprox(x.squaredNorm(),row.squaredNorm()); }) );
|
||||||
|
|
||||||
VectorType col = VectorType::Random(rows);
|
VectorType col = VectorType::Random(rows);
|
||||||
A.colwise() = col;
|
A.colwise() = col;
|
||||||
VERIFY( std::all_of(A.colwise().begin(), A.colwise().end(), [&col](typename ColMatrixType::ColXpr x) { return internal::isApprox(x.norm(),col.norm()); }) );
|
VERIFY( std::all_of(A.colwise().begin(), A.colwise().end(), [&col](typename ColMatrixType::ColXpr x) { return internal::isApprox(x.squaredNorm(),col.squaredNorm()); }) );
|
||||||
VERIFY( std::all_of(A.colwise().cbegin(), A.colwise().cend(), [&col](typename ColMatrixType::ConstColXpr x) { return internal::isApprox(x.norm(),col.norm()); }) );
|
VERIFY( std::all_of(A.colwise().cbegin(), A.colwise().cend(), [&col](typename ColMatrixType::ConstColXpr x) { return internal::isApprox(x.squaredNorm(),col.squaredNorm()); }) );
|
||||||
|
|
||||||
i = internal::random<Index>(0,A.rows()-1);
|
i = internal::random<Index>(0,A.rows()-1);
|
||||||
A.setRandom();
|
A.setRandom();
|
||||||
A.row(i).setZero();
|
A.row(i).setZero();
|
||||||
VERIFY_IS_EQUAL( std::find_if(A.rowwise().begin(), A.rowwise().end(), [](typename ColMatrixType::RowXpr x) { return x.norm() == Scalar(0); })-A.rowwise().begin(), i );
|
VERIFY_IS_EQUAL( std::find_if(A.rowwise().begin(), A.rowwise().end(), [](typename ColMatrixType::RowXpr x) { return x.squaredNorm() == Scalar(0); })-A.rowwise().begin(), i );
|
||||||
|
|
||||||
j = internal::random<Index>(0,A.cols()-1);
|
j = internal::random<Index>(0,A.cols()-1);
|
||||||
A.setRandom();
|
A.setRandom();
|
||||||
A.col(j).setZero();
|
A.col(j).setZero();
|
||||||
VERIFY_IS_EQUAL( std::find_if(A.colwise().begin(), A.colwise().end(), [](typename ColMatrixType::ColXpr x) { return x.norm() == Scalar(0); })-A.colwise().begin(), j );
|
VERIFY_IS_EQUAL( std::find_if(A.colwise().begin(), A.colwise().end(), [](typename ColMatrixType::ColXpr x) { return x.squaredNorm() == Scalar(0); })-A.colwise().begin(), j );
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user