ratio
Factor documentation > Factor handbook > The language > Numbers > Rational numbers
Next:numerator ( a/b -- a )


Vocabulary
math

Class description
The class of rational numbers with denominator not equal to 1.

Definition
IN: math

TUPLE: ratio
{ numerator integer read-only initial: 0 }
{ denominator integer read-only initial: 0 } ;


Methods
USING: kernel math math.ratios ;

M: ratio * 2>fraction [ * ] 2bi@ / ;


USING: kernel math math.ratios.private ;

M: ratio + [ scale + ] [ ratio+d ] 2bi / ;


USING: kernel math math.ratios.private ;

M: ratio - [ scale - ] [ ratio+d ] 2bi / ;


USING: math math.ratios.private ;

M: ratio / scale / ;


USING: math math.ratios.private ;

M: ratio /f scale /f ;


USING: math math.ratios.private ;

M: ratio /i scale /i ;


USING: kernel math ;

M: ratio /mod [ /i ] 2keep mod ;


USING: math math.ratios.private ;

M: ratio < scale < ;


USING: math math.ratios.private ;

M: ratio <= scale <= ;


USING: math math.ratios.private ;

M: ratio > scale > ;


USING: math math.ratios.private ;

M: ratio >= scale >= ;


USING: kernel math math.parser math.parser.private namespaces
sequences ;

M: ratio >base
[
dup 0 < negative? set abs 1 /mod
[ [ "" ] [ (>base) sign append ] if-zero ] [
[ numerator (>base) ] [ denominator (>base) ] bi
"/" glue
] bi* append negative? get [ 45 prefix ] when
] with-radix ;


USING: math math.functions ;

M: ratio >bignum >fraction /i >bignum ;


USING: math math.functions ;

M: ratio >fixnum >fraction /i >fixnum ;


USING: math math.functions ;

M: ratio >float >fraction /f ;


USING: kernel math math.functions math.functions.private ;

M: ratio ^n [ >fraction ] dip [ ^n ] curry bi@ / ;


USING: accessors math ;

M: ratio denominator denominator>> ; inline


USING: compiler.tree.propagation.info kernel math ;

M: ratio eql? over ratio? [ = ] [ 2drop f ] if ;


USING: kernel math math.ratios ;

M: ratio equal?
over ratio?
[ 2>fraction = [ = ] [ 2drop f ] if ] [ 2drop f ] if ;


USING: kernel math math.functions ;

M: ratio hashcode* nip >fraction [ hashcode ] bi@ bitxor ;


USING: kernel math ;

M: ratio mod 2dup /i * - ;


USING: kernel math math.ratios ;

M: ratio number=
2>fraction number= [ number= ] [ 2drop f ] if ;


USING: accessors math ;

M: ratio numerator numerator>> ; inline