Merge branch 'master' of github.com:treeform/vmath

This commit is contained in:
treeform 2022-06-20 13:10:05 -07:00
commit 5d7c5e4115
2 changed files with 14 additions and 2 deletions

View file

@ -404,7 +404,7 @@ proc sign*[T](v: T): T =
proc quantize*[T: SomeFloat](v, n: T): T =
## Makes v be multiple of n. Rounding to integer quantize by 1.0.
sign(v) * trunc(abs(v) / n) * n
trunc(v / n) * n
proc fractional*[T: SomeFloat](v: T): T =
## Returns fractional part of a number.

View file

@ -37,9 +37,21 @@ block:
doAssert sign(0.0) == 1.0
doAssert sign(1.0) == 1.0
block:
proc quantize2(v, n: float32): float32 =
## Makes v be multiple of n. Rounding to integer quantize by 1.0.
sign(v) * trunc(abs(v) / n) * n
let n = 1.float32 / 10
for _ in 0 ..< 10_000:
let f = rand(-100.float32 .. 100.float32)
doAssert quantize(f, n) == quantize2(f, n)
doAssert quantize(1.23456789, 1.0) ~= 1
doAssert quantize(1.23456789, 0.1) ~= 1.2
doAssert quantize(1.23456789, 0.01) ~= 1.23
doAssert quantize(-1.23456789, 0.01) ~= -1.23
doAssert fractional(0.0) ~= 0.0
doAssert fractional(3.14) ~= 0.14