From 1938d04cf89fa01062e942681cfb3d2fc1d01777 Mon Sep 17 00:00:00 2001 From: Ryan Oldenburg Date: Fri, 10 Jun 2022 14:31:42 -0500 Subject: [PATCH] simpler quantize + test --- src/vmath.nim | 2 +- tests/test.nim | 14 +++++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/vmath.nim b/src/vmath.nim index 9d649b3..865c2fa 100644 --- a/src/vmath.nim +++ b/src/vmath.nim @@ -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. diff --git a/tests/test.nim b/tests/test.nim index 2d2c0bc..3de9563 100644 --- a/tests/test.nim +++ b/tests/test.nim @@ -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 @@ -604,7 +616,7 @@ block: var translation = translate(vec3(1, 2, 3)) translation.pos = vec3(3, 4, 5) - doAssert translation.pos == vec3(3, 4, 5) + doAssert translation.pos == vec3(3, 4, 5) block: # Test basic vector mat4 and quat.