lt-core/lt-stdlib/ratio.lt
2024-12-24 12:51:36 +01:00

69 lines
1.7 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 {
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;
};
*/
}