math: rename >fraction to fraction>parts
parent
815591e10c
commit
0319ff7920
|
@ -12,7 +12,7 @@ IN: math.integers
|
||||||
|
|
||||||
M: integer numerator ; inline
|
M: integer numerator ; inline
|
||||||
M: integer denominator drop 1 ; inline
|
M: integer denominator drop 1 ; inline
|
||||||
M: integer >fraction 1 ; inline
|
M: integer fraction>parts 1 ; inline
|
||||||
|
|
||||||
M: fixnum >fixnum ; inline
|
M: fixnum >fixnum ; inline
|
||||||
M: fixnum >bignum fixnum>bignum ; inline
|
M: fixnum >bignum fixnum>bignum ; inline
|
||||||
|
|
|
@ -84,7 +84,7 @@ GENERIC: integer>fixnum-strict ( x -- y ) foldable
|
||||||
|
|
||||||
GENERIC: numerator ( a/b -- a )
|
GENERIC: numerator ( a/b -- a )
|
||||||
GENERIC: denominator ( a/b -- b )
|
GENERIC: denominator ( a/b -- b )
|
||||||
GENERIC: >fraction ( a/b -- a b )
|
GENERIC: fraction>parts ( a/b -- a b )
|
||||||
|
|
||||||
GENERIC: real-part ( z -- x )
|
GENERIC: real-part ( z -- x )
|
||||||
GENERIC: imaginary-part ( z -- y )
|
GENERIC: imaginary-part ( z -- y )
|
||||||
|
|
|
@ -480,7 +480,7 @@ M: integer >base
|
||||||
} cond ;
|
} cond ;
|
||||||
|
|
||||||
M: ratio >base
|
M: ratio >base
|
||||||
[ >fraction [ /mod ] keep ] [ [ >base ] curry tri@ ] bi*
|
[ fraction>parts [ /mod ] keep ] [ [ >base ] curry tri@ ] bi*
|
||||||
"/" glue over first-unsafe {
|
"/" glue over first-unsafe {
|
||||||
{ char: 0 [ nip ] }
|
{ char: 0 [ nip ] }
|
||||||
{ char: - [ append ] }
|
{ char: - [ append ] }
|
||||||
|
|
|
@ -17,7 +17,7 @@ $nl
|
||||||
{ $subsections
|
{ $subsections
|
||||||
numerator
|
numerator
|
||||||
denominator
|
denominator
|
||||||
>fraction
|
fraction>parts
|
||||||
}
|
}
|
||||||
{ $see-also "syntax-ratios" } ;
|
{ $see-also "syntax-ratios" } ;
|
||||||
|
|
||||||
|
@ -37,14 +37,14 @@ HELP: denominator
|
||||||
{ $values { "a/b" rational } { "b" "a positive integer" } }
|
{ $values { "a/b" rational } { "b" "a positive integer" } }
|
||||||
{ $description "Outputs the denominator of a rational number. Always outputs 1 with integers." } ;
|
{ $description "Outputs the denominator of a rational number. Always outputs 1 with integers." } ;
|
||||||
|
|
||||||
HELP: fraction>
|
HELP: parts>fraction
|
||||||
{ $values { "a" integer } { "b" "a positive integer" } { "a/b" rational } }
|
{ $values { "a" integer } { "b" "a positive integer" } { "a/b" rational } }
|
||||||
{ $description "Creates a new ratio, or outputs the numerator if the denominator is 1. This word does not reduce the fraction to lowest terms, and should not be called directly; use " { $link / } " instead." } ;
|
{ $description "Creates a new ratio, or outputs the numerator if the denominator is 1. This word does not reduce the fraction to lowest terms, and should not be called directly; use " { $link / } " instead." } ;
|
||||||
|
|
||||||
HELP: >fraction
|
HELP: fraction>parts
|
||||||
{ $values { "a/b" rational } { "a" integer } { "b" "a positive integer" } }
|
{ $values { "a/b" rational } { "a" integer } { "b" "a positive integer" } }
|
||||||
{ $description "Extracts the numerator and denominator of a rational number." } ;
|
{ $description "Extracts the numerator and denominator of a rational number." } ;
|
||||||
|
|
||||||
HELP: 2>fraction
|
HELP: 2fraction>parts
|
||||||
{ $values { "a/b" rational } { "c/d" rational } { "a" integer } { "c" integer } { "b" "a positive integer" } { "d" "a positive integer" } }
|
{ $values { "a/b" rational } { "c/d" rational } { "a" integer } { "c" integer } { "b" "a positive integer" } { "d" "a positive integer" } }
|
||||||
{ $description "Extracts the numerator and denominator of two rational numbers at once." } ;
|
{ $description "Extracts the numerator and denominator of two rational numbers at once." } ;
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
USING: kernel math math.functions math.parser tools.test ;
|
USING: kernel math math.functions math.parser tools.test ;
|
||||||
|
|
||||||
{ 1 2 } [ 1/2 >fraction ] unit-test
|
{ 1 2 } [ 1/2 fraction>parts ] unit-test
|
||||||
|
|
||||||
{ 1/2 } [ 1 >bignum 2 >bignum / ] unit-test
|
{ 1/2 } [ 1 >bignum 2 >bignum / ] unit-test
|
||||||
{ t } [ 10 3 / ratio? ] unit-test
|
{ t } [ 10 3 / ratio? ] unit-test
|
||||||
|
|
|
@ -3,22 +3,22 @@
|
||||||
USING: accessors kernel math ;
|
USING: accessors kernel math ;
|
||||||
IN: math.ratios
|
IN: math.ratios
|
||||||
|
|
||||||
: 2>fraction ( a/b c/d -- a c b d )
|
: 2fraction>parts ( a/b c/d -- a c b d )
|
||||||
[ >fraction ] bi@ swapd ; inline
|
[ fraction>parts ] bi@ swapd ; inline
|
||||||
|
|
||||||
<PRIVATE
|
<PRIVATE
|
||||||
|
|
||||||
: fraction> ( a b -- a/b )
|
: parts>fraction ( a b -- a/b )
|
||||||
dup 1 number= [ drop ] [ ratio boa ] if ; inline
|
dup 1 number= [ drop ] [ ratio boa ] if ; inline
|
||||||
|
|
||||||
: (scale) ( a b c d -- a*d b*c )
|
: (scale) ( a b c d -- a*d b*c )
|
||||||
[ * swap ] dip * swap ; inline
|
[ * swap ] dip * swap ; inline
|
||||||
|
|
||||||
: scale ( a/b c/d -- a*d b*c )
|
: scale ( a/b c/d -- a*d b*c )
|
||||||
2>fraction (scale) ; inline
|
2fraction>parts (scale) ; inline
|
||||||
|
|
||||||
: scale+d ( a/b c/d -- a*d b*c b*d )
|
: scale+d ( a/b c/d -- a*d b*c b*d )
|
||||||
2>fraction [ (scale) ] 2keep * ; inline
|
2fraction>parts [ (scale) ] 2keep * ; inline
|
||||||
|
|
||||||
PRIVATE>
|
PRIVATE>
|
||||||
|
|
||||||
|
@ -29,37 +29,37 @@ M: integer /
|
||||||
division-by-zero
|
division-by-zero
|
||||||
] [
|
] [
|
||||||
dup 0 < [ [ neg ] bi@ ] when
|
dup 0 < [ [ neg ] bi@ ] when
|
||||||
2dup simple-gcd [ /i ] curry bi@ fraction>
|
2dup simple-gcd [ /i ] curry bi@ parts>fraction
|
||||||
] if-zero ;
|
] if-zero ;
|
||||||
|
|
||||||
M: integer recip
|
M: integer recip
|
||||||
1 swap [
|
1 swap [
|
||||||
division-by-zero
|
division-by-zero
|
||||||
] [
|
] [
|
||||||
dup 0 < [ [ neg ] bi@ ] when fraction>
|
dup 0 < [ [ neg ] bi@ ] when parts>fraction
|
||||||
] if-zero ;
|
] if-zero ;
|
||||||
|
|
||||||
M: ratio recip
|
M: ratio recip
|
||||||
>fraction swap dup 0 < [ [ neg ] bi@ ] when fraction> ;
|
fraction>parts swap dup 0 < [ [ neg ] bi@ ] when parts>fraction ;
|
||||||
|
|
||||||
M: ratio hashcode*
|
M: ratio hashcode*
|
||||||
nip >fraction [ hashcode ] bi@ bitxor ;
|
nip fraction>parts [ hashcode ] bi@ bitxor ;
|
||||||
|
|
||||||
M: ratio equal?
|
M: ratio equal?
|
||||||
over ratio? [
|
over ratio? [
|
||||||
2>fraction = [ = ] [ 2drop f ] if
|
2fraction>parts = [ = ] [ 2drop f ] if
|
||||||
] [ 2drop f ] if ;
|
] [ 2drop f ] if ;
|
||||||
|
|
||||||
M: ratio number=
|
M: ratio number=
|
||||||
2>fraction number= [ number= ] [ 2drop f ] if ;
|
2fraction>parts number= [ number= ] [ 2drop f ] if ;
|
||||||
|
|
||||||
M: ratio >fixnum >fraction /i >fixnum ;
|
M: ratio >fixnum fraction>parts /i >fixnum ;
|
||||||
M: ratio >bignum >fraction /i >bignum ;
|
M: ratio >bignum fraction>parts /i >bignum ;
|
||||||
M: ratio >float >fraction /f ;
|
M: ratio >float fraction>parts /f ;
|
||||||
|
|
||||||
M: ratio numerator numerator>> ; inline
|
M: ratio numerator numerator>> ; inline
|
||||||
M: ratio denominator denominator>> ; inline
|
M: ratio denominator denominator>> ; inline
|
||||||
M: ratio >fraction [ numerator ] [ denominator ] bi ; inline
|
M: ratio fraction>parts [ numerator ] [ denominator ] bi ; inline
|
||||||
|
|
||||||
M: ratio < scale < ;
|
M: ratio < scale < ;
|
||||||
M: ratio <= scale <= ;
|
M: ratio <= scale <= ;
|
||||||
|
@ -68,11 +68,11 @@ M: ratio >= scale >= ;
|
||||||
|
|
||||||
M: ratio + scale+d [ + ] [ / ] bi* ;
|
M: ratio + scale+d [ + ] [ / ] bi* ;
|
||||||
M: ratio - scale+d [ - ] [ / ] bi* ;
|
M: ratio - scale+d [ - ] [ / ] bi* ;
|
||||||
M: ratio * 2>fraction [ * ] 2bi@ / ;
|
M: ratio * 2fraction>parts [ * ] 2bi@ / ;
|
||||||
M: ratio / scale / ;
|
M: ratio / scale / ;
|
||||||
M: ratio /i scale /i ;
|
M: ratio /i scale /i ;
|
||||||
M: ratio /f scale /f ;
|
M: ratio /f scale /f ;
|
||||||
M: ratio mod scale+d [ mod ] [ / ] bi* ;
|
M: ratio mod scale+d [ mod ] [ / ] bi* ;
|
||||||
M: ratio /mod scale+d [ /mod ] [ / ] bi* ;
|
M: ratio /mod scale+d [ /mod ] [ / ] bi* ;
|
||||||
M: ratio abs dup neg? [ >fraction [ neg ] dip fraction> ] when ;
|
M: ratio abs dup neg? [ fraction>parts [ neg ] dip parts>fraction ] when ;
|
||||||
M: ratio neg? numerator neg? ; inline
|
M: ratio neg? numerator neg? ; inline
|
||||||
|
|
Loading…
Reference in New Issue