brintos

brintos / llvm-project-archived public Read only

0
0
Text · 1.3 KiB · 237ee50 Raw
44 lines · c
1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9#ifndef SUPPORT_FP_COMPARE_H10#define SUPPORT_FP_COMPARE_H11 12#include <algorithm> // for std::max13#include <cassert>14#include <cmath> // for std::abs15 16#include "test_macros.h"17 18// See https://www.boost.org/doc/libs/1_70_0/libs/test/doc/html/boost_test/testing_tools/extended_comparison/floating_point/floating_points_comparison_theory.html19 20template <typename T>21bool fptest_close(T val, T expected, T eps) {22  TEST_CONSTEXPR T zero = T(0);23  assert(eps >= zero);24 25  // Handle the zero cases26  if (eps == zero)27    return val == expected;28  if (val == zero)29    return std::abs(expected) <= eps;30  if (expected == zero)31    return std::abs(val) <= eps;32 33  return std::abs(val - expected) < eps && std::abs(val - expected) / std::abs(val) < eps;34}35 36template <typename T>37bool fptest_close_pct(T val, T expected, T percent) {38  assert(percent >= T(0));39  T eps = (percent / T(100)) * std::max(std::abs(val), std::abs(expected));40  return fptest_close(val, expected, eps);41}42 43#endif // SUPPORT_FP_COMPARE_H44