-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fraction.js
341 lines (341 loc) · 11.5 KB
/
Fraction.js
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
class Fraction{
/**
* __creates a Fraction-Object__
* @param {number} a _(Real-Integer)_ "whole number" - _default `0` (can be negative)_
* @param {number} b _(Real-Integer)_ "numerator" - _default `0` (can be negative)_
* @param {number} c _(Real-Integer)_ "denominator" - _default `1` (can be negative)_
* @throws {Error} if denominator is `0`
* @description formula: `(a+(b/c))` (mixed-fraction)
*/
constructor(a=0,b=0,c=1){
/** @type {boolean} - `true` if this fraction is of negative value */
this.neg;
/** @type {number} - "whole number" _(Real-positive-Integer)_ */
this.a;
/** @type {number} - "numerator" _(Real-positive-Integer)_ */
this.b;
/** @type {number} - "denominator" _(Real-positive-Integer)_ */
this.c;
if(c==0){throw new Error("denominator can not be zero");}
if(
a<0&&b<0&&c<0||
a<0&&b>0&&c>0||
a>0&&b<0&&c>0||
a>0&&b>0&&c<0
){this.neg=true;}
else{this.neg=false;}
this.a=Math.abs(a);
this.b=Math.abs(b);
this.c=Math.abs(c);
}
/**
* __computes the greatest-common-dividor of two whole numbers__
* @param {number} A (Real-Integer) whole number
* @param {number} B (Real-Integer) whole number
* @returns {number} greatest-common-dividor (whole number)
*/
static gcd(A,B){
if(A<B){[A,B]=[B,A];}
for(;A%B>0;[A,B]=[B,A%B]);
return B;
}
/**
* __converts a decimal number to a improper-fraction (rough estimation)__
* @param {number} dec - (real float) - decimal number
* @param {number} max_den - (real positive integer) - max number for denominator - _default `0` (no limit)_
* @param {number} max_iter - (real positive integer) - max iteration count - _default `1e6`_
* @param {number} prec - (real positive float) - decimal value for precision error - _default `Number.EPSILON`_
* @returns {{t:number,b:number,c:number,n:number,l:string}}
* + t : sign (`1|-1`)
* + b : numerator (real positive integer)
* + c : denominator (real positive integer)
* + n : iteration count (real positive integer)
* + l : reason of exit (`'max_den'|'infinity'|'prec'|'max_iter'`)
*/
static dectofrac(dec,max_den=0,max_iter=1e6,prec=Number.EPSILON){
let sign=(dec<0?-1:1),
nint,ndec=Math.abs(dec),
num,pnum=1,ppnum=0,
den,pden=0,ppden=1,
iter=0;
do{
nint=Math.floor(ndec);
if(max_den>0&&(ppden+(nint*pden))>max_den){return{t:sign,b:num,c:den,n:iter,l:'max_den'};}
if(!Number.isFinite(ppnum+(nint*pnum))){return{t:sign,b:num,c:den,n:iter,l:'infinity'};}
num=ppnum+nint*pnum;
den=ppden+nint*pden;
// console.log(
// ">>%d\n%s\n%s",
// iter,
// `${num.toString().padEnd(10,' ')} ${ppnum} + ${nint} * ${pnum}`,
// `${den.toString().padEnd(10,' ')} ${ppden} + ${nint} * ${pden}`
// );
ppnum=pnum;
ppden=pden;
pnum=num;
pden=den;
ndec=1.0/(ndec-nint);
if(prec>Math.abs((sign*(num/den))-dec)){return{t:sign,b:num,c:den,n:iter,l:'prec'};}
}while(iter++<max_iter);
return {t:sign,b:num,c:den,n:iter,l:'max_iter'};
};
/**
* __converting a decimal number to a Fraction-Object__
* @param {string|number} v - _Real_ decimal number _(auto detects repeating digits) - (can be a string and of negative value) - default `0.0`_
* @param {number} md - _(Real Integer)_ maximum value for denominator _(makes it a rough estimation - `0` is no limit) - default `0`_
* @param {boolean} nl - if `true` doesn't loop -> literal `0.4` not `0.44444..4` - _default `false`_
* @param {boolean} fl - if `true` allways loop -> `0.123` => `0.123123..123` - _(only active if `nl` is `false`) - default `false`_
* @returns {Fraction} Fraction-Object
* @throws {Error} if parsing a string that can't be converted to a decimal number
*/
static fromdecimal(v=0.0,md=0,nl=false,fl=false){
if(typeof(v)=='string'){
v=Number.parseFloat(v);
if(v==NaN){throw new Error("parsed string is not a number");}
}
if(v==0.0){return new Fraction();}
let _az,f=new Fraction();
if(v<0){f.neg=true;}
[,f.a,_az]=[...Math.abs(v).toString().match(/(-?[0-9]+)(?:[,.]([0-9]+))?/)];
f.a=Number.parseInt(f.a);
if(!_az){_az='0';}
if(nl){
f.b=Number.parseInt(_az);
f.c=Math.pow(10,_az.length);
}else{
const r=/^((?<d>[0-9]+?)\k<d>*)$/;
const _a=_az.match(r)[1],
_d=_az.match(r)[2];
f.b=Number.parseInt(_d);
f.c=Math.pow(10,_d.length);
if(_a!=_d||_a.length==1||fl){f.c--;}
}
if(md>0){
if(f.toImproper().c>md){
let f2=Fraction.dectofrac(v,md);
{const _tmp=Math.floor(f2.b/f2.c);f2.b-=_tmp*f2.c;f2.t*=_tmp;}
f.neg=(f2.t<0);
f.a=Math.abs(f2.t);
f.b=f2.b;
f.c=f2.c;
}
}
return f.toMixed();
}
/**
* __converts this fraction to the smallest possible mixed-fraction-form__
* @returns {Fraction} current object (`this`)
*/
toMixed(){
if(this.b%this.c==0){
this.a+=this.b/this.c;
this.b=0;
this.c=1;
return this;
}
if(this.b>this.c){
this.a+=Math.floor(this.b/this.c);
this.b=this.b%this.c;
}
const _gcd=Fraction.gcd(this.c,this.b);
if(_gcd>1){
this.b/=_gcd;
this.c/=_gcd;
}
return this;
};
/**
* __converts this fraction to the smallest possible improper-fraction-form__
* @returns {Fraction} current object (`this`)
*/
toImproper(){
if(this.b%this.c==0){
this.b=this.a+(this.b/this.c);
this.a=0;
this.c=1;
return this;
}
this.b+=this.c*this.a;
this.a=0;
const _gcd=Fraction.gcd(this.c,this.b);
if(_gcd>1){
this.b/=_gcd;
this.c/=_gcd;
}
return this;
}
/**
* __computes the decimal value of this fraction and returns it__
* @returns {number} decimal value of the fraction
*/
todecimal(){
if(this.neg){return -(this.a+(this.b/this.c));}
else{return this.a+(this.b/this.c);}
}
/**
* __creates a formated string of this fraction with decimal value__
* @returns {string} - `+-(a+(b/c)) ~ +-1.234`
*/
tostr(){return `${this.neg?'-':'+'}(${this.a}+(${this.b}/${this.c})) ~ ${this.todecimal()}`;}
/**
* __adds a single integer to this fraction__
* @param {number} n - Real-Integer _(can be negative)_
* @returns {Fraction} Fraction-Object
* @description _for subtraction parse negative Real-Integer_
*/
add_n(n=0){
if(n==0){return this.toMixed();}
this.toMixed();
if(this.neg){
this.a=n-this.a;
if(this.a>=0){
this.neg=false;
this.b=this.c-this.b;
}
}else{
this.a+=n;
if(this.a<0){
this.neg=true;
this.b=this.c-this.b;
}
}
this.a=Math.abs(this.a);
return this.toMixed();
}
/**
* __multiplies a single integer with this fraction__
* @param {number} n - Real-Integer _(can be negative)_
* @returns {Fraction} Fraction-Object
*/
mul_n(n=1){
if(n==1){return this.toMixed();}
this.toImproper();
if(n<0){this.neg=!this.neg;}
this.b*=Math.abs(n);
return this.toMixed();
}
/**
* __raises this fraction to the nth power__
* @param {number} n - Real-Integer _(can be negative)_
* @returns {Fraction} Fraction-Object
*/
pow_n(n=1){
if(n===1){return this;}
if(n===0){
this.neg=false;
this.a=1;
this.b=0;
this.c=1;
return this;
}
if(n===-1){
this.toImproper();
[this.b,this.c]=[this.c,this.b];
return this.toMixed();
}
if(n%2===0){this.neg=false;}
if(n<-1){return this.pow_n(-1).pow_n(Math.abs(n));}
this.toImproper();
this.b**=n;
this.c**=n;
return this.toMixed();
}
/**
* __divides a single integer from this fraction__
* @param {number} n - Real-Integer _(can be negative)_
* @returns {Fraction} Fraction-Object
* @throws {Error} when trying to divide by `0`
*/
div_n(n=1){
if(n==0){throw new Error("can not divide by zero");}
if(n==1){return this.toMixed();}
if(n<0){this.neg=!this.neg;}
this.c*=Math.abs(n);
return this.toMixed();
}
/**
* __adds another fraction to this fraction__
* @param {Fraction} f - Fraction-Object
* @returns {Fraction} Fraction-Object
*/
add_f(f=null){
if(f==null){return this.toMixed();}
this.toMixed();
f.toMixed();
if(this.neg&&!f.neg){
this.a=Math.abs(f.a-this.a);
this.b=Math.abs((f.b*this.c)-(this.b*f.c));
}else if(!this.neg&&f.neg){
this.a=Math.abs(this.a-f.a);
this.b=Math.abs((this.b*f.c)-(f.b*this.c));
}else{
this.a+=f.a;
this.b=((this.b*f.c)+(f.b*this.c));
}
this.c*=f.c;
if(f.neg){this.neg=!this.neg;}
return this.toMixed();
}
/**
* __subtracts another fraction from this fraction__
* @param {Fraction} f - Fraction-Object
* @returns {Fraction} Fraction-Object
*/
sub_f(f=null){
if(f==null){return this.toMixed();}
this.toMixed();
f.toMixed();
this.a-=f.a;
this.b=(this.b*f.c-f.b*this.c);
this.c*=f.c;
if(f.neg){
this.toMixed();
this.neg=!this.neg;
this.b=this.c-this.b;
}
return this.toMixed();
}
/**
* __multiplies another fraction with this fraction__
* @param {Fraction} f - Fraction-Object
* @returns {Fraction} Fraction-Object
*/
mul_f(f=null){
if(f==null){return this.toMixed();}
this.toImproper();
f.toImproper();
this.b*=f.b;
this.c*=f.c;
if(f.neg){
this.toMixed();
this.neg=!this.neg;
this.b=this.c-this.b;
}
return this.toMixed();
}
/**
* __divides another fraction from this fraction__
* @param {Fraction} f - Fraction-Object
* @returns {Fraction} Fraction-Object
* @throws {Error} when trying to divide by `0`
*/
div_f(f=null){
if(f==null){return this.toMixed();}
this.toImproper();
f.toImproper();
this.b*=f.c;
this.c*=f.b;
if(f.neg){
this.toMixed();
this.neg=!this.neg;
this.b=this.c-this.b;
}
return this.toMixed();
}
};
// console.log(
// "%s\n%s\n%s",
// new Fraction(0,1,3).div_f(new Fraction(0,3,4)).tostr(),//=> 4/9 ~ 0.44444..4
// Fraction.fromdecimal(3.333).tostr(),//=> 3 1/3 ~ 3.33333..5
// Fraction.fromdecimal(Math.PI,1e9,true).tostr()//=> 3 11080585/78256779 ~ 3.141592653589793
// );