alpaka
Abstraction Library for Parallel Kernel Acceleration
FloatEqualExact.hpp
Go to the documentation of this file.
1 /* Copyright 2021 Jiri Vyskocil
2  * SPDX-License-Identifier: MPL-2.0
3  */
4 
5 #pragma once
6 
7 #include "alpaka/core/Common.hpp"
8 
9 #include <type_traits>
10 
11 namespace alpaka
12 {
13  namespace math
14  {
15  /** Compare two floating point numbers for exact equivalence. Use only when necessary, and be aware of the
16  * implications. Most codes should not use this function and instead implement a correct epsilon-based
17  * comparison. If you are unfamiliar with the topic, check out
18  * https://www.geeksforgeeks.org/problem-in-comparing-floating-point-numbers-and-how-to-compare-them-correctly/
19  * or Goldberg 1991: "What every computer scientist should know about floating-point arithmetic",
20  * https://dl.acm.org/doi/10.1145/103162.103163
21  *
22  * This function calls the == operator for floating point types, but disables the warning issued by the
23  * compiler when compiling with the float equality warning checks enabled. This warning is valid an valuable in
24  * most codes and should be generally enabled, but there are specific instances where a piece of code might
25  * need to do an exact comparison (e.g. @a CudaVectorArrayWrapperTest.cpp). The verbose name for the function
26  * is intentional as it should raise a red flag if used while not absolutely needed. Users are advised to add a
27  * justification whenever they use this function.
28  *
29  * @tparam T both operands have to be the same type and conform to std::is_floating_point
30  * @param a first operand
31  * @param b second operand
32  * @return a == b
33  */
34  template<typename T>
36  {
37  static_assert(std::is_floating_point_v<T>, "floatEqualExactNoWarning is for floating point values only!");
38 
39  // So far only GCC and Clang check for float comparison and both accept the GCC pragmas.
40 #ifdef __GNUC__
41 # pragma GCC diagnostic push
42 # pragma GCC diagnostic ignored "-Wfloat-equal"
43 #endif
44  return a == b;
45 #ifdef __GNUC__
46 # pragma GCC diagnostic pop
47 #endif
48  }
49  } // namespace math
50 } // namespace alpaka
#define ALPAKA_FN_HOST_ACC
Definition: Common.hpp:39
#define ALPAKA_FN_INLINE
Macro defining the inline function attribute.
Definition: Common.hpp:95
ALPAKA_FN_INLINE ALPAKA_FN_HOST_ACC auto floatEqualExactNoWarning(T a, T b) -> bool
The alpaka accelerator library.