truncate ( x -- y ) (original) (raw)

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

Prev: floor ( x -- y )
Next: round ( x -- y )

Vocabulary
math.functions

Inputs

x a real

Outputs

y a whole real number

Word description
Outputs the number that results from subtracting the fractional component of x.

Notes
The result is not necessarily an integer.

Definition

IN: math.functions

GENERIC: truncate ( x -- y )

Methods

USING: kernel math math.functions ;

M: float truncate
dup double>bits dup -52 shift 2047 bitand 1023 - dup 52 < [
nipd dup 0 <
[ 2drop 0.0 ] [
4503599627370495 swap neg shift bitnot bitand
bits>double
] if
] [ nip 1024 = [ dup + ] when ] if ; inline

USING: kernel math math.functions ;

M: real truncate dup 1 mod - ;