2006-01-11 18:26:12 -05:00
|
|
|
! Copyright (C) 2003, 2006 Slava Pestov.
|
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2004-07-16 02:26:21 -04:00
|
|
|
IN: math
|
2005-04-21 00:49:19 -04:00
|
|
|
USING: errors generic kernel math-internals ;
|
2004-07-16 02:26:21 -04:00
|
|
|
|
2005-08-22 15:33:18 -04:00
|
|
|
G: number= ( x y -- ? ) math-combination ; foldable
|
2004-12-18 23:35:20 -05:00
|
|
|
M: object number= 2drop f ;
|
|
|
|
|
2005-08-22 15:33:18 -04:00
|
|
|
G: < ( x y -- ? ) math-combination ; foldable
|
|
|
|
G: <= ( x y -- ? ) math-combination ; foldable
|
|
|
|
G: > ( x y -- ? ) math-combination ; foldable
|
|
|
|
G: >= ( x y -- ? ) math-combination ; foldable
|
|
|
|
|
|
|
|
G: + ( x y -- x+y ) math-combination ; foldable
|
|
|
|
G: - ( x y -- x-y ) math-combination ; foldable
|
|
|
|
G: * ( x y -- x*y ) math-combination ; foldable
|
|
|
|
G: / ( x y -- x/y ) math-combination ; foldable
|
|
|
|
G: /i ( x y -- x/y ) math-combination ; foldable
|
|
|
|
G: /f ( x y -- x/y ) math-combination ; foldable
|
|
|
|
G: mod ( x y -- x%y ) math-combination ; foldable
|
|
|
|
|
|
|
|
G: /mod ( x y -- x/y x%y ) math-combination ; foldable
|
|
|
|
|
|
|
|
G: bitand ( x y -- z ) math-combination ; foldable
|
|
|
|
G: bitor ( x y -- z ) math-combination ; foldable
|
|
|
|
G: bitxor ( x y -- z ) math-combination ; foldable
|
|
|
|
G: shift ( x n -- y ) math-combination ; foldable
|
2004-12-18 23:18:32 -05:00
|
|
|
|
2006-01-11 18:26:12 -05:00
|
|
|
GENERIC: bitnot ( n -- n ) foldable
|
|
|
|
|
2005-09-16 22:47:28 -04:00
|
|
|
GENERIC: 1+ ( x -- x+1 ) foldable
|
|
|
|
GENERIC: 1- ( x -- x-1 ) foldable
|
2006-01-26 23:01:14 -05:00
|
|
|
GENERIC: abs ( z -- |z| ) foldable
|
|
|
|
GENERIC: absq ( n -- |n|^2 ) foldable
|
2004-12-18 23:18:32 -05:00
|
|
|
|
2006-01-28 15:49:31 -05:00
|
|
|
GENERIC: zero? ( x -- ? ) foldable
|
|
|
|
M: object zero? drop f ;
|
|
|
|
|
2005-05-15 21:17:56 -04:00
|
|
|
: sq dup * ; inline
|
|
|
|
: neg 0 swap - ; inline
|
|
|
|
: recip 1 swap / ; inline
|
2006-01-26 23:01:14 -05:00
|
|
|
: max ( x y -- z ) [ > ] 2keep ? ; foldable
|
|
|
|
: min ( x y -- z ) [ < ] 2keep ? ; foldable
|
|
|
|
: between? ( x min max -- ? ) pick >= >r >= r> and ; foldable
|
|
|
|
: rem ( x y -- z ) tuck mod over + swap mod ; foldable
|
2006-01-12 00:34:56 -05:00
|
|
|
: sgn ( m -- n ) dup 0 < -1 0 ? swap 0 > 1 0 ? bitor ; foldable
|
|
|
|
: align ( m w -- n ) 1- [ + ] keep bitnot bitand ; inline
|
2006-01-26 23:01:14 -05:00
|
|
|
: truncate ( x -- y ) dup 1 mod - ; foldable
|
|
|
|
|
|
|
|
: floor ( x -- y )
|
|
|
|
dup 1 mod dup 0 =
|
|
|
|
[ drop ] [ dup 0 < [ - 1- ] [ - ] if ] if ; foldable
|
|
|
|
|
|
|
|
: ceiling ( x -- y ) neg floor neg ; foldable
|
2005-01-23 16:47:28 -05:00
|
|
|
|
2006-04-27 00:44:50 -04:00
|
|
|
: (repeat) ( i n quot -- )
|
|
|
|
pick pick >= [
|
2006-04-26 03:05:38 -04:00
|
|
|
3drop
|
|
|
|
] [
|
2006-04-27 00:44:50 -04:00
|
|
|
[ swap >r call 1+ r> ] keep (repeat)
|
2006-04-26 03:05:38 -04:00
|
|
|
] if ; inline
|
|
|
|
|
2006-04-27 00:44:50 -04:00
|
|
|
: repeat 0 -rot (repeat) ; inline
|
2005-01-23 16:47:28 -05:00
|
|
|
|
2005-06-29 19:40:44 -04:00
|
|
|
: times ( n quot -- | quot: -- )
|
2005-01-23 16:47:28 -05:00
|
|
|
swap [ >r dup slip r> ] repeat drop ; inline
|
2005-05-05 15:31:57 -04:00
|
|
|
|
2006-01-12 00:34:56 -05:00
|
|
|
GENERIC: number>string ( n -- str ) foldable
|