69 lines
1.7 KiB
Text
69 lines
1.7 KiB
Text
export {
|
||
import "int.lt";
|
||
import "euclidean.lt";
|
||
|
||
/* Implementation of Rational Numbers
|
||
*/
|
||
let ratio-sign = λ{
|
||
p: machine.Int64 ~ machine.Word;
|
||
q: machine.Int64 ~ machine.Word;
|
||
}
|
||
↦ bool-xor (int-sign p) (int-sign q);
|
||
|
||
let ratio-scale = λ{
|
||
{
|
||
p: machine.Int64~machine.Word;
|
||
q: machine.Int64~machine.Word;
|
||
} : ℚ ~ <Ratio machine.Int64~machine.Word>;
|
||
n : machine.Int64 ~machine.Word;
|
||
} ↦ {
|
||
i* q n;
|
||
i* p n;
|
||
};
|
||
|
||
let ratio-normalize = λ{
|
||
p: machine.Int64 ~ machine.Word;
|
||
q: machine.Int64 ~ machine.Word;
|
||
} : ℚ ~ <Ratio machine.Int64 ~ machine.Word>
|
||
↦ {
|
||
let s = gcd p q;
|
||
i/ q s;
|
||
i/ p s;
|
||
};
|
||
|
||
let ratio-add = λ{
|
||
{
|
||
ap:machine.Int64~machine.Word;
|
||
aq:machine.Int64~machine.Word;
|
||
}: ℚ ~ <Ratio machine.Int64 ~ machine.Word> ;
|
||
{
|
||
bp:machine.Int64~machine.Word;
|
||
bq:machine.Int64~machine.Word;
|
||
}: ℚ ~ <Ratio machine.Int64 ~ machine.Word> ;
|
||
} ↦ {
|
||
let l = lcm aq bq;
|
||
let a = i/ l aq;
|
||
let b = i/ l bq;
|
||
|
||
i* aq a;
|
||
i+ (i* ap a) (i* bp b);
|
||
};
|
||
|
||
let ratio-mul = λ{
|
||
{
|
||
ap:machine.Int64~machine.Word;
|
||
aq:machine.Int64~machine.Word;
|
||
}: ℚ ~ <Ratio machine.Int64 ~ machine.Word> ;
|
||
{
|
||
bp:machine.Int64~machine.Word;
|
||
bq:machine.Int64~machine.Word;
|
||
}: ℚ ~ <Ratio machine.Int64 ~ machine.Word> ;
|
||
} ↦ ratio-normalize (i* ap bp) (i* aq bq);
|
||
|
||
/*
|
||
let fmt-ratio = λ{ p:ℤ; q:ℤ; }: ℚ~<Ratio ℤ> ↦ {
|
||
fmt-int q;':';fmt-int p;
|
||
};
|
||
*/
|
||
}
|
||
|