ALPSCore reference
is_zero.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1998-2018 ALPS Collaboration. See COPYRIGHT.TXT
3  * All rights reserved. Use is subject to license terms. See LICENSE.TXT
4  * For use in publications, see ACKNOWLEDGE.TXT
5  */
6 
7 /* $Id$ */
8 
9 #ifndef ALPS_NUMERIC_IS_ZERO_HPP
10 #define ALPS_NUMERIC_IS_ZERO_HPP
11 
12 #include <cmath>
13 #include <type_traits>
14 
15 namespace alps { namespace numeric {
16 
17 namespace detail {
18 
19 template<class T, unsigned int N = 5>
20 struct precision
21 {
22  static inline T epsilon() { return 1e-50; }
23 };
24 template<class T> struct precision<T, 0>;
25 template<class T> struct precision<T, 1>
26 {
27  static inline T epsilon() { return 1e-10; }
28 };
29 template<class T> struct precision<T, 2>
30 {
31  static inline T epsilon() { return 1e-20; }
32 };
33 template<class T> struct precision<T, 3>
34 {
35  static inline T epsilon() { return 1e-30; }
36 };
37 template<class T> struct precision<T, 4>
38 {
39  static inline T epsilon() { return 1e-40; }
40 };
41 
42 } // end namespace detail
43 
44 
45 //
46 // is_zero
47 //
48 
53 template<unsigned int N, class T>
54 inline bool is_zero(T x,
55  typename std::enable_if<std::is_arithmetic<T>::value >::type* = 0,
56  typename std::enable_if<std::is_float<T>::value >::type* = 0)
57 { return std::abs(x) < detail::precision<T, N>::epsilon(); }
58 template<unsigned int N, class T>
59 inline bool is_zero(T x,
60  typename std::enable_if<std::is_arithmetic<T>::value >::type* = 0,
61  typename std::enable_if<std::is_integral<T>::value >::type* = 0)
62 { return x == T(0); }
63 
64 namespace detail {
65 
66 template<unsigned int N, class T>
67 struct is_zero_helper
68 {
69  static bool result(const T& x) { return x == T(0); }
70 };
71 template<unsigned int N, class T>
72 struct is_zero_helper<N, std::complex<T> >
73 {
74  static bool result(const std::complex<T>& x)
75  { return is_zero<N>(std::abs(x)); }
76 };
77 
78 } // end namespace detail
79 
80 template<unsigned int N, class T>
81 inline bool is_zero(const T& x,
82  typename std::enable_if<!std::is_arithmetic<T>::value >::type* = 0)
84 
85 template<class T>
86 inline bool is_zero(T x,
87  typename std::enable_if<std::is_arithmetic<T>::value >::type* = 0,
88  typename std::enable_if<std::is_float<T>::value >::type* = 0)
89 { return std::abs(x) < detail::precision<T>::epsilon(); }
90 template<class T>
91 inline bool is_zero(T x,
92  typename std::enable_if<std::is_arithmetic<T>::value >::type* = 0,
93  typename std::enable_if<std::is_integral<T>::value >::type* = 0)
94 { return x == T(0); }
95 template<class T>
96 inline bool is_zero(const T& x,
97  typename std::enable_if<!std::is_arithmetic<T>::value >::type* = 0)
98 { return x == T(0); }
99 template<class T>
100 inline bool is_zero(const std::complex<T>& x)
101 { return is_zero(std::abs(x)); }
102 
103 } } // end namespace
104 
105 #endif // ALPS_NUMERIC_IS_ZERO_HPP
boost::array< T, N > abs(boost::array< T, N > arg)
bool is_zero(T x, typename std::enable_if< std::is_arithmetic< T >::value >::type *=0, typename std::enable_if< std::is_float< T >::value >::type *=0)
checks if a number is zero in case of a floating point number, absolute values less than epsilon (1e-...
Definition: is_zero.hpp:54
traits< Acc >::result_type result(const Acc &acc)
Definition: util.hpp:53