Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
wilkinszhang committed Nov 27, 2024
1 parent f6bd226 commit 73a7d36
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion bloom.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,27 @@ func (f *BloomFilter) ApproximatedSize() uint32 {
x := float64(f.b.Count())
m := float64(f.Cap())
k := float64(f.K())
size := -1 * m / k * math.Log(1-x/m) / math.Log(math.E)

if x == 0 {
return 0 // If no bit is set, returns 0
}

ratio := x / m
if ratio >= 1.0 {
return math.MaxUint32 // If all bits are set, returns the maximum value
}

// To prevent floating point precision issues, ensure ratio does not exceed 1
if ratio > 1.0 {
ratio = 1.0
}

size := -m / k * math.Log(1-ratio)

// Check for overflow
if size > float64(math.MaxUint32) {
return math.MaxUint32
}
return uint32(math.Floor(size + 0.5)) // round
}

Expand Down

0 comments on commit 73a7d36

Please sign in to comment.