! Copyright (C) 2003, 2009 Slava Pestov, Joe Groff. ! See http://factorcode.org/license.txt for BSD license. USING: kernel ; IN: math BUILTIN: fixnum ; BUILTIN: bignum ; BUILTIN: float ; PRIMITIVE: bits>double ( n -- x ) PRIMITIVE: bits>float ( n -- x ) PRIMITIVE: double>bits ( x -- n ) PRIMITIVE: float>bits ( x -- n ) ( x y -- ? ) PRIMITIVE: bignum>= ( x y -- ? ) PRIMITIVE: bignum>fixnum ( x -- y ) PRIMITIVE: bignum>fixnum-strict ( x -- y ) PRIMITIVE: both-fixnums? ( x y -- ? ) PRIMITIVE: fixnum* ( x y -- z ) PRIMITIVE: fixnum*fast ( x y -- z ) PRIMITIVE: fixnum+ ( x y -- z ) PRIMITIVE: fixnum+fast ( x y -- z ) PRIMITIVE: fixnum- ( x y -- z ) PRIMITIVE: fixnum-bitand ( x y -- z ) PRIMITIVE: fixnum-bitnot ( x -- y ) PRIMITIVE: fixnum-bitor ( x y -- z ) PRIMITIVE: fixnum-bitxor ( x y -- z ) PRIMITIVE: fixnum-fast ( x y -- z ) PRIMITIVE: fixnum-mod ( x y -- z ) PRIMITIVE: fixnum-shift ( x y -- z ) PRIMITIVE: fixnum-shift-fast ( x y -- z ) PRIMITIVE: fixnum/i ( x y -- z ) PRIMITIVE: fixnum/i-fast ( x y -- z ) PRIMITIVE: fixnum/mod ( x y -- z w ) PRIMITIVE: fixnum/mod-fast ( x y -- z w ) PRIMITIVE: fixnum< ( x y -- ? ) PRIMITIVE: fixnum<= ( x y -- z ) PRIMITIVE: fixnum> ( x y -- ? ) PRIMITIVE: fixnum>= ( x y -- ? ) PRIMITIVE: fixnum>bignum ( x -- y ) PRIMITIVE: fixnum>float ( x -- y ) PRIMITIVE: float* ( x y -- z ) PRIMITIVE: float+ ( x y -- z ) PRIMITIVE: float- ( x y -- z ) PRIMITIVE: float-u< ( x y -- ? ) PRIMITIVE: float-u<= ( x y -- ? ) PRIMITIVE: float-u> ( x y -- ? ) PRIMITIVE: float-u>= ( x y -- ? ) PRIMITIVE: float/f ( x y -- z ) PRIMITIVE: float< ( x y -- ? ) PRIMITIVE: float<= ( x y -- ? ) PRIMITIVE: float= ( x y -- ? ) PRIMITIVE: float> ( x y -- ? ) PRIMITIVE: float>= ( x y -- ? ) PRIMITIVE: float>bignum ( x -- y ) PRIMITIVE: float>fixnum ( x -- y ) PRIVATE> GENERIC: >fixnum ( x -- n ) foldable GENERIC: >bignum ( x -- n ) foldable GENERIC: >integer ( x -- n ) foldable GENERIC: >float ( x -- y ) foldable GENERIC: integer>fixnum ( x -- y ) foldable GENERIC: integer>fixnum-strict ( x -- y ) foldable GENERIC: numerator ( a/b -- a ) GENERIC: denominator ( a/b -- b ) GENERIC: real-part ( z -- x ) GENERIC: imaginary-part ( z -- y ) MATH: number= ( x y -- ? ) foldable M: object number= 2drop f ; MATH: < ( x y -- ? ) foldable MATH: <= ( x y -- ? ) foldable MATH: > ( x y -- ? ) foldable MATH: >= ( x y -- ? ) foldable MATH: unordered? ( x y -- ? ) foldable MATH: u< ( x y -- ? ) foldable MATH: u<= ( x y -- ? ) foldable MATH: u> ( x y -- ? ) foldable MATH: u>= ( x y -- ? ) foldable M: object unordered? 2drop f ; MATH: + ( x y -- z ) foldable MATH: - ( x y -- z ) foldable MATH: * ( x y -- z ) foldable MATH: / ( x y -- z ) foldable MATH: /f ( x y -- z ) foldable MATH: /i ( x y -- z ) foldable MATH: mod ( x y -- z ) foldable MATH: /mod ( x y -- z w ) foldable MATH: bitand ( x y -- z ) foldable MATH: bitor ( x y -- z ) foldable MATH: bitxor ( x y -- z ) foldable GENERIC# shift 1 ( x n -- y ) foldable GENERIC: bitnot ( x -- y ) foldable GENERIC# bit? 1 ( x n -- ? ) foldable GENERIC: abs ( x -- y ) foldable ERROR: log2-expects-positive x ; : log2 ( x -- n ) dup 0 <= [ log2-expects-positive ] [ (log2) ] if ; inline : zero? ( x -- ? ) 0 number= ; inline : 2/ ( x -- y ) -1 shift ; inline : sq ( x -- y ) dup * ; inline : neg ( x -- -x ) -1 * ; inline : sgn ( x -- n ) dup 0 < [ drop -1 ] [ 0 > 1 0 ? ] if ; inline : ?1+ ( x -- y ) [ 1 + ] [ 0 ] if* ; inline : rem ( x y -- z ) abs [ mod ] [ + ] [ mod ] tri ; foldable : 2^ ( n -- 2^n ) 1 swap shift ; inline : even? ( n -- ? ) 1 bitand zero? ; inline : odd? ( n -- ? ) 1 bitand 1 number= ; inline GENERIC: neg? ( x -- -x ) : if-zero ( ..a n quot1: ( ..a -- ..b ) quot2: ( ..a n -- ..b ) -- ..b ) [ dup zero? ] [ [ drop ] prepose ] [ ] tri* if ; inline : when-zero ( ..a n quot: ( ..a -- ..b ) -- ..b ) [ ] if-zero ; inline : unless-zero ( ..a n quot: ( ..a n -- ..b ) -- ..b ) [ ] swap if-zero ; inline UNION: integer fixnum bignum ; TUPLE: ratio { numerator integer read-only } { denominator integer read-only } ; UNION: rational integer ratio ; M: rational neg? 0 < ; inline UNION: real rational float ; TUPLE: complex { real real read-only } { imaginary real read-only } ; UNION: number real complex ; GENERIC: recip ( x -- y ) M: number recip 1 swap / ; inline : fp-bitwise= ( x y -- ? ) [ double>bits ] same? ; inline GENERIC: fp-special? ( x -- ? ) GENERIC: fp-nan? ( x -- ? ) GENERIC: fp-qnan? ( x -- ? ) GENERIC: fp-snan? ( x -- ? ) GENERIC: fp-infinity? ( x -- ? ) GENERIC: fp-nan-payload ( x -- bits ) GENERIC: fp-sign ( x -- ? ) M: object fp-special? drop f ; inline M: object fp-nan? drop f ; inline M: object fp-qnan? drop f ; inline M: object fp-snan? drop f ; inline M: object fp-infinity? drop f ; inline : ( payload -- nan ) 0x7ff0000000000000 bitor bits>double ; inline GENERIC: next-float ( m -- n ) GENERIC: prev-float ( m -- n ) : next-power-of-2 ( m -- n ) dup 2 <= [ drop 2 ] [ 1 - log2 1 + 2^ ] if ; inline : power-of-2? ( n -- ? ) dup 0 <= [ drop f ] [ dup 1 - bitand zero? ] if ; foldable : align ( m w -- n ) 1 - [ + ] keep bitnot bitand ; inline : (each-integer) ( ... i n quot: ( ... i -- ... ) -- ... ) [ iterate-step iterate-next (each-integer) ] [ 3drop ] if-iterate? ; inline recursive : (find-integer) ( ... i n quot: ( ... i -- ... ? ) -- ... i/f ) [ iterate-step iterate-rot [ 2drop ] [ iterate-next (find-integer) ] if ] [ 3drop f ] if-iterate? ; inline recursive : (all-integers?) ( ... i n quot: ( ... i -- ... ? ) -- ... ? ) [ iterate-step iterate-rot [ iterate-next (all-integers?) ] [ 3drop f ] if ] [ 3drop t ] if-iterate? ; inline recursive : each-integer ( ... n quot: ( ... i -- ... ) -- ... ) iterate-prep (each-integer) ; inline : times ( ... n quot: ( ... -- ... ) -- ... ) [ drop ] prepose each-integer ; inline : find-integer ( ... n quot: ( ... i -- ... ? ) -- ... i ) iterate-prep (find-integer) ; inline : all-integers? ( ... n quot: ( ... i -- ... ? ) -- ... ? ) iterate-prep (all-integers?) ; inline : find-last-integer ( ... n quot: ( ... i -- ... ? ) -- ... i ) over 0 < [ 2drop f ] [ [ call ] 2keep rot [ drop ] [ [ 1 - ] dip find-last-integer ] if ] if ; inline recursive