1
0
mirror of https://github.com/catchorg/Catch2.git synced 2025-05-11 13:43:54 +00:00
Catch2/src/catch2/internal/catch_floating_point_helpers.cpp
Martin Hořeňovský d139b4ff7c
Add implementation of helpers for uniform float distribution
Specifically we add
 * `gamma(a, b)`, which returns the magnitude of largest 1-ULP
   step in range [a, b].
 * `count_equidistant_float(a, b, distance)`, which returns the
   number of equi-distant floats in range [a, b].
2023-12-10 18:44:10 +01:00

44 lines
1.3 KiB
C++

// Copyright Catch2 Authors
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.txt or copy at
// https://www.boost.org/LICENSE_1_0.txt)
// SPDX-License-Identifier: BSL-1.0
#include <catch2/internal/catch_floating_point_helpers.hpp>
#include <cstring>
namespace Catch {
namespace Detail {
uint32_t convertToBits(float f) {
static_assert(sizeof(float) == sizeof(uint32_t), "Important ULP matcher assumption violated");
uint32_t i;
std::memcpy(&i, &f, sizeof(f));
return i;
}
uint64_t convertToBits(double d) {
static_assert(sizeof(double) == sizeof(uint64_t), "Important ULP matcher assumption violated");
uint64_t i;
std::memcpy(&i, &d, sizeof(d));
return i;
}
#if defined( __GNUC__ ) || defined( __clang__ )
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wfloat-equal"
#endif
bool directCompare( float lhs, float rhs ) { return lhs == rhs; }
bool directCompare( double lhs, double rhs ) { return lhs == rhs; }
#if defined( __GNUC__ ) || defined( __clang__ )
# pragma GCC diagnostic pop
#endif
} // end namespace Detail
} // end namespace Catch