2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2014, by the GROMACS development team, led by
5 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 * and including many others, as listed in the AUTHORS file in the
7 * top-level source directory and at http://www.gromacs.org.
9 * GROMACS is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1
12 * of the License, or (at your option) any later version.
14 * GROMACS is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with GROMACS; if not, see
21 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * If you want to redistribute modifications to GROMACS, please
25 * consider that scientific software is very special. Version
26 * control is crucial - bugs must be traceable. We will be happy to
27 * consider code for inclusion in the official distribution, but
28 * derived work must not be called official GROMACS. Details are found
29 * in the README & COPYING files - if they are missing, get the
30 * official version at http://www.gromacs.org.
32 * To help us fund GROMACS development, we humbly ask that you cite
33 * the research papers on the package. Check out http://www.gromacs.org.
37 * Implements floating-point comparison routines from testasserts.h.
39 * \author Teemu Murtola <teemu.murtola@gmail.com>
40 * \ingroup module_testutils
44 #include "testutils/testasserts.h"
50 #include <gtest/gtest.h>
52 #include "gromacs/utility/basedefinitions.h"
53 #include "gromacs/utility/stringutil.h"
64 using ::testing::internal::FloatingPoint;
66 //! \internal \addtogroup module_testutils
69 /*! \name Helper functions for computing floating-point differences
71 * These routines are used to initialize FloatingPointDifference.
72 * They peek into some internal types from Google Test (gtest-internal.h),
73 * and duplicate some other functionality from there, but that is likely
74 * a better alternative than just copying all that code here.
79 * Computes biased integer representation for a floating-point value.
81 * This moves the integer representation from a sign-and-magnitude
82 * representation to a biased representation where the 0x8000... represents
83 * zero, and the order of the integer values matches the order of the
84 * floating-point values.
86 template <typename FloatType>
87 typename FloatingPoint<FloatType>::Bits
88 floatingPointToBiasedInteger(const FloatingPoint<FloatType> &value)
92 return ~value.bits() + 1;
96 return value.bits() | FloatingPoint<FloatType>::kSignBitMask;
101 * Computes difference in ULPs between two numbers, treating also values of
104 template <typename FloatType>
105 gmx_uint64_t calculateUlpDifference(const FloatingPoint<FloatType> &value1,
106 const FloatingPoint<FloatType> &value2)
108 typename FloatingPoint<FloatType>::Bits biased1
109 = floatingPointToBiasedInteger(value1);
110 typename FloatingPoint<FloatType>::Bits biased2
111 = floatingPointToBiasedInteger(value2);
112 return biased1 > biased2 ? biased1 - biased2 : biased2 - biased1;
116 * Helper to implement the constructors for FloatingPointDifference.
118 template <typename FloatType>
119 void initDifference(FloatType raw1, FloatType raw2, double *absoluteDifference,
120 gmx_uint64_t *ulpDifference, bool *bSignDifference)
122 FloatingPoint<FloatType> value1(raw1);
123 FloatingPoint<FloatType> value2(raw2);
125 if (value1.is_nan() || value2.is_nan())
127 *absoluteDifference = std::numeric_limits<double>::quiet_NaN();
128 *bSignDifference = false;
132 *absoluteDifference = std::fabs(raw1 - raw2);
133 *bSignDifference = (value1.sign_bit() != value2.sign_bit());
134 *ulpDifference = calculateUlpDifference(value1, value2);
142 /********************************************************************
143 * FloatingPointDifference
146 FloatingPointDifference::FloatingPointDifference(float value1, float value2)
148 initDifference(value1, value2,
149 &absoluteDifference_, &ulpDifference_, &bSignDifference_);
153 FloatingPointDifference::FloatingPointDifference(double value1, double value2)
155 initDifference(value1, value2,
156 &absoluteDifference_, &ulpDifference_, &bSignDifference_);
160 bool FloatingPointDifference::isNaN() const
162 return FloatingPoint<double>(absoluteDifference_).is_nan();
165 std::string FloatingPointDifference::toString() const
167 return formatString("%g (%" GMX_PRIu64 " %s-prec. ULPs)%s",
168 absoluteDifference_, ulpDifference_,
169 bDouble_ ? "double" : "single",
170 bSignDifference_ ? ", signs differ" : "");
173 /********************************************************************
174 * FloatingPointTolerance
177 bool FloatingPointTolerance::isWithin(
178 const FloatingPointDifference &difference) const
180 if (difference.isNaN())
185 if (bSignMustMatch_ && difference.signsDiffer())
190 if (difference.asAbsolute() < absoluteTolerance_)
195 if (ulpTolerance_ >= 0
196 && difference.asUlps() <= static_cast<gmx_uint64_t>(ulpTolerance_))
203 std::string FloatingPointTolerance::toString() const
206 if (absoluteTolerance_ > 0.0)
208 result.append(formatString("abs. %g", absoluteTolerance_));
210 if (ulpTolerance_ >= 0)
216 result.append(formatString("%d ULPs", ulpTolerance_));
224 result.append("sign must match");