Skip to content

Commit

Permalink
Properly handle prob=1.0f due to precision conversion.
Browse files Browse the repository at this point in the history
  • Loading branch information
ye-luo committed Sep 20, 2024
1 parent 98ae91d commit 4cfc794
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/QMCHamiltonians/NonLocalTOperator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ NonLocalTOperator::NonLocalTOperator(const TmoveKind non_local_move_option,

const NonLocalData* NonLocalTOperator::selectMove(RealType prob, const std::vector<NonLocalData>& txy)
{
// Although prob is required to be [0, 1), the received value can still be 1 when there is precision conversion.
// For example, when the caller value is the largest value smaller than 1.0 in double precision,
// the callee (RealType=float) can still receive 1.0f after an implicit conversion.
// Here we adjust the value of prob to be slightly smaller than 1.
if (prob == RealType(1))
prob = std::nextafter(RealType(1), RealType(0));
assert(prob >= 0 && prob < 1);

// txy_scan_[0] = 1.0, txy_scan_[i>0] = txy_scan_[i-1] + txy[i-1].Weight (modified)
txy_scan_.resize(txy.size());
RealType wgt_t = 1.0;
Expand Down
4 changes: 2 additions & 2 deletions src/QMCHamiltonians/tests/test_NonLocalTOperator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ TEST_CASE("NonLocalTOperator", "[hamiltonian]")
REQUIRE(select5 != nullptr);
CHECK(select5->PID == 2);

RealType near_one = std::nextafter(RealType(1), RealType(0));
CHECK(near_one < RealType(1));
double near_one = std::nextafter(1.0, 0.0);
CHECK(near_one < 1.0);
auto select6 = t_op.selectMove(near_one, Txy);
REQUIRE(select6 != nullptr);
CHECK(select6->PID == 2);
Expand Down

0 comments on commit 4cfc794

Please sign in to comment.