From fdc43d0574ca45cfc5a14818c4c9ffdc9349e18d Mon Sep 17 00:00:00 2001 From: John Benediktsson Date: Mon, 1 Apr 2013 17:04:07 -0700 Subject: [PATCH] math.extras: adding round-to-step. --- extra/math/extras/extras-tests.factor | 6 ++++++ extra/math/extras/extras.factor | 3 +++ 2 files changed, 9 insertions(+) diff --git a/extra/math/extras/extras-tests.factor b/extra/math/extras/extras-tests.factor index 9358bf19c1..cda7a9ee23 100644 --- a/extra/math/extras/extras-tests.factor +++ b/extra/math/extras/extras-tests.factor @@ -120,3 +120,9 @@ IN: math.extras.test 12345.6789012 12345.67890123 12345.678901235 } } [ 12345.67890123456 -6 9 [a,b] [ round-to-decimal ] with map ] unit-test + +{ 0 } [ 0 5 round-to-step ] unit-test +{ 0 } [ 1 5 round-to-step ] unit-test +{ 5 } [ 3 5 round-to-step ] unit-test +{ 10 } [ 12 5 round-to-step ] unit-test +{ 15 } [ 13 5 round-to-step ] unit-test diff --git a/extra/math/extras/extras.factor b/extra/math/extras/extras.factor index 37478e3d0e..d26bbe6cd3 100644 --- a/extra/math/extras/extras.factor +++ b/extra/math/extras/extras.factor @@ -255,3 +255,6 @@ M: float round-to-even : round-to-decimal ( x n -- y ) 10^ [ * 0.5 over 0 > [ + ] [ - ] if truncate ] [ / ] bi ; + +: round-to-step ( x step -- y ) + [ [ / round ] [ * ] bi ] unless-zero ;