Skip to content

Commit

Permalink
use almostEqual in tstats.nim (#20431)
Browse files Browse the repository at this point in the history
prefer math.almostEqual
  • Loading branch information
n5m committed Sep 26, 2022
1 parent cb24eea commit 6b4ea88
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions tests/stdlib/tstats.nim
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import std/math
import std/stats

proc `~=`(x, y: float): bool =
abs(x - y) < 10e-8
func `~=`(x, y: float32): bool =
math.almostEqual(x, y)

template main() =
var rs: RunningStat
Expand All @@ -28,21 +29,21 @@ template main() =
doAssert rs1.kurtosis ~= rs.kurtosis
rs1.clear()
rs1.push(@[1.0, 2.2, 1.4, 4.9])
doAssert(rs1.sum == 9.5)
doAssert(rs1.mean() == 2.375)
doAssert rs1.sum ~= 9.5
doAssert rs1.mean() ~= 2.375

when not defined(cpu32):
# XXX For some reason on 32bit CPUs these results differ
var rr: RunningRegress
rr.push(@[0.0, 1.0, 2.8, 3.0, 4.0], @[0.0, 1.0, 2.3, 3.0, 4.0])
doAssert(rr.slope() == 0.9695585996955861)
doAssert(rr.intercept() == -0.03424657534246611)
doAssert(rr.correlation() == 0.9905100362239381)
doAssert rr.slope() ~= 0.9695585996955861
doAssert rr.intercept() ~= -0.03424657534246611
doAssert rr.correlation() ~= 0.9905100362239381
var rr1, rr2: RunningRegress
rr1.push(@[0.0, 1.0], @[0.0, 1.0])
rr2.push(@[2.8, 3.0, 4.0], @[2.3, 3.0, 4.0])
let rr3 = rr1 + rr2
doAssert(rr3.correlation() == rr.correlation())
doAssert rr3.correlation() ~= rr.correlation()
doAssert rr3.slope() ~= rr.slope()
doAssert rr3.intercept() ~= rr.intercept()

Expand Down

0 comments on commit 6b4ea88

Please sign in to comment.