math.extras: adding round-away-from-zero.
parent
d3df35102a
commit
cfb44999a1
|
@ -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
|
||||
|
|
|
@ -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 '[
|
||||
|
|
Loading…
Reference in New Issue