frexp ( x -- y exp ) (original) (raw)

frexp ( x -- y exp )

Vocabulary
math.functions

Inputs

x a number

Outputs

y a float
exp an integer

Word description
Break the number x into a normalized fraction y and an integral power of 2 e^.

The function returns a number y in the interval [1/2, 1) or 0, and a number exp such that x = y*(2**exp).

Definition

IN: math.functions

GENERIC: frexp ( x -- y exp )

Methods

USING: kernel math math.functions ;

M: float frexp
dup fp-special? [ dup zero? ] unless*
[ 0 ] [
double>bits [
9227875636482146303 bitand 0.5 double>bits bitor
bits>double
] [ -52 shift 2047 bitand 1022 - ] bi
] if ; inline

USING: kernel math math.functions ;

M: integer frexp
[ 0.0 0 ] [
dup 0 > [ 1 ] [ abs -1 ] if swap dup log2 [
52 swap - shift 4503599627370495 bitand
0.5 double>bits bitor bits>double
] [ 1 + ] bi [ * ] dip
] if-zero ; inline