Skip to content

fix: handle saturated bitset in ApproximatedSize#113

Open
Yanhu007 wants to merge 1 commit intobits-and-blooms:masterfrom
Yanhu007:fix/approximated-size-saturated
Open

fix: handle saturated bitset in ApproximatedSize#113
Yanhu007 wants to merge 1 commit intobits-and-blooms:masterfrom
Yanhu007:fix/approximated-size-saturated

Conversation

@Yanhu007
Copy link
Copy Markdown

Fixes #104

Problem

ApproximatedSize() produces +Inf when the bitset is fully saturated (x == m), because log(1 - x/m) = log(0) = -Inf. Converting +Inf to uint32 is undefined behavior in Go:

  • amd64: returns 0
  • arm64: returns 4294967295 (MaxUint32)

Fix

Guard against saturation and overflow:

  1. Return MaxUint32 when all bits are set (x >= m)
  2. Return MaxUint32 when the computed size is +Inf, NaN, or exceeds MaxUint32

This provides consistent cross-platform behavior.

All existing tests pass.

When all bits are set (x == m), the formula produces +Inf because
log(1-x/m) = log(0) = -Inf. Converting +Inf to uint32 is undefined
behavior in Go: it returns 0 on amd64 and MaxUint32 on arm64.

Return math.MaxUint32 when the bitset is saturated or when the
computed size overflows uint32, providing consistent cross-platform
behavior.

Fixes bits-and-blooms#104
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Approximated Size returns 0 for a saturated bitset on amd64

1 participant