fix corner case in is_zero(): explicitly handle nan

This commit is contained in:
conrad
2026-03-23 15:12:22 +10:00
parent 825dbf7e60
commit f2691c6beb
2 changed files with 49 additions and 18 deletions
+40 -12
View File
@@ -261,24 +261,52 @@ BaseCube<elem_type,derived>::is_zero(const typename get_pod_type<elem_type>::res
const typename ProxyCube<derived>::ea_type Pea = P.get_ea();
if(is_cx<elem_type>::yes)
if(tol == T(0))
{
for(uword i=0; i<n_elem; ++i)
if(is_cx<elem_type>::yes)
{
const elem_type val = Pea[i];
const T val_real = access::tmp_real(val);
const T val_imag = access::tmp_imag(val);
if(eop_aux::arma_abs(val_real) > tol) { return false; }
if(eop_aux::arma_abs(val_imag) > tol) { return false; }
for(uword i=0; i<n_elem; ++i)
{
const elem_type val = Pea[i];
const T val_real = access::tmp_real(val);
const T val_imag = access::tmp_imag(val);
if(eop_aux::arma_abs(val_real) != T(0)) { return false; }
if(eop_aux::arma_abs(val_imag) != T(0)) { return false; }
}
}
else // not complex
{
for(uword i=0; i<n_elem; ++i)
{
if(eop_aux::arma_abs(Pea[i]) != T(0)) { return false; }
}
}
}
else // not complex
else // tol is not zero
{
for(uword i=0; i < n_elem; ++i)
if(is_cx<elem_type>::yes)
{
if(eop_aux::arma_abs(Pea[i]) > tol) { return false; }
for(uword i=0; i<n_elem; ++i)
{
const elem_type val = Pea[i];
const T val_real = access::tmp_real(val);
const T val_imag = access::tmp_imag(val);
if( (eop_aux::arma_abs(val_real) > tol) || arma_isnan(val_real) ) { return false; }
if( (eop_aux::arma_abs(val_imag) > tol) || arma_isnan(val_imag) ) { return false; }
}
}
else // not complex
{
for(uword i=0; i < n_elem; ++i)
{
const elem_type val = Pea[i];
if( (eop_aux::arma_abs(val) > tol) || arma_isnan(val) ) { return false; }
}
}
}
+9 -6
View File
@@ -1395,18 +1395,21 @@ subview_cube<eT>::is_zero(const typename get_pod_type<eT>::result tol) const
{
arma_debug_sigprint();
typedef typename get_pod_type<elem_type>::result T;
arma_conform_check( (tol < T(0)), "is_zero(): parameter 'tol' must be >= 0" );
const uword local_n_rows = n_rows;
const uword local_n_cols = n_cols;
const uword local_n_slices = n_slices;
if( (local_n_rows != 0) && (local_n_cols != 0) )
if( (local_n_rows == 0) || (local_n_cols == 0) || (local_n_slices == 0) ) { return false; }
for(uword slice = 0; slice < local_n_slices; ++slice)
{
for(uword slice = 0; slice < local_n_slices; ++slice)
for(uword col = 0; col < local_n_cols; ++col)
{
for(uword col = 0; col < local_n_cols; ++col)
{
if(arrayops::is_zero(slice_colptr(slice,col), local_n_rows, tol) == false) { return false; }
}
if(arrayops::is_zero(slice_colptr(slice,col), local_n_rows, tol) == false) { return false; }
}
}