45 lines
1.1 KiB
Text
45 lines
1.1 KiB
Text
export {
|
||
/* Implementation of Rational Numbers
|
||
*/
|
||
|
||
let ratio-scale = λ{
|
||
{p:ℕ; q:ℕ;} : ℚ ~ <Ratio ℕ~machine.UInt64> ;
|
||
n : ℕ ~ machine.UInt64 ;
|
||
} ↦ {
|
||
i* q n;
|
||
i* p n;
|
||
};
|
||
|
||
let ratio-normalize = λ{
|
||
p: ℤ~machine.Int64;
|
||
q: ℤ~machine.Int64;
|
||
} : ℚ ~ <Ratio ℤ~machine.Int64>
|
||
↦ {
|
||
let s = gcd p q;
|
||
i/ q s;
|
||
i/ p s;
|
||
};
|
||
|
||
let ratio-add = λ{
|
||
{ap:ℕ; aq:ℕ;}: ℚ ~ <Ratio ℕ ~ ℤ_2^64 ~ machine.UInt64> ;
|
||
{bp:ℕ; bq:ℕ;}: ℚ ~ <Ratio ℕ ~ ℤ_2^64 ~ machine.UInt64> ;
|
||
} ↦ {
|
||
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:ℤ; aq:ℤ;}: ℚ ~ <Ratio ℤ ~ ℤ_2^64 ~ machine.Int64> ;
|
||
{bp:ℤ; bq:ℤ;}: ℚ ~ <Ratio ℤ ~ ℤ_2^64 ~ machine.Int64> ;
|
||
} ↦ ratio-normalize (i* ap bp) (i* aq bq);
|
||
|
||
|
||
let fmt-ratio = λ{ p:ℤ; q:ℤ; }: ℚ~<Ratio ℤ> ↦ {
|
||
fmt-int q;':';fmt-int p;
|
||
};
|
||
}
|
||
|