Skip to content

Commit

Permalink
Fix uniform_floating_point_distribution for unit ranges
Browse files Browse the repository at this point in the history
  • Loading branch information
horenmar committed Dec 10, 2023
1 parent 46ebc10 commit 7ca1e2c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/catch2/internal/catch_random_floating_point_helpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace Catch {
"gamma returns the largest ULP magnitude within "
"floating point range [a, b]. This only makes sense "
"for floating point types" );
assert( a < b );
assert( a <= b );

const auto gamma_up = Catch::nextafter( a, std::numeric_limits<FloatType>::infinity() ) - a;
const auto gamma_down = b - Catch::nextafter( b, -std::numeric_limits<FloatType>::infinity() );
Expand Down Expand Up @@ -69,7 +69,7 @@ namespace Catch {
template <typename FloatType>
DistanceType<FloatType>
count_equidistant_floats( FloatType a, FloatType b, FloatType distance ) {
assert( a < b );
assert( a <= b );
// We get distance as gamma for our uniform float distribution,
// so this will round perfectly.
const auto ag = a / distance;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class uniform_floating_point_distribution {
m_max_steps_in_one_go( Detail::calculate_max_steps_in_one_go(m_ulp_magnitude)),
m_a_has_leq_magnitude(std::fabs(m_a) <= std::fabs(m_b))
{
assert( a < b );
assert( a <= b );
}

template <typename Generator>
Expand Down
20 changes: 20 additions & 0 deletions tests/SelfTest/IntrospectiveTests/RandomNumberGeneration.tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include <catch2/catch_test_macros.hpp>
#include <catch2/catch_template_test_macros.hpp>
#include <catch2/internal/catch_floating_point_helpers.hpp>
#include <catch2/internal/catch_random_integer_helpers.hpp>
#include <catch2/internal/catch_random_number_generator.hpp>
#include <catch2/internal/catch_random_seed_generation.hpp>
Expand Down Expand Up @@ -568,3 +569,22 @@ TEMPLATE_TEST_CASE( "uniform_floating_point_distribution is reproducible",

REQUIRE_THAT( generated, Catch::Matchers::RangeEquals( uniform_fp_test_params<TestType>::expected ) );
}

TEMPLATE_TEST_CASE( "uniform_floating_point_distribution can handle unitary ranges",
"[rng][distribution][floating-point][approvals]",
float,
double ) {
std::random_device rd;
auto seed = rd();
CAPTURE( seed );
Catch::SimplePcg32 pcg( seed );

const auto highest = uniform_fp_test_params<TestType>::highest;
Catch::uniform_floating_point_distribution<TestType> dist( highest,
highest );

constexpr auto iters = 20;
for (int i = 0; i < iters; ++i) {
REQUIRE( Catch::Detail::directCompare( dist( pcg ), highest ) );
}
}

0 comments on commit 7ca1e2c

Please sign in to comment.