-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFL64.js
1672 lines (1093 loc) · 53.8 KB
/
FL64.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//**********************************************************************************
//Simple function for if value is an number.
//**********************************************************************************
Object.prototype.isNum = function () { if (isNaN(this)) { throw new TypeError("Expected Number;"); } }
//**********************************************************************************
//Construct the new fraction data type.
//**********************************************************************************
var Fract = function (X, Y)
{
var n = new Number( X / Y ); for( k in this ){ n[k] = this[k]; }
n.reduce = n.getFract;
n.x = X; n.y = Y;
if (n.x > 3.99168061906944e+292 || n.y > 3.99168061906944e+292)
{
n.x /= 1.7726622920963562e+277; n.y /= 1.7726622920963562e+277;
}
return (n);
};
//**********************************************************************************
//Teach compiler how to display fraction data type, or to combine it as code.
//**********************************************************************************
var Oporators = [
"*", "*=", "/", "/=", "+", "+=", "-", "-=", "%", "%=", "**", "**=",
"&", "&=", "|", "|=", "~", "~", "^", "^=", "<<", "<<=", ">>", ">>=", ">>>", ">>>=",
"<", "<=", ">", ">=", "==", "===", "!=", "!==", "&&", "&&=", "||", "||=", "!", "!=", "", "="
];
Fract.prototype.toString = function (op, s)
{
//Check if operator is valid.
if (op != undefined && (op = Oporators.indexOf(op)) < 0) { throw (new Error("Operator is not supported.")); }
//Is the value of the fraction negative or positive. Note if "s" is true force absolute value.
var sing = (this.x ^ this.y) < 0 && !s;
//absolute value numerator, denominator, remainder, and sum.
var out = "", numerator = Math.floor(Math.abs(Math.round(this.x)), denominator = Math.abs(Math.round(this.y)));
var sum = Math.floor(numerator / denominator);
//subtract sum amount leaving the remainder of the fractional part.
numerator = numerator - (denominator * sum);
//If has fractional part.
if (numerator !== 0 && !(op > 11 && op < 26))
{
if (sing) { out += "-"; sing = ~sing; }
//Multiply, and Divide flip if it is 1 divided by.
if ((numerator === 1 && sum === 0) && (op < 4)) { numerator = 0; op += op < 2 ? 2 : -2; }
//Else Regular fraction amount.
else { out += numerator + (op != undefined ? " / " : "\xF7"); }
//Divided by.
out += denominator;
}
//Sum amount.
if (sum !== 0) { out += !sing ? (out == "" ? "" : " + ") : " - "; out += sum; }
//If fraction and sum is combined with operation must be put in parenthesis.
if (op != undefined && numerator !== 0 && sum !== 0 && ((op & 1) == 0 && op < 12)) { out = "( " + out + " )"; }
//Return value.
if ((out === "1" || out === "") && (op < 4)) { return (""); } //Divide multiply by one is no operation.
if (Oporators[op]) { out = Oporators[op] + " " + out; } else { out = out.replace(/ /g, ""); }
return (out !== "" ? out : "0");
};
//**********************************************************************************
//Teach the compiler how to read the value of an fraction.
//**********************************************************************************
Fract.prototype.valueOf = function() { return( this.x / this.y ); }
Fract.prototype.reValue = function () { return ((isNaN(this.r[this.length]) ? this : this.r[this.length].valueOf().getFract()).toString()); };
//**********************************************************************************
//factorial number.
//**********************************************************************************
FNumber = function( v )
{
var n = new Number( v ); for( k in this ){ n[k] = this[k]; }
n.abLim = n.reFact = true;
n.r = [Math.abs( v )];
n.val = [0]; n.sing = v < 0 ? -1 : 1;
return (n);
};
Number.prototype.sing = 1;
FNumber.prototype.valueOf = function () { return ( this.r[this.length] * this.sing ); };
FNumber.prototype.reValue = function () { return ( this.r[this.length] ); };
FNumber.prototype.toString = function( pos )
{
var o = "", e = this.length, pos = pos ? Math.max(0, pos) : Math.max( 0, e - 20 );
pos = Math.min( pos, e - 1 ); e = Math.min( pos + 20, e );
for( var i = pos; i < e; i++ )
{
o += "X" + i + " = " + this.a[i] + " / " + this.b[i] + "\r\n";
}
return( o );
};
//**********************************************************************************
//Transcendental number.
//**********************************************************************************
function TNumber( v )
{
var n = new Number(v); for( k in this ){ n[k] = this[k]; }
n.abLim = n.reFact = true;
if( v )
{
n.r = [v.primitive()]; n.val = [0];
n.sing = v < 0 ? -1 : 1;
return(n);
}
n.r = [NaN]; n.val = [0];
return( n );
};
TNumber.prototype.valueOf = function() { return( this.r[this.length] ); };
TNumber.prototype.reValue = function () { return ( this.r[this.length] ); };
TNumber.prototype.toString = function( pos )
{
var o = "", e = this.length, pos = pos ? Math.max(0, pos) : Math.max( 0, e - 20 );
pos = Math.min( pos, e - 1 ); e = Math.min( pos + 20, e );
for( var i = pos; i < e; i++ )
{
o += "X" + i + " = " + this.a[i] + " / " + this.b[i] + "\r\n";
}
return( o );
};
//**********************************************************************************
//Last binary digit of accuracy in an float 64 number.
//This value is not predefined in older script engines.
//**********************************************************************************
Number.EPSILON = 1 / Math.pow(2, 52);
//**********************************************************************************
//Number of parts number, or fraction is split into.
//**********************************************************************************
Number.prototype.length = 0;
//*****************************************************************************************************
//Each part as we split number, or fraction apart.
//*****************************************************************************************************
Number.prototype.a = [undefined];
Number.prototype.b = [undefined];
Number.prototype.r = [undefined];
//*****************************************************************************************************
//Parts are added up by these values to compute the remaining value.
//*****************************************************************************************************
Number.prototype.tx = [0]; Number.prototype.fx = [1]; Number.prototype.ty = [1]; Number.prototype.fy = [0];
//*****************************************************************************************************
//Remaining value.
//*****************************************************************************************************
Number.prototype.val = [undefined];
//**********************************************************************************
//Anything past the last binary digit of accuracy is inaccurate.
//It can be set to a different cut off limit via the limit function.
//**********************************************************************************
Number.prototype.ac = Number.EPSILON;
//**********************************************************************************
//The cut off range has to be calculated once for each number, or can be set using limit.
//**********************************************************************************
Number.prototype.init_ac = false;
//**********************************************************************************
//The FL64 UI tool needs to know which number types need to refactor after one factor changes.
//**********************************************************************************
Number.prototype.reFact = false;
//*****************************************************************************************************
//Allows us to use integer, or floating point numbers when splitting an number apart.
//*****************************************************************************************************
Number.prototype.int = true;
//*****************************************************************************************************
//Split a number, or Fraction and return the object.
//*****************************************************************************************************
Number.prototype.rA = false; Number.prototype.rB = false;
function cfAdj( A, B )
{
Number.prototype.rA = A;
Number.prototype.rB = B;
Number.prototype.abLim = Number.prototype.reFact = A || B;
}
function intAdj(int) { Number.prototype.int = parseInt(int) == 1; }
Number.prototype.split = function (a, b)
{
if (this.r[this.length] === 0) { return (this); }
//On first split override the to string operation to show the remaining part, and value of to return the remaining value.
if (!this.init_ac) { this.init_ac = true; this.ac = Math.pow(2, (Math.round(Math.log(Math.abs(this.primitive())) / 0.6931471805599453))) * Number.EPSILON; }
if (this.length === 0)
{
var n = this.primitive(), s1 = n < 0 ? -1 : 1;
a = isNaN(a) ? Math.floor(n * s1) * s1 : a; b = b || 1;
if( !isNaN(n) && this.rA )
{
var r = a/(1/(n-a)), s2 = r < 0 ? -1 : 1;
r = Math.ceil( r * s2 ) * s2;
//Allow the value for B to be adjusted higher than r.
if( b * s2 < r * s2 ) { b = r; }
}
this.tx = [0]; this.ty = [1];
this.fx = [1]; this.fy = [0];
this.fx[0] = this.tx[0] + (this.tx[0] = this.fx[0] * b) * a / b;
this.fy[0] = this.ty[0] + (this.ty[0] = this.fy[0] * b) * a / b;
this.a = [a]; this.b = [b];
this.r = [n, (1 / (this.val[0] = n - a)) * b];
if (this.r[1] == Infinity) { this.r[1] = 0; }
if( this instanceof Number )
{
this.val = [n, Math.abs(this.r[0] - (this.fx[0] / this.fy[0]))];
}
else
{
this.r = [ new Fract(this.x, this.y), new Fract(this.y * b, this.x - (this.y * a)) ];
this.toString = function ()
{
var s = "";
for (var i = 0; i < this.length; s += "a=" + this.a[i] + ", b=" + this.b[i] + "\r\n", i++);
return (s + this.valueOf().getFract().toString());
};
}
this.valueOf = function () { return (this.val[this.length]); };
this.length += 1; return (this);
}
//Split value a by b. Or by default scale a=int, b=1.
var n = this.r[this.length], s1 = n < 0 ? -1 : 1;
//Split value a by b. Default is a=int, b=1.
a = isNaN(a) ? Math.floor(n * s1) * s1 : a; b = b || 1;
//If rA is active adjust the value for B so that the next value for A is as close to the previous value for A.
if( this.rA )
{
var r = a/(1/(n-a)), s = r < 0 ? -1 : 1;
r = Math.ceil( r * s ) * s;
//Allow the value for B to be adjusted higher than r.
if( b * s < r * s ) { b = r; }
}
//Force b >= b.
if( this.rB )
{
var s = this.b[this.length-1] < 0 ? -1 : 1;
if( b * s < this.b[this.length-1] * s )
{
b = this.b[this.length-1];
}
}
//The value for b should never be zero.
b = b == 0 ? 1 : b;
//Add a by b point.
this.a[this.length] = a; this.b[this.length] = b;
//Write remaining value. used by each next split.
if( this instanceof Number ) { this.r[this.length + 1] = (1 / (n - a)) * b; }
else { this.r[this.length + 1] = new Fract(n.y * b, n.x - (n.y * a)); }
//Add up each split into fx, fy per split
this.fx[this.length] = this.tx[this.length - 1] + (this.tx[this.length] = this.fx[this.length - 1] * b) * a / b;
this.fy[this.length] = this.ty[this.length - 1] + (this.ty[this.length] = this.fy[this.length - 1] * b) * a / b;
//Only re-factor FX, FY, TX, TY when values get to the end of the exponent.
//For the time being we will just move the exponent.
//Note to self. Max is 2^1023, so 2^(1023-51)=3.99168061906944e+292. Thus 2^(1023-51*2)=1.7726622920963562e+277
//Working close to the edge of a number still produces errors so we will use half of 512 instead of 1023 giving us 5.954262829429612e+138, and 2.6442238751609944e+123.
if (this.fx[this.length] > 5.954262829429612e+138 || this.fy[this.length] > 5.954262829429612e+138)
{
this.fx[this.length] /= 2.6442238751609944e+123; this.fy[this.length] /= 2.6442238751609944e+123;
this.tx[this.length] /= 2.6442238751609944e+123; this.ty[this.length] /= 2.6442238751609944e+123;
}
//Val is automatically 0 at cut off range.
if ((this.val[this.length+1] = Math.abs(this.r[0] - (this.fx[this.length] / this.fy[this.length]))) < this.ac)
{
this.val[this.length + 1] = 0; this.r[this.length + 1] = 0;
}
this.length += 1; return (this);
};
//*****************************************************************************************************
//Split a number and return the factorial number object.
//*****************************************************************************************************
FNumber.prototype.split = function (a, b)
{
if (this.r[this.length] === 0) { return (this); }
//On first split override the to string operation to show the remaining part, and value of to return the remaining value.
if (!this.init_ac) { this.init_ac = true; this.ac = Math.pow(2, (Math.round(Math.log(Math.abs(this.r[0])) / 0.6931471805599453))) * Number.EPSILON; }
//Split a number as spaced apart factorial expansion.
if( this.length > 0 )
{
//Denominator can not be made smaller once it is incremented in a number expansion.
a = ((a = ( a || 1 )) < this.a[this.length - 1] ? this.a[this.length - 1] : a);
//New factor must expand past at least +1 past current factor.
mx = this.b[this.length - 1]; b = b || 0; b = b === mx ? mx + 1 : b < mx ? mx : b;
//The min factor is exactly Denominator +1 in order for each factor to be an expansion from the last.
mn = !isNaN(this.reValue()) ? Math.round( ( a + 1 ) / this.reValue() ) : mx * ( this.a[this.length - 1] + 1 );
//The factor limit.
b = ( b || 0 ) < mn ? mn : b;
//Create the new factor.
this.a[this.length] = a; this.b[this.length] = b;
this.val[this.length + 1] = this.val[this.length] + ( a / b );
this.r[this.length + 1] = this.r[this.length] - ( a / b );
this.length += 1;
}
//First time we split a number.
else
{
if( a && b ) { if( ( a / b ) > this.r[0] ){ a = null; b = null; } }
//Zero can not be used.
if( (this.a = [a || Math.floor( this.r[0] )])[0] == 0 )
{
this.a = [1]; this.b = [Math.round( 2 / this.r[0] )];
}
else{ this.b = [b || 1]; }
this.val[1] = this.a[0] / this.b[0];
this.r[1] = this.r[0] - (this.a[0] / this.b[0]);
this.length += 1;
}
//Check number at accuracy set limit.
if( this.reValue() < this.ac ) { this.r[this.length] = 0; }
return (this);
};
//*****************************************************************************************************
//Split a Transcendental number and return the Transcendental number object.
//*****************************************************************************************************
TNumber.prototype.getMaxB = function()
{
return( Math.floor( Math.abs( this.a[this.length] / this.r[this.length] ) ) );
};
TNumber.prototype.getMinB = function()
{
return( Math.round( Math.sqrt( Math.abs( this.a[this.length] / this.r[this.length] ) ) ) );
};
TNumber.prototype.split = function( a, b )
{
if( this.r[this.length] == 0 ){ return(this); }
if (!this.init_ac) { this.init_ac = true; this.ac = Math.pow(2, (Math.round(Math.log(Math.abs(this.v / 0.6931471805599453))))) * Number.EPSILON; }
var s = ( this.length % 2 == 0 ) ? this.sing : -this.sing;
a = a || Math.ceil( isNaN(this.r[0]) ? 1 : Math.abs( this.r[0] ) ); if( a < this.a[this.length - 1] )
{
a = this.a[this.length - 1];
}
this.a[this.length] = a;
if( !isNaN(b) && b < 0 ) { b = -b; }
if(!isNaN(this.r[this.length]))
{
max = this.getMaxB(); min = this.getMinB();
if ( isNaN(b) ) { b = max; } else if( b > max ) { b = max; } else if( b < min ) { b = min; }
}
if( b <= this.b[this.length - 1] * -s )
{
b = ( this.b[this.length - 1] * -s ) + 1;
}
this.b[this.length] = b * s;
this.r[this.length+1] = this.r[this.length] - this.a[this.length] / this.b[this.length];
this.val[this.length+1] = this.val[this.length] + this.a[this.length] / this.b[this.length];
this.length += 1; if( Math.abs(this.r[this.length]) < this.ac ){ this.r[this.length] = 0; }
return( this );
};
//*****************************************************************************************************
//Split a number into all parts.
//*****************************************************************************************************
Number.prototype.splitAll = function ()
{
while( this.split() != 0 ); return (this);
};
//**********************************************************************************
//Remove a factor.
//**********************************************************************************
Number.prototype.remove = function (el)
{
var n = this;
var End = n.length - 1, ba = n.a, bb = n.b; n.length = el;
while (n.length < End) { el += 1; n = n.split(ba[el], bb[el]); }
return (n);
};
//**********************************************************************************
//set the A, or B factors.
//**********************************************************************************
Number.prototype.setA = function (el, v)
{
if(this.int){ v = Math.round(v); }
var n = this, End = n.length, ba = n.a, bb = n.b; n.length = el;
n = n.split(v, bb[el]); while (el < End) { el += 1; n = n.split(ba[el], bb[el]); }
while( End < this.length ) { this.length -= 1; n.a.pop(); n.b.pop(); }
return(n);
};
Number.prototype.setB = function (el, v)
{
if(this.int){ v = Math.round(v); }
//The value cant be set 0 so it is set -1 or +1 based on direction.
if (v === 0) { v = this.b[el] > v ? -1 : 1; }
//Continue with function.
var n = this, End = n.length, ba = n.a, bb = n.b; n.length = el;
n = n.split(ba[el], v); while (el < End) { el += 1; n = n.split(ba[el], bb[el]); }
while( End < this.length ) { this.length -= 1; n.a.pop(); n.b.pop(); }
return (n);
};
//*****************************************************************************************************
//Directly calculate parts to a floating point number.
//*****************************************************************************************************
Number.prototype.calc = function (Start, End)
{
if (this.length == 0) { return (0); }
var Start = Start || 0; if (typeof (End) === "undefined") { End = this.length - 1; }
//If start position is unaltered and is 0. Then each added split from 0 is prerecorded.
if (Start === 0) { return (this.fx[End] / this.fy[End]); }
//Else calculate the value.
for (var i = End - 1, f = this.a[End] / this.b[End]; i >= Start; i--) { f = this.a[i] + (this.b[i] / f); }
return (f);
};
FNumber.prototype.calc = function (Start, End)
{
if (this.length == 0) { return ( 0 ); }
var Start = Start || 0, End = End || this.length - 1;
Start = Math.max( Start, 0 ); End = Math.max( End, 0 );
if (typeof (End) === "undefined") { End = this.length - 1; }
//If start position is unaltered and is 0. Then each added split from 0 is prerecorded.
if (Start === 0) { return ( this.val[End + 1] * this.sing ); }
//Else calculate the value.
for (var i = End - 1, f = this.a[End] / this.b[End]; i >= Start; i--) { f += this.a[i] / this.b[i]; }
return ( f * this.sing );
};
TNumber.prototype.calc = function (Start, End)
{
if (this.length == 0) { return ( 0 ); }
var Start = Start || 0, End = End || this.length - 1;
Start = Math.max( Start, 0 ); End = Math.max( End, 0 );
if (typeof (End) === "undefined") { End = this.length - 1; }
//If start position is unaltered and is 0. Then each added split from 0 is prerecorded.
if (Start === 0) { return ( this.val[End + 1] ); }
//Else calculate the value.
for (var i = End - 1, f = this.a[End] / this.b[End]; i >= Start; i--) { f += this.a[i] / this.b[i]; }
return ( f );
};
//**********************************************************************************
//Combine Start to end parts as a whole fraction.
//**********************************************************************************
Number.prototype.calcF = function (Start, End)
{
var Start = Start || 0; if (typeof (End) === "undefined") { End = this.length - 1; }
//If start position is unaltered and is 0. Then each added split from 0 is prerecorded.
if (Start === 0) { return (new Fract(this.fx[End], this.fy[End])); }
//Else calculate the value.
var x1 = 1, x2 = 0, y1 = 0, y2 = 1;
for (var i = Start; i <= End; i++)
{
x1 = x2 + (x2 = x1 * this.b[i]) * this.a[i] / this.b[i];
y1 = y2 + (y2 = y1 * this.b[i]) * this.a[i] / this.b[i];
}
return (new Fract(x1, y1));
};
FNumber.prototype.calcF = function (Start, End)
{
if (this.length == 0) { return (new Fract(0, 1)); }
var Start = Start || 0; if (typeof (End) === "undefined") { End = this.length - 1; }
//If start position is unaltered and is 0. Then each added split from 0 is prerecorded.
if (Start === 0) { return ( ( this.val[End + 1] * this.sing ).getFract() ); }
//Else calculate the value.
for (var i = End - 1, f = this.a[End] / this.b[End]; i >= Start; i--) { f += this.a[i] / this.b[i]; }
return ( ( f * this.sing ).getFract() );
};
TNumber.prototype.calcF = function (Start, End)
{
if (this.length == 0) { return (new Fract(0, 1)); }
var Start = Start || 0; if (typeof (End) === "undefined") { End = this.length - 1; }
//If start position is unaltered and is 0. Then each added split from 0 is prerecorded.
if (Start === 0) { return (new TNumber( this.val[End + 1].getFract() ) ); }
//Else calculate the value.
for (var i = End - 1, f = this.a[End] / this.b[End]; i >= Start; i--) { f += this.a[i] / this.b[i]; }
return ( f.getFract() );
};
//**********************************************************************************
//Transform All factors by two functions.
//**********************************************************************************
Number.prototype.abLim = false;
Number.prototype.Trans = function (x, fa, fb)
{
if (isNaN(fa(1)) || isNaN(fb(1))) { return (this); }
var sing = ( this.primitive() || 0 ) < 0, a = fa(1), b = fb(1), i = 1;
if (isNaN(this)) { while (this.length < x) { this.split(1,1); }; }
else { while (this.length < x) { this.split(); }; }
this.length = x;
//Get time. This is set for if the calculation takes longer than 1.5 seconds.
var t = new Date().getTime();
//If we are transforming a number that is NaN.
if (isNaN(this))
{
if(this.abLim)
{
while (x < 20000)
{
if(this.int) { a = Math.round(a); b = Math.round(b); }
x += 1; i += 1; this.split(a, b);
if( a == this.a[this.length-1] && b == Math.abs( this.b[this.length-1] ) ) { a = fa(i); b = fb(i); }
else { this.length -= 1; break; }
}
}
else
{
while (x < 20000)
{
if(this.int) { a = Math.round(a); b = Math.round(b); }
x += 1; i += 1; this.split(a, b);
a = fa(i); b = fb(i);
}
}
}
//Transform as many factors as possible. In range of each split.
else if( this.abLim )
{
while (this.r[this.length] != 0 && (new Date().getTime() - t) < 1500 )
{
if(this.int) { a = Math.round(a); b = Math.round(b); }
x += 1; i += 1; this.split(a, b);
if( a == this.a[this.length-1] && b == Math.abs( this.b[this.length-1] ) ) { a = fa(i); b = fb(i); }
else { this.length -= 1; break; }
}
}
else
{
if( !sing )
{
while (this.r[this.length] > 0 && (new Date().getTime() - t) < 1500 )
{
if(this.int) { a = Math.round(a); b = Math.round(b); }
x += 1; i += 1; this.split(a, b);
a = fa(i); b = fb(i);
}
}
else
{
while (this.r[this.length] < 0 && (new Date().getTime() - t) < 1500 )
{
if(this.int) { a = Math.round(a); b = Math.round(b); }
x += 1; i += 1; this.split(a, b);
a = fa(i); b = fb(i);
}
}
}
if ((new Date().getTime() - t) >= 1500) { throw(new RangeError("Time out")); }
return (this);
};
//**********************************************************************************
//Remove factors that are past a set accuracy limit.
//**********************************************************************************
Number.prototype.limit = function (ac)
{
//Set accuracy limit.
this.init_ac = true; this.ac = Math.pow(2, (Math.round(Math.log(Math.abs(this.primitive())) / 0.6931471805599453))) * (ac || Number.EPSILON);
//Set length to accuracy limit.
for (; this.val[this.length - 2] < this.ac; this.length--);
if (this.length > 0) { this.length -= 1; this.split(this.a[this.length], this.b[this.length]); }
return (this);
};
//*****************************************************************************************************
//Split and add number to smallest fraction. Works for reducing a fraction as well.
//*****************************************************************************************************
Number.prototype.getFract = function ()
{
if (!this.init_ac) { this.init_ac = true; this.ac = Math.pow(2, (Math.round(Math.log(Math.abs(this.primitive())) / 0.6931471805599453))) * Number.EPSILON; }
var v = (n = this.primitive()), r = 0;
this.fx = 1; this.fy=0; this.tx = 0; this.ty=1;
while ((Math.abs(v - (this.fx / this.fy))) > this.ac)
{
r = Math.floor(n); n = 1 / (n - r);
//Add up each split into fx, fy per split
this.fx = this.tx + (this.tx = this.fx) * r;
this.fy = this.ty + (this.ty = this.fy) * r;
}
return (new Fract(this.fx, this.fy));
};
//**********************************************************************************
//Directly translate an float to it's average matching integer parts.
//**********************************************************************************
Number.prototype.avgFract = function ()
{
for (var i = 0, o = 0, n = this.splitAll(); i < n.length; o += n.a[i++]); o = Math.round(o / i);
i = 0; while (n.a[i] <= o) { i += 1; }; i -= 1; return (new Fract(n.fx[i], n.fy[i]));
};
//**********************************************************************************
//Error correct an number.
//**********************************************************************************
Number.prototype.err = function ()
{
for (var i = 0, o = 0, n = this.splitAll(); i < n.length; o += n.a[i], i++); o = Math.round(o / i);
//Remove a factor bigger than the average integer parts from the end of the number.
while( i > 0 )
{
if( n.a[i] >= o )
{
return(new Fract(n.fx[i-1], n.fy[i-1]));
}
i-=1;
}
return( new Fract( n.fx[n.lenght-1], n.fy[n.length-1] ) );
};
//**********************************************************************************
//Show different stats about Numbers.
//**********************************************************************************
Number.prototype.stats = function()
{
for (var i = 0, o = 0, n = this.splitAll(); i < n.length; o += n.a[i], i++); o = Math.round(o / i);
console.log("A = " + n.a.toString());
console.log("B = " + n.b.toString());
console.log("Avg Int = " + o);
//calculate convergence.
var r = Math.round(Math.log(n.val[0])/Math.log(2))+1, c = [];
for( var i = 0; i < n.length; i++ )
{
c[i]=Math.abs(Math.round(Math.log(n.val[i])/Math.log(2))-r);
}
c.pop(); console.log("Converge Speed (2^52)+1.\r\n" + c.toString().replace(/,/g, "\r\n"));
//Average converge speed.
for (var i = 0, o = 0; i < c.length; i++) { o += c[i]; }; o /= i;
console.log("Avg converge speed (2^52)+1.\r\n" + o + "");
//the Size between each per factor jump.
for (var i = 0; i < c.length; i++) { c[i] = c[i + 1] - c[i]; }; c.pop();
console.log("Converge Speed Dif.\r\n" + c.toString().replace(/,/g, "\r\n"));
//Average converge speed dif.
for (var i = 0, o = 0; i < c.length; i++) { o += c[i]; }; o/=i;
console.log("Avg converge speed Dif.\r\n" + o + "");
//Value at different accuracy levels.
for(var i = 0, ac = []; i < n.length; i++)
{
ac[i] = n.fx[i]+"/"+n.fy[i]+"\r\n"+(n.fx[i]/n.fy[i])+"\r\n-----------------------";
}
console.log("Accuracy levels, and factors.\r\n" + ac.toString().replace(/,/g, "\r\n"));
};
//**********************************************************************************
//The pattern data type is used for numbers that do not divide thus the pattern can be stored separably as an data type.
//**********************************************************************************
var Pattern = function (str, base)
{
base = base || 2; base.isNum(); this.base = base; this.pat = []; str = str || ""; str = str.replace("\u221E", "").toUpperCase();
//Check if invalid base setting.
if (base < 2 || base > 36) { throw new RangeError("radix must be an integer at least 2 and no greater than 36"); }
//Parse the pattern. Return undefined if improper format.
for (var i = 0; i < str.length; i++)
{
this.pat[i] = str.charCodeAt(i) < 0x40 ? str.charCodeAt(i) - 0x30 : str.charCodeAt(i) - 0x37;
if (this.pat[i] >= base || this.pat[i] < 0) { this.pat = [Infinity]; return; };
}
//Return the pattern data.
return (this);
};
//**********************************************************************************
//Do division one step at an time stopping at the pattern in the division.
//Default base is binary, however base can be set 2 to 36.
//**********************************************************************************
Fract.prototype.divP = function (base)
{
//Pattern data type.
var pat = new Pattern(); pat.base = base || 2; pat.base.isNum();
//Fraction to divide.
n1 = Math.round(Math.abs(this.x)), n2 = Math.round(Math.abs(this.y));
//Data of the divide operation.
pat.data = [n1];
//El (Element), and C (Current element), and whether to force computation.
var El = -1, C = 0;
//Get time. This is set for if the calculation takes longer than 7 seconds.
var t = new Date().getTime();
//If divide by 0.
if (pat.data[0] === 0) { pat.pat = [0]; pat.data = null; return (pat); }
//While the Element is not equal to the divide going in and coming out.
while (El === -1)
{
if ((new Date().getTime() - t) > 7000) { var err = new RangeError("Time out"); err.data = pat.data; throw(err); }
for (var Exp = pat.data[pat.data.length - 1], i = 0; (Exp - n2) < 0; Exp *= pat.base, i++)
{
if (i >= 1)
{
pat.pat[pat.pat.length] = 0; pat.data[pat.data.length] = null;
}
}
i = ((Exp / n2) & -1); pat.pat[pat.pat.length] = i; C = Exp - (n2 * i); pat.data[pat.data.length] = C;
for (El = pat.data.length - 2; El > -1 && pat.data[El] !== C; El--);
if (C === 0) { pat.pat = [0]; pat.data = null; return (pat); }
}
//return the pattern at the first matching element to current element.
pat.data = null; pat.pat = pat.pat.slice(El, pat.pat.length); return (pat);
};
//**********************************************************************************
//Convert patterns into an fraction that generates the division pattern.
//**********************************************************************************
Pattern.prototype.getFract = function ()
{
//Initialize.