From f86d93e4dfe54c4a8f8d622b5be90ae75ca9620b Mon Sep 17 00:00:00 2001 From: Moritz Allmaras Date: Fri, 19 Oct 2007 20:09:51 +0000 Subject: [PATCH] Added is_finite() function for complex numbers git-svn-id: https://svn.dealii.org/trunk@15353 0785d39b-7218-0410-832d-ea1e28bc413d --- deal.II/base/include/base/numbers.h | 36 +++++++++++++++++++++++------ deal.II/base/source/config.cc | 24 ++++++++++++++++++- deal.II/lac/include/lac/vector.h | 2 +- 3 files changed, 53 insertions(+), 9 deletions(-) diff --git a/deal.II/base/include/base/numbers.h b/deal.II/base/include/base/numbers.h index cdf52f15a9..6439644bf4 100644 --- a/deal.II/base/include/base/numbers.h +++ b/deal.II/base/include/base/numbers.h @@ -15,6 +15,7 @@ #include +#include DEAL_II_NAMESPACE_OPEN @@ -102,12 +103,12 @@ namespace deal_II_numbers { */ static const double SQRT1_2 = 0.70710678118654752440; - /** - * Return @p true if the given - * value is a finite floating - * point number, i.e. is neither - * plus or minus infinity nor NaN - * (not a number). + /** + * Return @p true if the given + * value is a finite floating + * point number, i.e. is neither + * plus or minus infinity nor NaN + * (not a number). * * Note that the argument type of * this function is @@ -120,8 +121,29 @@ namespace deal_II_numbers { * number is finite with respect * to type long * double. - */ + */ bool is_finite (const double x); + + /** + * Return @p true if real and + * imaginary parts of the given + * complex number are finite. + */ + bool is_finite (const std::complex x); + + /** + * Return @p true if real and + * imaginary parts of the given + * complex number are finite. + * + * Again may not work correctly if + * real or imaginary parts are very + * large numbers that are infinite in + * terms of double, but + * finite with respect to + * long double. + */ + bool is_finite (const std::complex x); } DEAL_II_NAMESPACE_CLOSE diff --git a/deal.II/base/source/config.cc b/deal.II/base/source/config.cc index 80b5f4581a..46092dc9e1 100644 --- a/deal.II/base/source/config.cc +++ b/deal.II/base/source/config.cc @@ -15,6 +15,7 @@ #include #include #include +#include DEAL_II_NAMESPACE_OPEN @@ -25,7 +26,7 @@ namespace deal_II_numbers #ifdef DEAL_II_HAVE_ISFINITE return std::isfinite (x); #else - // check against infinities. not + // Check against infinities. Note // that if x is a NaN, then both // comparisons will be false return ((x >= -std::numeric_limits::max()) @@ -33,6 +34,27 @@ namespace deal_II_numbers (x <= std::numeric_limits::max())); #endif } + + + + bool is_finite (const std::complex x) + { + // Check complex numbers for infinity + // by testing real and imaginary part + return ( is_finite (x.real()) + && + is_finite (x.imag()) ); + } + + + + bool is_finite (const std::complex x) + { + // Same for std::complex + return ( is_finite (x.real()) + && + is_finite (x.imag()) ); + } } DEAL_II_NAMESPACE_CLOSE diff --git a/deal.II/lac/include/lac/vector.h b/deal.II/lac/include/lac/vector.h index 7251f05525..628c0a3291 100644 --- a/deal.II/lac/include/lac/vector.h +++ b/deal.II/lac/include/lac/vector.h @@ -857,7 +857,7 @@ Vector & Vector::operator = (const Number s) Assert (deal_II_numbers::is_finite(s), ExcMessage("The given value is not finite but either infinite or Not A Number (NaN)")); - if (s != 0.) + if (s != Number()) Assert (vec_size!=0, ExcEmptyObject()); if (vec_size!=0) std::fill (begin(), end(), s); -- 2.39.5