[petsc-dev] complex fix and -Wfloat-equal

Lisandro Dalcin dalcinl at gmail.com
Mon Jan 27 15:42:17 CST 2020


I usually compile my code with almost most warning flags on, including
-Wfloat-equal.
My implementation of the C++ complex fix that is not included by default is
an obvious offender (see warning messages at the end).

A simple fix (and I think it is a safe one, same thing is used in the boost
libraries,
https://www.boost.org/doc/libs/1_72_0/boost/config/detail/suffix.hpp) that
works with both GCC and clang (and most likely Intel, I'll try tomorrow) is
the following:

diff --git a/include/petsccxxcomplexfix.h b/include/petsccxxcomplexfix.h
index d3614e8a62..3c3090215b 100644
--- a/include/petsccxxcomplexfix.h
+++ b/include/petsccxxcomplexfix.h
@@ -35,6 +35,10 @@
      before including any PETSc include files to prevent these methods
from being provided.
 */

+#if defined(__GNUC__) && (__GNUC__ >= 4)
+#pragma GCC system_header
+#endif
+
 #define PETSC_CXX_COMPLEX_FIX(Type) \
 static inline PetscComplex operator+(const PetscComplex& lhs, const Type&
rhs) { return const_cast<PetscComplex&>(lhs) + PetscReal(rhs); } \
 static inline PetscComplex operator+(const Type& lhs, const PetscComplex&
rhs) { return PetscReal(lhs) + const_cast<PetscComplex&>(rhs); } \

Would that be an acceptable solution o we should rather implement a
configure test and define PETSC_HAVE_PRAGMA_GCC_SYSTEM_HEADER ?

These are the kind of errors that show up for EVERY C++ source compile if
-Wfloat-equal is passed.

/home/devel/petsc/dev/include/petsccxxcomplexfix.h: In function ‘bool
operator==(const PetscComplex&, const PetscInt&)’:
/home/devel/petsc/dev/include/petsccxxcomplexfix.h:47:134: warning:
comparing floating point with == or != is unsafe [-Wfloat-equal]
   47 | static inline bool operator==(const PetscComplex& lhs, const Type&
rhs) { return const_cast<PetscComplex&>(lhs).imag() == PetscReal(0) &&
const_cast<PetscComplex&>(lhs).real() == PetscReal(rhs); } \
      |
                                                                 ^
/home/devel/petsc/dev/include/petsccxxcomplexfix.h:60:1: note: in expansion
of macro ‘PETSC_CXX_COMPLEX_FIX’
   60 | PETSC_CXX_COMPLEX_FIX(PetscInt)

-- 
Lisandro Dalcin
============
Research Scientist
Extreme Computing Research Center (ECRC)
King Abdullah University of Science and Technology (KAUST)
http://ecrc.kaust.edu.sa/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-dev/attachments/20200128/89d7b989/attachment.html>


More information about the petsc-dev mailing list