diff --git a/basis/math/functions/functions-tests.factor b/basis/math/functions/functions-tests.factor index e6d79a8428..23568897ff 100644 --- a/basis/math/functions/functions-tests.factor +++ b/basis/math/functions/functions-tests.factor @@ -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 diff --git a/basis/math/functions/functions.factor b/basis/math/functions/functions.factor index 340bbff202..72812764b2 100644 --- a/basis/math/functions/functions.factor +++ b/basis/math/functions/functions.factor @@ -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