math.functions: speed up round (thanks Loryn Jenkins!)

db4
John Benediktsson 2013-03-26 14:21:55 -07:00
parent 891f6fef36
commit 4c941e8996
2 changed files with 13 additions and 1 deletions

View File

@ -203,6 +203,11 @@ CONSTANT: log10-factorial-1000 0x1.40f3593ed6f8ep11
[ -4.0 ] [ -4.0 floor ] unit-test
[ -4.0 ] [ -4.0 ceiling ] unit-test
[ -5 ] [ -9/2 round ] unit-test
[ -4 ] [ -22/5 round ] unit-test
[ 5 ] [ 9/2 round ] unit-test
[ 4 ] [ 22/5 round ] unit-test
[ -5.0 ] [ -4.5 round ] unit-test
[ -4.0 ] [ -4.4 round ] unit-test
[ 5.0 ] [ 4.5 round ] unit-test

View File

@ -362,7 +362,14 @@ M: real atan >float atan ; inline
: truncate ( x -- y ) dup 1 mod - ; inline
: round ( x -- y ) dup sgn 2 / + truncate ; inline
GENERIC: round ( x -- y )
M: integer round ; inline
M: ratio round
>fraction [ /mod abs 2 * ] keep >= [ dup 0 < -1 1 ? + ] when ;
M: float round dup sgn 2 /f + truncate ;
: floor ( x -- y )
dup 1 mod