Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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