-
Notifications
You must be signed in to change notification settings - Fork 13
/
rational.ts
172 lines (149 loc) · 5.12 KB
/
rational.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import * as Maybe from './maybe';
export class ℚ {
private constructor(private rational: 'RATIONAL') {}
}
export function inj<A>(a: string): ℚ {
return (a as any) as ℚ;
}
export function prj<A>(fa: ℚ): string {
return (fa as any) as string;
}
export function parse(value: number | string): ℚ {
const [, fractional] = `${value}`.split('.');
const length = !!fractional ? fractional.length : 0;
const integer = parseInt(`${value}`.replace(/\.|\-|\+/gi, ''), 10);
const sign = `${value}`[0] === '-' ? '-' : '+';
return inj(`${sign}${integer}/${Math.pow(10, length)}`);
}
export function multiplyBy(bFraction: ℚ | number): (aFraction: ℚ) => ℚ {
const b = isNumber(bFraction) ? extract(parse(bFraction as number)) : extract(bFraction as ℚ);
return (aFraction: ℚ): ℚ => {
const a = extract(aFraction);
return toString(simplify({
sign: a.sign * b.sign,
numerator: a.numerator * b.numerator,
denominator: a.denominator * b.denominator
}));
};
}
export function divideBy(bFraction: ℚ | number): (aFraction: ℚ) => ℚ {
const b = isNumber(bFraction) ? extract(parse(bFraction as number)) : extract(bFraction as ℚ);
return (aFraction: ℚ): ℚ => {
const a = extract(aFraction);
return toString(simplify({
sign: a.sign * b.sign,
numerator: a.numerator * b.denominator,
denominator: a.denominator * b.numerator
}));
};
}
export function addTo(bFraction: ℚ | number): (aFraction: ℚ) => ℚ {
const b = isNumber(bFraction) ? extract(parse(bFraction as number)) : extract(bFraction as ℚ);
return (aFraction: ℚ): ℚ => {
const a = extract(aFraction);
const numerator = (a.sign * a.numerator * b.denominator) + (b.sign * b.numerator * a.denominator);
return toString(simplify({
sign: Math.sign(numerator),
numerator: Math.abs(numerator),
denominator: a.denominator * b.denominator
}));
};
}
export function subtractTo(bFraction: ℚ | number): (aFraction: ℚ) => ℚ {
const b = isNumber(bFraction) ? extract(parse(bFraction as number)) : extract(bFraction as ℚ);
return (aFraction: ℚ): ℚ => {
const a = extract(aFraction);
const numerator = (a.sign * a.numerator * b.denominator) + (-1 * b.sign * b.numerator * a.denominator);
return toString(simplify({
sign: Math.sign(numerator),
numerator: Math.abs(numerator),
denominator: a.denominator * b.denominator
}));
};
}
export function inverse(aFraction: ℚ): ℚ {
const a = extract(aFraction);
return toString({
sign: a.sign,
numerator: a.denominator,
denominator: a.numerator
});
}
export function round(precision: number) {
return (aFraction: ℚ): ℚ => {
const a = extract(aFraction);
return parse(rounder(a.numerator / a.denominator, precision));
};
}
export function isGreaterThan(aFraction: ℚ): (bFraction: ℚ) => Maybe.Maybe<ℚ> {
const a = extract(aFraction);
return (bFraction: ℚ): Maybe.Maybe<ℚ> => {
const b = extract(bFraction);
return a.sign * a.numerator * b.denominator > a.denominator * b.numerator * b.sign ?
Maybe.of(aFraction) : Maybe.Nothing;
};
}
export function isLesserThan(aFraction: ℚ): (bFraction: ℚ) => Maybe.Maybe<ℚ> {
const a = extract(aFraction);
return (bFraction: ℚ): Maybe.Maybe<ℚ> => {
const b = extract(bFraction);
return a.sign * a.numerator * b.denominator < a.denominator * b.numerator * b.sign ?
Maybe.of(aFraction) : Maybe.Nothing;
};
}
export function min(aFraction: ℚ): (bFraction: ℚ) => ℚ {
const a = extract(aFraction);
return (bFraction: ℚ): ℚ => {
const b = extract(bFraction);
return a.sign * a.numerator * b.denominator > b.sign * b.numerator * a.denominator ?
bFraction : aFraction;
}
}
export function abs(aFraction: ℚ): ℚ {
const a = extract(aFraction);
return toString({
sign: 1,
numerator: a.numerator,
denominator: a.denominator
});
}
export function toFloat(aFraction: ℚ): number {
return (new Function(`return ${aFraction}`) as any)();
}
function toString(fraction: IFraction): ℚ {
return inj(`${sign(fraction.sign)}${fraction.numerator}/${fraction.denominator}`);
}
function sign(num: number): string {
return num >= 0 ? '+' : '-';
}
function extract(fraction: ℚ): IFraction {
const sign = `${prj(fraction)[0]}1`;
const [numerator, denominator] = prj(fraction).slice(1).split('/');
return {
sign: parseInt(sign, 10),
numerator: parseInt(numerator, 10),
denominator: parseInt(denominator, 10),
};
}
function simplify(f: IFraction): IFraction {
const gcd = greatCommonDivisor(f.numerator, f.denominator);
return {
sign: f.sign,
numerator: f.numerator / gcd,
denominator: f.denominator / gcd
};
}
function greatCommonDivisor(a: number, b: number): number {
return b ? greatCommonDivisor(b, a % b) : a;
}
function rounder(value: number, decimals: number): number {
return Number(`${Math.round(<any>`${value}e${decimals}`)}e-${decimals}`);
}
interface IFraction {
sign: number
numerator: number
denominator: number
}
function isNumber(value: any): boolean {
return ({}).toString.call(value) === '[object Number]';
}