Skip to content

Commit

Permalink
fix: overflow when calculating stdDev
Browse files Browse the repository at this point in the history
Signed-off-by: Jone Zhang <[email protected]>
  • Loading branch information
zhangzhongjun authored and zhangzhongjun committed Nov 22, 2024
1 parent 5a5f1d7 commit a9e538c
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ type Pinger struct {
maxRtt time.Duration
avgRtt time.Duration
stdDevRtt time.Duration
stddevm2 time.Duration
stddevm2 float64
statsMu sync.RWMutex

// If true, keep a record of rtts of all received packets.
Expand Down Expand Up @@ -340,9 +340,9 @@ func (p *Pinger) updateStatistics(pkt *Packet) {
delta := pkt.Rtt - p.avgRtt
p.avgRtt += delta / pktCount
delta2 := pkt.Rtt - p.avgRtt
p.stddevm2 += delta * delta2
p.stddevm2 += float64(delta) * float64(delta2)

p.stdDevRtt = time.Duration(math.Sqrt(float64(p.stddevm2 / pktCount)))
p.stdDevRtt = time.Duration(math.Sqrt(p.stddevm2 / float64(pktCount)))
}

// SetIPAddr sets the ip address of the target host.
Expand Down

0 comments on commit a9e538c

Please sign in to comment.