factor/extra/math/floating-point/floating-point.factor

41 lines
1.0 KiB
Factor
Raw Normal View History

! Copyright (C) 2008 Doug Coleman.
! See http://factorcode.org/license.txt for BSD license.
2008-11-11 14:05:48 -05:00
USING: kernel math sequences prettyprint math.parser io
math.functions ;
IN: math.floating-point
2008-11-11 14:05:48 -05:00
: (double-sign) ( bits -- n ) -63 shift ; inline
: double-sign ( double -- n ) double>bits (double-sign) ;
2008-11-11 14:05:48 -05:00
: (double-exponent-bits) ( bits -- n )
-52 shift 11 2^ 1- bitand ; inline
: double-exponent-bits ( double -- n )
2008-11-11 14:05:48 -05:00
double>bits (double-exponent-bits) ;
2008-11-11 14:05:48 -05:00
: (double-mantissa-bits) ( double -- n )
52 2^ 1- bitand ;
: double-mantissa-bits ( double -- n )
2008-11-11 14:05:48 -05:00
double>bits (double-mantissa-bits) ;
: >double ( S E M -- frac )
[ 52 shift ] dip
[ 63 shift ] 2dip bitor bitor bits>double ;
: >double< ( double -- S E M )
double>bits
[ (double-sign) ]
[ (double-exponent-bits) ]
[ (double-mantissa-bits) ] tri ;
: double. ( double -- )
double>bits
[ (double-sign) .b ]
[ (double-exponent-bits) >bin 11 CHAR: 0 pad-left bl print ]
[
(double-mantissa-bits) >bin 52 CHAR: 0 pad-left
11 [ bl ] times print
] tri ;