round ( x -- y ) (original) (raw)

round ( x -- y )
Factor handbook » The language » Numbers » Mathematical functions » Arithmetic functions

Prev: truncate ( x -- y )
Next: round-to-decimal ( x n -- y )

Vocabulary
math.functions

Inputs

x a real

Outputs

y a whole real number

Word description
Outputs the whole number closest to x, rounding out at half.

Notes
The result is not necessarily an integer.

Examples

USING: math.functions prettyprint ; 4.5 round .
5.0

USING: math.functions prettyprint ; 4.4 round .
4.0

Definition

IN: math.functions

GENERIC: round ( x -- y )

Methods

USING: kernel math math.functions ;

M: float round dup sgn 2 /f + truncate ;

USING: math math.functions ;

M: integer round ; inline

USING: math math.functions ;

M: ratio round [ >= ] (ratio-round) ;