math.extras: adding round-away-from-zero.

db4
John Benediktsson 2013-06-17 10:10:40 -07:00
parent d3df35102a
commit cfb44999a1
2 changed files with 13 additions and 0 deletions

View File

@ -130,5 +130,11 @@ IN: math.extras.test
{ 10 } [ 12 5 round-to-step ] unit-test
{ 15 } [ 13 5 round-to-step ] unit-test
{ 0 } [ 0 round-away-from-zero ] unit-test
{ -1.0 } [ -0.1 round-away-from-zero ] unit-test
{ 1.0 } [ 0.1 round-away-from-zero ] unit-test
{ -2.0 } [ -1.9 round-away-from-zero ] unit-test
{ 2.0 } [ 1.9 round-away-from-zero ] unit-test
{ { 0 1 2 3 0 0 1 } } [ { 1 2 3 3 2 1 2 } [ <= ] monotonic-count ] unit-test
{ 4 } [ { 1 2 3 1 2 3 4 5 } [ < ] max-monotonic-count ] unit-test

View File

@ -265,6 +265,13 @@ M: float round-to-even
: round-to-step ( x step -- y )
[ [ / round ] [ * ] bi ] unless-zero ;
GENERIC: round-away-from-zero ( x -- y )
M: integer round-away-from-zero ; inline
M: real round-away-from-zero
dup 0 < [ floor ] [ ceiling ] if ;
: monotonic-count ( seq quot: ( elt1 elt2 -- ? ) -- newseq )
over empty? [ 2drop { } ] [
[ 0 swap unclip-slice swap ] dip '[