2024-05-18 18:01:41 +02:00
|
|
|
|
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;
|
2024-09-30 11:27:35 +02:00
|
|
|
|
let a = i/ l aq;
|
|
|
|
|
let b = i/ l bq;
|
2024-05-18 18:01:41 +02:00
|
|
|
|
|
2024-09-30 11:27:35 +02:00
|
|
|
|
i* aq a;
|
|
|
|
|
i+ (i* ap a) (i* bp b);
|
2024-05-18 18:01:41 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|