reduce float warnings (comparisons and implicit conversions)

This commit is contained in:
Erik Schultheis
2022-01-26 18:16:19 +00:00
committed by Rasmus Munk Larsen
parent 51311ec651
commit d271a7d545
41 changed files with 152 additions and 133 deletions

View File

@@ -32,12 +32,12 @@ class AnnoyingScalar
{
public:
AnnoyingScalar() { init(); *v = 0; }
AnnoyingScalar(long double _v) { init(); *v = _v; }
AnnoyingScalar(double _v) { init(); *v = _v; }
AnnoyingScalar(long double _v) { init(); *v = static_cast<float>(_v); }
AnnoyingScalar(double _v) { init(); *v = static_cast<float>(_v); }
AnnoyingScalar(float _v) { init(); *v = _v; }
AnnoyingScalar(int _v) { init(); *v = _v; }
AnnoyingScalar(long _v) { init(); *v = _v; }
AnnoyingScalar(long long _v) { init(); *v = _v; }
AnnoyingScalar(int _v) { init(); *v = static_cast<float>(_v); }
AnnoyingScalar(long _v) { init(); *v = static_cast<float>(_v); }
AnnoyingScalar(long long _v) { init(); *v = static_cast<float>(_v); }
AnnoyingScalar(const AnnoyingScalar& other) { init(); *v = *(other.v); }
~AnnoyingScalar() {
if(v!=&data)
@@ -81,8 +81,8 @@ class AnnoyingScalar
AnnoyingScalar& operator/=(const AnnoyingScalar& other) { *v /= *other.v; return *this; }
AnnoyingScalar& operator= (const AnnoyingScalar& other) { *v = *other.v; return *this; }
bool operator==(const AnnoyingScalar& other) const { return *v == *other.v; }
bool operator!=(const AnnoyingScalar& other) const { return *v != *other.v; }
bool operator==(const AnnoyingScalar& other) const { return numext::equal_strict(*v, *other.v); }
bool operator!=(const AnnoyingScalar& other) const { return numext::not_equal_strict(*v, *other.v); }
bool operator<=(const AnnoyingScalar& other) const { return *v <= *other.v; }
bool operator< (const AnnoyingScalar& other) const { return *v < *other.v; }
bool operator>=(const AnnoyingScalar& other) const { return *v >= *other.v; }

View File

@@ -45,7 +45,7 @@ template<> struct adjoint_specific<false> {
VERIFY_IS_APPROX((v1*0).normalized(), (v1*0));
#if (!EIGEN_ARCH_i386) || defined(EIGEN_VECTORIZE)
RealScalar very_small = (std::numeric_limits<RealScalar>::min)();
VERIFY( (v1*very_small).norm() == 0 );
VERIFY( numext::is_exactly_zero((v1*very_small).norm()) );
VERIFY_IS_APPROX((v1*very_small).normalized(), (v1*very_small));
v3 = v1*very_small;
v3.normalize();

View File

@@ -149,11 +149,11 @@ template<typename MatrixType> void block(const MatrixType& m)
}
// stress some basic stuffs with block matrices
VERIFY(numext::real(ones.col(c1).sum()) == RealScalar(rows));
VERIFY(numext::real(ones.row(r1).sum()) == RealScalar(cols));
VERIFY_IS_EQUAL(numext::real(ones.col(c1).sum()), RealScalar(rows));
VERIFY_IS_EQUAL(numext::real(ones.row(r1).sum()), RealScalar(cols));
VERIFY(numext::real(ones.col(c1).dot(ones.col(c2))) == RealScalar(rows));
VERIFY(numext::real(ones.row(r1).dot(ones.row(r2))) == RealScalar(cols));
VERIFY_IS_EQUAL(numext::real(ones.col(c1).dot(ones.col(c2))), RealScalar(rows));
VERIFY_IS_EQUAL(numext::real(ones.row(r1).dot(ones.row(r2))), RealScalar(cols));
// check that linear acccessors works on blocks
m1 = m1_copy;

View File

@@ -26,7 +26,7 @@ void verify_euler(const Matrix<Scalar,3,1>& ea, int i, int j, int k)
VERIFY_IS_APPROX(m, mbis);
/* If I==K, and ea[1]==0, then there no unique solution. */
/* The remark apply in the case where I!=K, and |ea[1]| is close to pi/2. */
if( (i!=k || ea[1]!=0) && (i==k || !internal::isApprox(abs(ea[1]),Scalar(EIGEN_PI/2),test_precision<Scalar>())) )
if((i!=k || !numext::is_exactly_zero(ea[1])) && (i == k || !internal::isApprox(abs(ea[1]), Scalar(EIGEN_PI / 2), test_precision<Scalar>())) )
VERIFY((ea-eabis).norm() <= test_precision<Scalar>());
// approx_or_less_than does not work for 0

View File

@@ -29,8 +29,8 @@ template<int Alignment,typename VectorType> void map_class_vector(const VectorTy
map = v;
for(int i = 0; i < size; ++i)
{
VERIFY(array[3*i] == v[i]);
VERIFY(map[i] == v[i]);
VERIFY_IS_EQUAL(array[3*i], v[i]);
VERIFY_IS_EQUAL(map[i], v[i]);
}
}
@@ -39,8 +39,8 @@ template<int Alignment,typename VectorType> void map_class_vector(const VectorTy
map = v;
for(int i = 0; i < size; ++i)
{
VERIFY(array[2*i] == v[i]);
VERIFY(map[i] == v[i]);
VERIFY_IS_EQUAL(array[2*i], v[i]);
VERIFY_IS_EQUAL(map[i], v[i]);
}
}
@@ -84,8 +84,8 @@ template<int Alignment,typename MatrixType> void map_class_matrix(const MatrixTy
for(int i = 0; i < m.outerSize(); ++i)
for(int j = 0; j < m.innerSize(); ++j)
{
VERIFY(array[map.outerStride()*i+j] == m.coeffByOuterInner(i,j));
VERIFY(map.coeffByOuterInner(i,j) == m.coeffByOuterInner(i,j));
VERIFY_IS_EQUAL(array[map.outerStride()*i+j], m.coeffByOuterInner(i,j));
VERIFY_IS_EQUAL(map.coeffByOuterInner(i,j), m.coeffByOuterInner(i,j));
}
VERIFY_IS_APPROX(s1*map,s1*m);
map *= s1;
@@ -111,8 +111,8 @@ template<int Alignment,typename MatrixType> void map_class_matrix(const MatrixTy
for(int i = 0; i < m.outerSize(); ++i)
for(int j = 0; j < m.innerSize(); ++j)
{
VERIFY(array[map.outerStride()*i+j] == m.coeffByOuterInner(i,j));
VERIFY(map.coeffByOuterInner(i,j) == m.coeffByOuterInner(i,j));
VERIFY_IS_EQUAL(array[map.outerStride()*i+j], m.coeffByOuterInner(i,j));
VERIFY_IS_EQUAL(map.coeffByOuterInner(i,j), m.coeffByOuterInner(i,j));
}
VERIFY_IS_APPROX(s1*map,s1*m);
map *= s1;
@@ -133,8 +133,8 @@ template<int Alignment,typename MatrixType> void map_class_matrix(const MatrixTy
for(int i = 0; i < m.outerSize(); ++i)
for(int j = 0; j < m.innerSize(); ++j)
{
VERIFY(array[map.outerStride()*i+map.innerStride()*j] == m.coeffByOuterInner(i,j));
VERIFY(map.coeffByOuterInner(i,j) == m.coeffByOuterInner(i,j));
VERIFY_IS_EQUAL(array[map.outerStride()*i+map.innerStride()*j], m.coeffByOuterInner(i,j));
VERIFY_IS_EQUAL(map.coeffByOuterInner(i,j), m.coeffByOuterInner(i,j));
}
VERIFY_IS_APPROX(s1*map,s1*m);
map *= s1;
@@ -154,8 +154,8 @@ template<int Alignment,typename MatrixType> void map_class_matrix(const MatrixTy
for(int i = 0; i < m.outerSize(); ++i)
for(int j = 0; j < m.innerSize(); ++j)
{
VERIFY(array[map.innerSize()*i*2+j*2] == m.coeffByOuterInner(i,j));
VERIFY(map.coeffByOuterInner(i,j) == m.coeffByOuterInner(i,j));
VERIFY_IS_EQUAL(array[map.innerSize()*i*2+j*2], m.coeffByOuterInner(i,j));
VERIFY_IS_EQUAL(map.coeffByOuterInner(i,j), m.coeffByOuterInner(i,j));
}
VERIFY_IS_APPROX(s1*map,s1*m);
map *= s1;

View File

@@ -13,24 +13,20 @@
template<typename MatrixType>
bool equalsIdentity(const MatrixType& A)
{
typedef typename MatrixType::Scalar Scalar;
Scalar zero = static_cast<Scalar>(0);
bool offDiagOK = true;
for (Index i = 0; i < A.rows(); ++i) {
for (Index j = i+1; j < A.cols(); ++j) {
offDiagOK = offDiagOK && (A(i,j) == zero);
offDiagOK = offDiagOK && numext::is_exactly_zero(A(i, j));
}
}
for (Index i = 0; i < A.rows(); ++i) {
for (Index j = 0; j < (std::min)(i, A.cols()); ++j) {
offDiagOK = offDiagOK && (A(i,j) == zero);
offDiagOK = offDiagOK && numext::is_exactly_zero(A(i, j));
}
}
bool diagOK = (A.diagonal().array() == 1).all();
return offDiagOK && diagOK;
}
template<typename VectorType>

View File

@@ -11,7 +11,7 @@
template<typename T, typename U>
bool check_if_equal_or_nans(const T& actual, const U& expected) {
return ((actual == expected) || ((numext::isnan)(actual) && (numext::isnan)(expected)));
return (numext::equal_strict(actual, expected) || ((numext::isnan)(actual) && (numext::isnan)(expected)));
}
template<typename T, typename U>

View File

@@ -100,7 +100,7 @@ template<typename Scalar> bool areApprox(const Scalar* a, const Scalar* b, int s
{
for (int i=0; i<size; ++i)
{
if ( a[i]!=b[i] && !internal::isApprox(a[i],b[i])
if ( numext::not_equal_strict(a[i], b[i]) && !internal::isApprox(a[i],b[i])
&& !((numext::isnan)(a[i]) && (numext::isnan)(b[i])) )
{
print_mismatch(a, b, size);
@@ -114,7 +114,7 @@ template<typename Scalar> bool areEqual(const Scalar* a, const Scalar* b, int si
{
for (int i=0; i<size; ++i)
{
if ( (a[i] != b[i]) && !((numext::isnan)(a[i]) && (numext::isnan)(b[i])) )
if ( numext::not_equal_strict(a[i], b[i]) && !((numext::isnan)(a[i]) && (numext::isnan)(b[i])) )
{
print_mismatch(a, b, size);
return false;

View File

@@ -18,7 +18,6 @@ template<typename MatrixType> void real_qz(const MatrixType& m)
RealQZ.h
*/
using std::abs;
typedef typename MatrixType::Scalar Scalar;
Index dim = m.cols();
@@ -52,17 +51,18 @@ template<typename MatrixType> void real_qz(const MatrixType& m)
bool all_zeros = true;
for (Index i=0; i<A.cols(); i++)
for (Index j=0; j<i; j++) {
if (abs(qz.matrixT()(i,j))!=Scalar(0.0))
if (!numext::is_exactly_zero(abs(qz.matrixT()(i, j))))
{
std::cerr << "Error: T(" << i << "," << j << ") = " << qz.matrixT()(i,j) << std::endl;
all_zeros = false;
}
if (j<i-1 && abs(qz.matrixS()(i,j))!=Scalar(0.0))
if (j<i-1 && !numext::is_exactly_zero(abs(qz.matrixS()(i, j))))
{
std::cerr << "Error: S(" << i << "," << j << ") = " << qz.matrixS()(i,j) << std::endl;
all_zeros = false;
}
if (j==i-1 && j>0 && abs(qz.matrixS()(i,j))!=Scalar(0.0) && abs(qz.matrixS()(i-1,j-1))!=Scalar(0.0))
if (j==i-1 && j>0 && !numext::is_exactly_zero(abs(qz.matrixS()(i, j))) &&
!numext::is_exactly_zero(abs(qz.matrixS()(i - 1, j - 1))))
{
std::cerr << "Error: S(" << i << "," << j << ") = " << qz.matrixS()(i,j) << " && S(" << i-1 << "," << j-1 << ") = " << qz.matrixS()(i-1,j-1) << std::endl;
all_zeros = false;

View File

@@ -19,15 +19,15 @@ template<typename MatrixType> void verifyIsQuasiTriangular(const MatrixType& T)
// Check T is lower Hessenberg
for(int row = 2; row < size; ++row) {
for(int col = 0; col < row - 1; ++col) {
VERIFY(T(row,col) == Scalar(0));
VERIFY_IS_EQUAL(T(row,col), Scalar(0));
}
}
// Check that any non-zero on the subdiagonal is followed by a zero and is
// part of a 2x2 diagonal block with imaginary eigenvalues.
for(int row = 1; row < size; ++row) {
if (T(row,row-1) != Scalar(0)) {
VERIFY(row == size-1 || T(row+1,row) == 0);
if (!numext::is_exactly_zero(T(row, row - 1))) {
VERIFY(row == size-1 || numext::is_exactly_zero(T(row + 1, row)));
Scalar tr = T(row-1,row-1) + T(row,row);
Scalar det = T(row-1,row-1) * T(row,row) - T(row-1,row) * T(row,row-1);
VERIFY(4 * det > tr * tr);

View File

@@ -54,7 +54,8 @@ initSparse(double density,
enum { IsRowMajor = SparseMatrix<Scalar,Opt2,StorageIndex>::IsRowMajor };
sparseMat.setZero();
//sparseMat.reserve(int(refMat.rows()*refMat.cols()*density));
sparseMat.reserve(VectorXi::Constant(IsRowMajor ? refMat.rows() : refMat.cols(), int((1.5*density)*(IsRowMajor?refMat.cols():refMat.rows()))));
int nnz = static_cast<int>((1.5 * density) * static_cast<double>(IsRowMajor ? refMat.cols() : refMat.rows()));
sparseMat.reserve(VectorXi::Constant(IsRowMajor ? refMat.rows() : refMat.cols(), nnz));
Index insert_count = 0;
for(Index j=0; j<sparseMat.outerSize(); j++)
@@ -82,7 +83,7 @@ initSparse(double density,
if ((flags&ForceRealDiag) && (i==j))
v = numext::real(v);
if (v!=Scalar(0))
if (!numext::is_exactly_zero(v))
{
//sparseMat.insertBackByOuterInner(j,i) = v;
sparseMat.insertByOuterInner(j,i) = v;
@@ -115,7 +116,7 @@ initSparse(double density,
for(int i=0; i<refVec.size(); i++)
{
Scalar v = (internal::random<double>(0,1) < density) ? internal::random<Scalar>() : Scalar(0);
if (v!=Scalar(0))
if (!numext::is_exactly_zero(v))
{
sparseVec.insertBack(i) = v;
if (nonzeroCoords)

View File

@@ -679,7 +679,7 @@ void big_sparse_triplet(Index rows, Index cols, double density) {
typedef typename SparseMatrixType::Scalar Scalar;
typedef Triplet<Scalar,Index> TripletType;
std::vector<TripletType> triplets;
double nelements = density * rows*cols;
double nelements = density * static_cast<double>(rows*cols);
VERIFY(nelements>=0 && nelements < static_cast<double>(NumTraits<StorageIndex>::highest()));
Index ntriplets = Index(nelements);
triplets.reserve(ntriplets);

View File

@@ -90,11 +90,11 @@ template<typename SparseMatrixType> void sparse_block(const SparseMatrixType& re
VERIFY_IS_APPROX(m.middleCols(j,w).coeff(r,c), refMat.middleCols(j,w).coeff(r,c));
VERIFY_IS_APPROX(m.middleRows(i,h).coeff(r,c), refMat.middleRows(i,h).coeff(r,c));
if(m.middleCols(j,w).coeff(r,c) != Scalar(0))
if(!numext::is_exactly_zero(m.middleCols(j, w).coeff(r, c)))
{
VERIFY_IS_APPROX(m.middleCols(j,w).coeffRef(r,c), refMat.middleCols(j,w).coeff(r,c));
}
if(m.middleRows(i,h).coeff(r,c) != Scalar(0))
if(!numext::is_exactly_zero(m.middleRows(i, h).coeff(r, c)))
{
VERIFY_IS_APPROX(m.middleRows(i,h).coeff(r,c), refMat.middleRows(i,h).coeff(r,c));
}
@@ -166,14 +166,14 @@ template<typename SparseMatrixType> void sparse_block(const SparseMatrixType& re
{
VERIFY(j==numext::real(m3.innerVector(j).nonZeros()));
if(j>0)
VERIFY(RealScalar(j)==numext::real(m3.innerVector(j).lastCoeff()));
VERIFY_IS_EQUAL(RealScalar(j), numext::real(m3.innerVector(j).lastCoeff()));
}
m3.makeCompressed();
for(Index j=0; j<(std::min)(outer, inner); ++j)
{
VERIFY(j==numext::real(m3.innerVector(j).nonZeros()));
if(j>0)
VERIFY(RealScalar(j)==numext::real(m3.innerVector(j).lastCoeff()));
VERIFY_IS_EQUAL(RealScalar(j), numext::real(m3.innerVector(j).lastCoeff()));
}
VERIFY(m3.innerVector(j0).nonZeros() == m3.transpose().innerVector(j0).nonZeros());

View File

@@ -53,7 +53,7 @@ template<int OtherStorage, typename SparseMatrixType> void sparse_permutations(c
// bool IsRowMajor1 = SparseMatrixType::IsRowMajor;
// bool IsRowMajor2 = OtherSparseMatrixType::IsRowMajor;
double density = (std::max)(8./(rows*cols), 0.01);
double density = (std::max)(8./static_cast<double>(rows*cols), 0.01);
SparseMatrixType mat(rows, cols), up(rows,cols), lo(rows,cols);
OtherSparseMatrixType res;

View File

@@ -390,7 +390,7 @@ void test_mixing_types()
typedef Matrix<Cplx,Dynamic,Dynamic> DenseMatCplx;
Index n = internal::random<Index>(1,100);
double density = (std::max)(8./(n*n), 0.2);
double density = (std::max)(8./static_cast<double>(n*n), 0.2);
SpMatReal sR1(n,n);
SpMatCplx sC1(n,n), sC2(n,n), sC3(n,n);

View File

@@ -350,7 +350,7 @@ int generate_sparse_spd_problem(Solver& , typename Solver::MatrixType& A, typena
typedef Matrix<Scalar,Dynamic,Dynamic> DenseMatrix;
int size = internal::random<int>(1,maxSize);
double density = (std::max)(8./(size*size), 0.01);
double density = (std::max)(8./static_cast<double>(size*size), 0.01);
Mat M(size, size);
DenseMatrix dM(size, size);
@@ -419,7 +419,7 @@ template<typename Solver> void check_sparse_spd_solving(Solver& solver, int maxS
// generate the right hand sides
int rhsCols = internal::random<int>(1,16);
double density = (std::max)(8./(size*rhsCols), 0.1);
double density = (std::max)(8./static_cast<double>(size*rhsCols), 0.1);
SpMat B(size,rhsCols);
DenseVector b = DenseVector::Random(size);
DenseMatrix dB(size,rhsCols);
@@ -510,7 +510,7 @@ Index generate_sparse_square_problem(Solver&, typename Solver::MatrixType& A, De
typedef typename Mat::Scalar Scalar;
Index size = internal::random<int>(1,maxSize);
double density = (std::max)(8./(size*size), 0.01);
double density = (std::max)(8./static_cast<double>(size*size), 0.01);
A.resize(size,size);
dA.resize(size,size);
@@ -551,7 +551,7 @@ template<typename Solver> void check_sparse_square_solving(Solver& solver, int m
DenseVector b = DenseVector::Random(size);
DenseMatrix dB(size,rhsCols);
SpMat B(size,rhsCols);
double density = (std::max)(8./(size*rhsCols), 0.1);
double density = (std::max)(8./double(size*rhsCols), 0.1);
initSparse<Scalar>(density, dB, B, ForceNonZeroDiag);
B.makeCompressed();
SpVec c = B.col(0);

View File

@@ -47,8 +47,8 @@ template<typename Scalar,typename StorageIndex> void sparse_vector(int rows, int
for (typename SparseVectorType::InnerIterator it(v1); it; ++it,++j)
{
VERIFY(nonzerocoords[j]==it.index());
VERIFY(it.value()==v1.coeff(it.index()));
VERIFY(it.value()==refV1.coeff(it.index()));
VERIFY_IS_EQUAL(it.value(), v1.coeff(it.index()));
VERIFY_IS_EQUAL(it.value(), refV1.coeff(it.index()));
}
}
VERIFY_IS_APPROX(v1, refV1);

View File

@@ -438,14 +438,14 @@ void test_stl_iterators(int rows=Rows, int cols=Cols)
i = internal::random<Index>(0,A.rows()-1);
A.setRandom();
A.row(i).setZero();
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 );
VERIFY_IS_EQUAL( std::find_if(A.rowwise().rbegin(), A.rowwise().rend(), [](typename ColMatrixType::RowXpr x) { return x.squaredNorm() == Scalar(0); })-A.rowwise().rbegin(), (A.rows()-1) - i );
VERIFY_IS_EQUAL(std::find_if(A.rowwise().begin(), A.rowwise().end(), [](typename ColMatrixType::RowXpr x) { return numext::is_exactly_zero(x.squaredNorm()); }) - A.rowwise().begin(), i );
VERIFY_IS_EQUAL(std::find_if(A.rowwise().rbegin(), A.rowwise().rend(), [](typename ColMatrixType::RowXpr x) { return numext::is_exactly_zero(x.squaredNorm()); }) - A.rowwise().rbegin(), (A.rows() - 1) - i );
j = internal::random<Index>(0,A.cols()-1);
A.setRandom();
A.col(j).setZero();
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 );
VERIFY_IS_EQUAL( std::find_if(A.colwise().rbegin(), A.colwise().rend(), [](typename ColMatrixType::ColXpr x) { return x.squaredNorm() == Scalar(0); })-A.colwise().rbegin(), (A.cols()-1) - j );
VERIFY_IS_EQUAL(std::find_if(A.colwise().begin(), A.colwise().end(), [](typename ColMatrixType::ColXpr x) { return numext::is_exactly_zero(x.squaredNorm()); }) - A.colwise().begin(), j );
VERIFY_IS_EQUAL(std::find_if(A.colwise().rbegin(), A.colwise().rend(), [](typename ColMatrixType::ColXpr x) { return numext::is_exactly_zero(x.squaredNorm()); }) - A.colwise().rbegin(), (A.cols() - 1) - j );
}
{

View File

@@ -21,7 +21,7 @@ template<typename MatrixType> void matrixVisitor(const MatrixType& p)
m = MatrixType::Random(rows, cols);
for(Index i = 0; i < m.size(); i++)
for(Index i2 = 0; i2 < i; i2++)
while(m(i) == m(i2)) // yes, ==
while(numext::equal_strict(m(i), m(i2))) // yes, strict equality
m(i) = internal::random<Scalar>();
Scalar minc = Scalar(1000), maxc = Scalar(-1000);