From: Joe Wreschnig Date: Mon, 19 May 2014 08:12:44 +0000 (+0200) Subject: Lerp precision. X-Git-Tag: 1.0.0~10 X-Git-Url: https://git.yukkurigames.com/?a=commitdiff_plain;h=5bc5aa65b769e551177545fda77b31ea6c66467e;hp=d9344ccd68dbe21eb5af6d0575b5b84b9f8513aa;p=string-lerp.git Lerp precision. --- diff --git a/string-lerp.js b/string-lerp.js index 46a55bf..6c240e4 100644 --- a/string-lerp.js +++ b/string-lerp.js @@ -193,9 +193,10 @@ var part = nlerp(+sourcePart, +targetPart, amount); var sourcePoint = sourcePart.indexOf("."); var targetPoint = targetPart.indexOf("."); - var point = Math.max( + var point = Math.round(nlerp( sourcePoint >= 0 ? (sourcePart.length - 1) - sourcePoint : 0, - targetPoint >= 0 ? (targetPart.length - 1) - targetPoint : 0); + targetPoint >= 0 ? (targetPart.length - 1) - targetPoint : 0, + amount)); targetParts[i] = part.toFixed(point); } return targetParts.join(""); diff --git a/tests/string-lerp.js b/tests/string-lerp.js index 2eccba6..f2fb88d 100644 --- a/tests/string-lerp.js +++ b/tests/string-lerp.js @@ -152,14 +152,13 @@ JS.Test.describe('numeric lerp', function () { with (this) { it("rounds integers", function () { with (this) { assertEqual("12", lerp("0", "100", 0.123)); - assertEqual("12.3", lerp("0", "100.0", 0.123)); + assertEqual("12.3", lerp("0.0", "100.0", 0.123)); assertEqual("12.3", lerp("0.0", "100", 0.123)); assertEqual("12.3", lerp("0.0", "100.0", 0.123)); }}); it("thinks about precision", function () { with (this) { - assertEqual("12.30", lerp("0", "100.00", 0.123)); - assertEqual("12.30", lerp("0.00", "100", 0.123)); + assertEqual("12.30", lerp("0.00", "100.00", 0.123)); assertEqual("12.300", lerp("0.000", "100.000", 0.123)); }});