lt-core/lt-stdlib/ratio.lt

45 lines
1.1 KiB
Text
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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 as = i/ l aq;
let bs = i/ l bq;
i* aq as;
i+ (i* ap as) (i* bp bs);
};
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;
};
}