-
Notifications
You must be signed in to change notification settings - Fork 5
/
programmers_converter.html
2154 lines (2033 loc) · 99.7 KB
/
programmers_converter.html
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
<!doctype html>
<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="favicon.ico">
<link rel="stylesheet" type="text/css" href="style2.css">
<title>Programmer's Number Converter</title>
<script type="text/javascript" src="page.js"></script>
<style type="text/css">
div.linebreak {
width: 100%;
}
.hidden {
display: none;
}
h2 {
-webkit-flex: 1 1 auto;
flex: 1 1 auto;
margin: 1rem 0rem 0.5rem 0rem;
width: 100%;
}
div.indent_wrap {
margin-top: 0.5em;
margin-bottom: 0.5em;
padding-left: 2em;
text-indent: -2em;
}
label {
display: inline-block;
text-indent: 0;
}
.data {
box-sizing: border-box;
border: 1px solid #888888;
width: 100%;
max-width: 100%;
min-width: 100%;
height: 15em;
max-height: 15em;
margin: 0em;
padding: 0.2em;
overflow: scroll;
white-space: pre;
word-wrap: normal; /* Workaround for some browsers defaulting to break-word */
font-family: 'Courier New', Courier, monospace;
font-size: 1rem;
}
.data[readonly] {
background-color: transparent;
}
#convertbut {
box-sizing: border-box;
width: 100%;
font-weight: bold;
}
input[type="number"] {
text-align: right;
}
.in_digit_base, .in_number_field_width, .in_number_n_twos_compl_bits, .in_number_left_shift,
.out_digit_base, .out_number_n_int_digits, .out_number_n_frac_digits, .out_number_n_twos_compl_bits,
.out_number_left_shift,
.reshape_n_rows_or_cols {
width: 3em;
}
.in_digit_grp_separator,
.in_number_repetend_prefix, .in_number_repetend_suffix,
.out_number_repetend_prefix, .out_number_repetend_suffix,
.out_number_prefix, .out_number_suffix, .out_number_separator,
.out_row_prefix, .out_row_suffix, .out_row_separator {
width: 3em;
}
</style>
<script type="text/javascript">
"use strict";
///////////////////////////////////////////////////////////////////////////////
// Class ConversionConfig
//
class ConversionConfig
{
static SIGNED_TYPE = {UNSIGNED: 0, SIGNED: 1, TWOS_COMPL: 2};
static COLUMN_ALIGN_TYPE = {NONE: 0, RADIX_CHAR: 1};
in_digit_base = 10;
in_numbers_are_delimited = true;
in_number_field_width = 2;
in_number_signed_type = ConversionConfig.SIGNED_TYPE.SIGNED;
// in_number_n_twos_compl_bits is an integer (possibly negative) which defines the input two's-complement format
// such that a value is valid in this format if and only if the value is non-negative and less than
// 2**in_number_n_twos_compl_bits
in_number_n_twos_compl_bits = 8;
in_number_has_frac = true;
in_radix_char = '.';
in_digit_grp_separator = '';
in_number_has_repetend = false;
in_number_repetend_prefix = '(';
in_number_repetend_suffix = ')';
in_number_has_exponent = true;
// in_number_left_shift is the number of digits (or bits if in_number_left_shift_uses_bits is true or
// in_number_signed_type is TWOS_COMPL) which input numbers have been shifted left compared to their real values (a
// negative shift means they have been shifted right), i.e. the real values can be obtained by performing the
// opposite shift
in_number_left_shift = 0;
in_number_left_shift_uses_bits = false;
in_number_regexp = undefined; // calculated from other members
in_number_garbage_regexp = undefined; // calculated from other members
out_digit_base = 16;
out_numbers_are_fixed_width = false;
out_number_n_int_digits = 2;
out_number_signed_type = ConversionConfig.SIGNED_TYPE.SIGNED;
// out_number_n_twos_compl_bits is an integer (possibly negative) which defines the output two's-complement format
// such that a value is valid in this format if and only if the value is non-negative and less than
// 2**out_number_n_twos_compl_bits
out_number_n_twos_compl_bits = 8;
out_number_has_frac = true;
out_radix_char = '.';
// out_number_n_frac_digits may be undefined to indicate an infinite number of digits (when not fixed width)
out_number_n_frac_digits = 10;
out_number_repetend_prefix = '(';
out_number_repetend_suffix = ')';
// out_number_left_shift is the number of digits (or bits if out_number_left_shift_uses_bits is true or
// out_number_signed_type is TWOS_COMPL) which output numbers will be shifted left compared to their real values (a
// negative shift means they will be shifted right)
out_number_left_shift = 0;
out_number_left_shift_uses_bits = false;
out_number_prefix = '';
out_number_suffix = '';
out_number_separator = ', ';
out_row_prefix = '';
out_row_suffix = '\n';
out_row_separator = '';
out_column_align_type = ConversionConfig.COLUMN_ALIGN_TYPE.NONE;
reshape = false;
reshape_n_rows_or_cols = 1;
reshape_set_rows = false;
reorder_read_direction = 'cr';
reorder_write_direction = 'cr';
constructor()
{
this.recalc();
}
/**
* Recalculates the calculated members from the non-calculated members of this ConversionConfig
*/
recalc()
{
var digit_re =
this.in_digit_base <= 10 ? '[0-' + (this.in_digit_base - 1) + ']' :
'[0-9a-' + String.fromCharCode(this.in_digit_base - 1 - 10 + 97) + ']';
this.in_number_garbage_regexp = new RegExp('[^' + digit_re.slice(1), 'gi');
if (!this.in_numbers_are_delimited)
{
// Capture fixed number of digits as integer part, and empty strings as sign, fractional, repetend, and
// exponent parts
this.in_number_regexp = new RegExp('()(' + digit_re + '{' + this.in_number_field_width + '})(())()', 'gi');
}
else
{
// Sign capture
var sign_re = this.in_number_signed_type == ConversionConfig.SIGNED_TYPE.SIGNED ? '([+-]?)' : '()';
// Allow but don't capture prefixes for certain bases
var prefix_re =
this.in_digit_base == 16 ? '(?:0x)?' :
this.in_digit_base == 2 ? '(?:0b)?' :
'';
var grp_char_re = escape_string_for_regexp(this.in_digit_grp_separator);
var digit_grps_re = digit_re + '+(?:' + grp_char_re + digit_re + '+)*';
if (this.in_number_has_frac)
{
var radix_char_re = escape_string_for_regexp(this.in_radix_char);
// Integer part capture (allow an empty integer part in case the fractional part is not empty -
// extract_next_nbr_parts will reject the case where neither part has any digits)
var int_re = sign_re + prefix_re + '(' + digit_grps_re + '|(?=' + radix_char_re + '))';
var repetend_re = '()';
// Repetend capture (if allowed)
if (this.in_number_has_repetend)
{
repetend_re =
'(' +
escape_string_for_regexp(this.in_number_repetend_prefix) +
digit_grps_re +
escape_string_for_regexp(this.in_number_repetend_suffix) +
')?';
}
// Fractional part and repetend capture (exclude radix char)
var frac_re = '((?:' + radix_char_re + '(?:' + digit_grps_re + ')?' + repetend_re + ')?)';
}
else
{
// Integer part capture
var int_re = sign_re + prefix_re + '(' + digit_grps_re + ')';
// Empty fractional part and repetend capture
var frac_re = '(())';
}
var exponent_re = '()';
// Exponent capture (if allowed)
if (this.in_number_has_exponent && this.in_digit_base == 10)
{
exponent_re = '(?:[Ee]([+-]?' + digit_re + '+))?';
}
this.in_number_regexp = new RegExp(int_re + frac_re + exponent_re, 'gi');
}
}
merge_digit_groups(str)
{
return str.replace(this.in_number_garbage_regexp, '');
}
/**
* Returns an object containing the sign, integer, and fractional strings and repetend length for the next number
* found in the specified string, or null if no more numbers are found. This method may be called repeatedly with
* the same string to extract all numbers from that string.
*
* @param {string} str - String containing zero or more string representations of numbers.
* @returns {Object} Object containing the sign, integer, and fractional strings and repetend length of the next
* number found in str.
*/
extract_next_nbr_parts(str)
{
while (true)
{
// Find next number in string
var matches = this.in_number_regexp.exec(str);
// Return null if no number found
if (!matches) return null;
var int_str = this.merge_digit_groups(matches[2]);
var frac_str = this.merge_digit_groups(matches[3]);
// Only return a result if the integer and fractional parts are not both empty (a radix char without any
// digits is not considered a number) - otherwise loop around and try to find another number
if (int_str + frac_str != '')
{
// A number was found, so return its parts
return {
sign_str: matches[1],
int_str: int_str || '0',
frac_str: frac_str,
repetend_len: this.merge_digit_groups(matches[4] || '').length,
exponent_str: matches[5]
};
}
}
}
}
///////////////////////////////////////////////////////////////////////////////
// Class Nbr
//
class Nbr
{
// Internal representation (sign-magnitude):
// The int_part represents the integer part of the magnitude of Nbr as a non-negative BigInt.
// The frac_digit_vec represents the fractional part of the magnitude of Nbr as a little-endian Uint8Array where
// each element represents a digit in the given digit_base.
// The digit_base is the chosen base/radix of the Nbr (bases 2 through 36 are supported).
// The is_negative flag represents the sign of Nbr.
// The repetend_len is the number of fractional digits that form the repetend, starting from frac_digit_vec[0].
// The is_incomplete flag indicates that the fractional part has been cut short because it (or its repetend) would
// otherwise be excessively long (i.e. take excessively long time to calculate).
//
// The last_nbr_to_string_radix_pos and last_nbr_to_string_rest_len are not part of the internal representation of
// Nbr, but hold the position of the radix char (or where it would have been) and the length of the rest of the
// string in the output from the last call to Nbr.toString() - for the purpose of subsequent alignment of value
// strings in columns. The sum of last_nbr_to_string_radix_pos and last_nbr_to_string_rest_len is always equal to
// the total length of the string produced by the last call to Nbr.toString().
//
// For a normalized Nbr the following applies: the frac_digit_vec contains no leading (least-significant) zero
// values, and the Nbr zero is represented as a zero int_part, a zero-length frac_digit_vec, and a false
// is_negative flag.
//
// A Nbr is invalid if and only if int_part is undefined.
static conversion_config;
int_part;
frac_digit_vec;
digit_base;
is_negative;
repetend_len;
is_incomplete = false;
last_nbr_to_string_radix_pos;
last_nbr_to_string_rest_len;
/**
* Constructs a Nbr from the given integer value (may be negative or undefined) and optional base/radix (default
* 10), fractional digits (default none), sign (default non-negative), and repetend length (default 0). If a length
* is given instead of an array of fractional digits, the fractional part will be that many zeros. Note: The
* contructed number is NOT normalized, so may represent -0 and have trailing zeros.
*/
constructor(int_val, digit_base = 10, frac_digit_vec_or_len = 0, is_negative = false, repetend_len = 0)
{
this.int_part = (int_val !== undefined) ? BigInt(int_val) : undefined;
this.frac_digit_vec = new Uint8Array(frac_digit_vec_or_len);
this.digit_base = digit_base;
this.is_negative = is_negative;
if (int_val < 0)
{
this.int_part = -this.int_part;
this.is_negative = !this.is_negative;
}
this.repetend_len = repetend_len;
}
/**
* Returns a new normalized Nbr converted from the next match in the specified string, or null if no more matches.
* Nbr.conversion_config must be set before calling this method.
*/
static from_string(str)
{
var nbr_parts = Nbr.conversion_config.extract_next_nbr_parts(str);
if (!nbr_parts)
{
return null;
}
// Prepare new Nbr with zero integer part and correct base, fractional length, and sign
var nbr = new Nbr(
0,
Nbr.conversion_config.in_digit_base,
nbr_parts.frac_str.length,
nbr_parts.sign_str == '-',
nbr_parts.repetend_len);
// Update integer part from string
for (var i = 0; i < nbr_parts.int_str.length; i++)
{
nbr.int_part = nbr.int_part * BigInt(nbr.digit_base) +
BigInt(parseInt(nbr_parts.int_str.charAt(i), nbr.digit_base));
}
// Update fractional part from string
for (var i = 0; i < nbr_parts.frac_str.length; i++)
{
nbr.frac_digit_vec[nbr_parts.frac_str.length - 1 - i] =
parseInt(nbr_parts.frac_str.charAt(i), nbr.digit_base);
}
if (nbr_parts.exponent_str)
{
nbr.shift_left(parseInt(nbr_parts.exponent_str));
}
return nbr.normalize();
}
/**
* Returns the start row/column, and primary and secondary row/column step deltas determined from the given
* 2-character reorder direction and total number of rows and columns.
*/
static translate_reorder_direction(reorder_direction, n_rows, n_columns)
{
var start = {}, step1 = {}, step2 = {};
if (reorder_direction.charAt(0) == 'r')
{
start.r = 0;
step1.dr = 1;
step2.dr = -n_rows;
}
else if (reorder_direction.charAt(0) == 'R')
{
start.r = n_rows - 1;
step1.dr = -1;
step2.dr = n_rows;
}
else if (reorder_direction.charAt(1) == 'r')
{
start.r = 0;
step1.dr = 0;
step2.dr = 1;
}
else if (reorder_direction.charAt(1) == 'R')
{
start.r = n_rows - 1;
step1.dr = 0;
step2.dr = -1;
}
if (reorder_direction.charAt(0) == 'c')
{
start.c = 0;
step1.dc = 1;
step2.dc = -n_columns;
}
else if (reorder_direction.charAt(0) == 'C')
{
start.c = n_columns - 1;
step1.dc = -1;
step2.dc = n_columns;
}
else if (reorder_direction.charAt(1) == 'c')
{
start.c = 0;
step1.dc = 0;
step2.dc = 1;
}
else if (reorder_direction.charAt(1) == 'C')
{
start.c = n_columns - 1;
step1.dc = 0;
step2.dc = -1;
}
return [start, step1, step2];
}
/**
* Returns a string that is the specified string converted according to the specified ConversionConfig.
* The specified string may contain multiple rows of numbers which will all be converted.
* This method stores the specified ConversionConfig in Nbr.conversion_config.
*/
static convert(in_str, conv_cfg)
{
conv_cfg.recalc();
Nbr.conversion_config = conv_cfg;
var in_rows = in_str.trim().split(/\r?\n/);
var in_matrix = [];
var in_n_columns = 0;
for (var i = 0; i < in_rows.length; i++)
{
var numbers = [];
var nbr;
// Find all numbers in the row string, convert them to Nbrs, and perform any additional conversions
// specified (including changing their base to the specified output base)
while (nbr = Nbr.from_string(in_rows[i]))
{
// If base is not already 2, then convert to base 2 if input two's complement conversion or bit shifting
// is needed
if (nbr.digit_base != 2 && (
conv_cfg.in_number_signed_type == ConversionConfig.SIGNED_TYPE.TWOS_COMPL ||
conv_cfg.in_number_left_shift_uses_bits && conv_cfg.in_number_left_shift != 0
))
{
nbr.change_base(2);
}
// Perform input two's complement conversion if specified
if (conv_cfg.in_number_signed_type == ConversionConfig.SIGNED_TYPE.TWOS_COMPL)
{
nbr.convert_from_twos_compl();
}
// Perform input shift if specified
nbr.shift_left(-conv_cfg.in_number_left_shift);
let output_shifted_early = false;
// if output two's complement conversion or bit shifting is needed, then convert to base 2 if base is
// not already 2, and perform output shift (to achieve bit shifting BEFORE output two's complement
// conversion and output base conversion)
if (conv_cfg.out_number_signed_type == ConversionConfig.SIGNED_TYPE.TWOS_COMPL ||
conv_cfg.out_number_left_shift_uses_bits && conv_cfg.out_number_left_shift != 0)
{
if (nbr.digit_base != 2) nbr.change_base(2);
nbr.shift_left(conv_cfg.out_number_left_shift);
output_shifted_early = true;
}
// Perform output two's complement conversion if specified
if (conv_cfg.out_number_signed_type == ConversionConfig.SIGNED_TYPE.TWOS_COMPL)
{
nbr.convert_to_twos_compl();
}
// Perform requested conversion to output base
nbr.change_base(conv_cfg.out_digit_base, conv_cfg.out_number_n_frac_digits);
// If the output shift has not already been done, then do it now (for digit shifting rather than bit
// shifting)
if (!output_shifted_early)
{
nbr.shift_left(conv_cfg.out_number_left_shift);
}
numbers.push(nbr);
}
if (numbers.length > 0)
{
in_n_columns = Math.max(in_n_columns, numbers.length);
in_matrix.push(numbers);
}
}
var in_n_rows = in_matrix.length;
// Create output matrix from input matrix
var total_n_elems = in_n_rows * in_n_columns;
var out_n_columns = in_n_columns;
var out_n_rows = in_n_rows;
if (conv_cfg.reshape)
{
if (conv_cfg.reshape_set_rows)
{
out_n_rows = conv_cfg.reshape_n_rows_or_cols;
out_n_columns = Math.ceil(total_n_elems / out_n_rows);
}
else
{
out_n_columns = conv_cfg.reshape_n_rows_or_cols;
out_n_rows = Math.ceil(total_n_elems / out_n_columns);
}
}
var out_matrix = Array(out_n_rows);
for (var i = 0; i < out_n_rows; i++)
{
out_matrix[i] = Array(out_n_columns);
}
var [in_indices, in_step1, in_step2] =
Nbr.translate_reorder_direction(conv_cfg.reorder_read_direction, in_n_rows, in_n_columns);
var [out_indices, out_step1, out_step2] =
Nbr.translate_reorder_direction(conv_cfg.reorder_write_direction, out_n_rows, out_n_columns);
for (var i = 0; i < total_n_elems; i++)
{
if (in_matrix[in_indices.r][in_indices.c] !== undefined)
{
out_matrix[out_indices.r][out_indices.c] = in_matrix[in_indices.r][in_indices.c];
}
in_indices.r += in_step1.dr;
in_indices.c += in_step1.dc;
if (in_indices.r < 0 || in_indices.r >= in_n_rows || in_indices.c < 0 || in_indices.c >= in_n_columns)
{
in_indices.r += in_step2.dr;
in_indices.c += in_step2.dc;
}
out_indices.r += out_step1.dr;
out_indices.c += out_step1.dc;
if (out_indices.r < 0 || out_indices.r >= out_n_rows || out_indices.c < 0 || out_indices.c >= out_n_columns)
{
out_indices.r += out_step2.dr;
out_indices.c += out_step2.dc;
}
}
// Convert all elements of output matrix to strings (incl. undefined elements if aligning in columns)
var out_string_matrix = Array(out_n_rows);
if (conv_cfg.out_column_align_type == ConversionConfig.COLUMN_ALIGN_TYPE.RADIX_CHAR)
{
var column_radix_positions = Array(out_n_columns).fill(0);
var column_rest_lengths = Array(out_n_columns).fill(0);
for (var out_r = 0; out_r < out_n_rows; out_r++)
{
var row = out_matrix[out_r];
var string_row = Array(out_n_columns);
for (var out_c = 0; out_c < out_n_columns; out_c++)
{
var nbr = row[out_c];
if (nbr === undefined)
{
string_row[out_c] = '';
}
else
{
var s = nbr.toString();
column_radix_positions[out_c] = Math.max(column_radix_positions[out_c], nbr.last_nbr_to_string_radix_pos);
column_rest_lengths[out_c] = Math.max(column_rest_lengths[out_c], nbr.last_nbr_to_string_rest_len);
string_row[out_c] = s;
}
}
out_string_matrix[out_r] = string_row;
}
}
else
{
for (var out_r = 0; out_r < out_n_rows; out_r++)
{
var row = out_matrix[out_r];
var string_row = Array(out_n_columns);
for (var out_c = 0; out_c < out_n_columns; out_c++)
{
var nbr = row[out_c];
if (nbr !== undefined)
{
string_row[out_c] = nbr.toString();
}
}
out_string_matrix[out_r] = string_row;
}
}
// Join all elements of output string matrix into one large string - applying column alignment if requested
var out_string_rows = [];
for (var out_r = 0; out_r < out_n_rows; out_r++)
{
var string_row = out_string_matrix[out_r];
if (conv_cfg.out_column_align_type == ConversionConfig.COLUMN_ALIGN_TYPE.RADIX_CHAR)
{
// Align elements according to radix char positions
var row = out_matrix[out_r];
string_row = string_row.map((string_elem, out_c) => {
let nbr = row[out_c];
if (nbr === undefined)
{
return ' '.repeat(column_radix_positions[out_c] + column_rest_lengths[out_c]);
}
else
{
return ' '.repeat(column_radix_positions[out_c] - nbr.last_nbr_to_string_radix_pos)
+ string_elem
+ ' '.repeat(column_rest_lengths[out_c] - nbr.last_nbr_to_string_rest_len);
}
});
}
else
{
// Remove undefined string elements - and the entire row if all elements are undefined
string_row = Object.keys(string_row).map(k => string_row[k]);
if (string_row.length == 0) continue;
}
// Join all elements in the row using the specified separator and attach the specified row prefix and suffix
out_string_rows.push(conv_cfg.out_row_prefix
+ string_row.join(conv_cfg.out_number_separator)
+ conv_cfg.out_row_suffix);
}
var out_str = out_string_rows.join(conv_cfg.out_row_separator);
return out_str;
}
static self_test()
{
function assert(test_id, condition)
{
if (!condition)
{
throw 'Nbr self-test ' + test_id;
}
}
function check_nbr(test_id, nbr, int_part, frac_digit_vec, repetend_len, is_incomplete, digit_base, is_negative)
{
if (int_part === undefined && nbr.int_part !== undefined ||
int_part !== undefined && (
nbr.int_part === undefined || nbr.int_part != int_part ||
nbr.frac_digit_vec.length != frac_digit_vec.length ||
nbr.frac_digit_vec.some((v, i) => v != frac_digit_vec[i]) ||
nbr.repetend_len != repetend_len ||
nbr.is_incomplete != is_incomplete ||
nbr.digit_base != digit_base ||
nbr.is_negative != is_negative))
{
throw 'Nbr self-test ' + test_id + ' (' + nbr.int_part + ', [' + nbr.frac_digit_vec + '], ' +
nbr.repetend_len + ', ' + nbr.digit_base + ', ' + nbr.is_negative + ')';
}
}
// Test constructor
var nbr1 = new Nbr(1, 10, [3]); // 1.3
check_nbr(1.1, nbr1, 1, [3], 0, false, 10, false);
var nbr1m = new Nbr(1, 10, [3], true); // -1.3
check_nbr(1.1, nbr1m, 1, [3], 0, false, 10, true);
var nbr2m = new Nbr(-10000002n, 10); // -10000002
check_nbr(1.2, nbr2m, 10000002n, [], 0, false, 10, true);
nbr2m = new Nbr(10000002n, 10, 0, true); // -10000002
check_nbr(1.3, nbr2m, 10000002n, [], 0, false, 10, true);
var nbr3 = new Nbr(-4, 10, [8,7,6,5,4,3,2,9], true); // 4.92345678
check_nbr(1.4, nbr3, 4, [8,7,6,5,4,3,2,9], 0, false, 10, false);
var nbr4 = new Nbr(4, 10, [8,7,6,5,4,3,2,9], false, 5); // 4.923(45678) = (492345678 - 4923) / (10^8 - 10^3)
check_nbr(1.5, nbr4, 4, [8,7,6,5,4,3,2,9], 5, false, 10, false);
var nbr5 = new Nbr(0, 10, [7,5,8,2,4,1], false, 6); // 0.(142857) = 1/7
check_nbr(1.6, nbr5, 0, [7,5,8,2,4,1], 6, false, 10, false);
var nbr9 = new Nbr(0, 10, [1,0,0,0,0,0], false); // 0.000001
check_nbr(1.7, nbr9, 0, [1,0,0,0,0,0], 0, false, 10, false);
// Test sub_magnitude_from_pwr_of_2
var nbr6 = new Nbr(1, 2); // 1 (base 2)
check_nbr(2.1, nbr6.sub_magnitude_from_pwr_of_2(8), 255, [], 0, false, 2, false); // 11111111 (base 2)
check_nbr(2.2, nbr6.sub_magnitude_from_pwr_of_2(8), 1, [], 0, false, 2, false); // 1 (base 2)
check_nbr(2.3, nbr6.sub_magnitude_from_pwr_of_2(1), 1, [], 0, false, 2, false); // 1 (base 2)
var nbr7 = new Nbr(0, 2, [1,1,0,0,0]); // 0.00011 (base 2)
check_nbr(2.4, nbr7.sub_magnitude_from_pwr_of_2(-2), 0, [1,0,1,0,0], 0, false, 2, false); // 0.00101 (base 2)
check_nbr(2.5, nbr7.sub_magnitude_from_pwr_of_2(-2), 0, [1,1,0,0,0], 0, false, 2, false); // 0.00011 (base 2)
nbr7.repetend_len = 4; // 0.0(0011) (base 2) = 0.1 (base 10)
check_nbr(2.6, nbr7.sub_magnitude_from_pwr_of_2(-2), 0, [1,0,0,1,0,0], 4, false, 2, false); // 0.00(1001) (base 2)
check_nbr(2.7, nbr7.sub_magnitude_from_pwr_of_2(-2), 0, [0,1,1,0,0,0], 4, false, 2, false); // 0.00(0110) (base 2)
var nbr8 = new Nbr(10, 2, [1,1,0,0,0], false, 4); // 1010.0(0011) (base 2) = 10.1 (base 10)
check_nbr(2.8, nbr8.sub_magnitude_from_pwr_of_2(8), 245, [0,0,1,1,1], 4, false, 2, false); // 11110101.1(1100) (base 2) = 245.9 (base 10)
// Test has_1_at_bit_pos
assert(3.1, !nbr8.has_1_at_bit_pos(8));
assert(3.2, nbr8.has_1_at_bit_pos(7));
assert(3.3, nbr8.has_1_at_bit_pos(0));
assert(3.4, nbr8.has_1_at_bit_pos(-1));
assert(3.5, nbr8.has_1_at_bit_pos(-2));
assert(3.6, nbr8.has_1_at_bit_pos(-3));
assert(3.7, !nbr8.has_1_at_bit_pos(-4));
assert(3.8, !nbr8.has_1_at_bit_pos(-5));
assert(3.9, !nbr8.has_1_at_bit_pos(-6));
// Test has_1_at_or_left_of_bit_pos
assert(4.1, !nbr8.has_1_at_or_left_of_bit_pos(8));
assert(4.2, nbr8.has_1_at_or_left_of_bit_pos(7));
assert(4.3, nbr8.has_1_at_or_left_of_bit_pos(1));
assert(4.4, nbr8.has_1_at_or_left_of_bit_pos(0));
assert(4.5, nbr8.has_1_at_or_left_of_bit_pos(-5));
assert(4.6, nbr8.has_1_at_or_left_of_bit_pos(-6));
// Test mul_pos_int
check_nbr(5.1, nbr1.mul_pos_int(9), 11, [7], 0, false, 10, false); // 11.7
check_nbr(5.2, nbr1.mul_pos_int(10), 13, [], 0, false, 10, false); // 13
check_nbr(5.3, nbr1m.mul_pos_int(35), 45, [5], 0, false, 10, true); // -45.5
check_nbr(5.4, nbr3.mul_pos_int(5), 24, [9,3,8,2,7,1,6], 0, false, 10, false); // 24.6172839
check_nbr(5.5, nbr4.mul_pos_int(5), 24, [2,9,3,8,2,7,1,6], 5, false, 10, false); // 24.617(28392)
// = (2461728392 - 24617) / (10^8 - 10^3)
// = 5 * (492345678 - 4923) / (10^8 - 10^3)
check_nbr(5.6, nbr4.mul_pos_int(32), 157, [0,1,7,1,6,0,5,5], 5, false, 10, false); // 157.550(61710)
// = (15755061710 - 157550) / (10^8 - 10^3)
// = 32 * (492345678 - 4923) / (10^8 - 10^3)
check_nbr(5.7, nbr5.mul_pos_int(2), 0, [4,1,7,5,8,2], 6, false, 10, false); // 0.(285714) = 2/7
check_nbr(5.8, nbr5.mul_pos_int(7), 1, [], 0, false, 10, false); // 1
// Test change_base
check_nbr(6.1, nbr5.change_base(7), 0, [1], 0, false, 7, false); // 0.1 (base 7)
check_nbr(6.2, nbr5.change_base(10), 0, [7,5,8,2,4,1], 6, false, 10, false); // 0.(142857) (base 10)
check_nbr(6.3, nbr9.change_base(16), 0, [
0xE,0x1,0x7,0x6,0xA,0x9,0x0,0xB,0xD,0xF,
0x4,0x6,0xC,0x8,0xF,0x0,0x3,0xB,0x7,0x9,
0xD,0x6,0x7,0xA,0xD,0xA,0x1,0x1,0x1,0xC,
0x8,0x3,0x8,0xB,0x4,0x7,0xC,0xC,0x9,0x7,
0x6,0xC,0xE,0xB,0x4,0x6,0x3,0xE,0x1,0xC,
0x6,0x1,0xB,0xB,0xD,0x7,0x6,0x5,0x9,0x9,
0x9,0x2,0xD,0xA,0xF,0xB,0x5,0x2,0x0,0x0,
0xF,0xF,0x4,0x9,0xA,0x2,0x1,0x5,0x6,0xF,
0x6,0x9,0x2,0x7,0xE,0xB,0x8,0xD,0xB,0x7,
0x1,0xF,0x5,0x4,0xB,0x7,0xC,0xB,0x0,0x9,
0xE,0x0,0xF,0x0,0x1,0x6,0xC,0xF,0x4,0x3,
0xE,0xE,0xD,0xC,0xF,0x6,0x8,0x9,0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5,0x7,0x8,
0x5,0x1,0xE,0x6,0x9,0xF,0x8,0x4,0xA,0x4,
0xA,0x7,0x8,0x1,0xA,0x5,0xD,0x9,0xC,0x9,
0x1,0xA,0x8,0xB,0x3,0xE,0xD,0x4,0xE,0x7,
0xB,0x8,0xE,0x4,0x6,0x9,0xA,0x5,0xF,0xE,
0x7,0x3,0xA,0xD,0x1,0x7,0x3,0xC,0xF,0xE,
0x6,0xA,0xB,0x5,0x6,0x7,0x8,0x8,0xF,0x7,
0x8,0xD,0x2,0xD,0x3,0xA,0x9,0xA,0xE,0x9,
0xC,0xC,0xF,0x3,0xA,0xF,0x6,0x2,0xD,0x4,
0x3,0x8,0x2,0xA,0x9,0x7,0x0,0x0,0xB,0x8,
0xC,0xF,0xA,0xF,0x1,0x2,0x6,0x3,0x8,0x5,
0x8,0x3,0x9,0x4,0x3,0xF,0x7,0xC,0x4,0xB,
0x6,0x3,0xD,0x8,0xD,0xE,0x5,0xB,0x0,0xA,
0x7,0xF,0x6,0xC,0x0,0x1,0x0,0x0,0x0,0x0], 0, true, 16, false); // incomplete .0.000010C6F... (base 16, 1000 digits)
// Test shift_left
check_nbr(7.1, nbr3.shift_left(3), 4923, [8,7,6,5,4], 0, false, 10, false); // 4923.45678
check_nbr(7.2, nbr3.shift_left(6), 4923456780n, [], 0, false, 10, false); // 4923456780
check_nbr(7.3, nbr3.shift_left(-6), 4923, [8,7,6,5,4], 0, false, 10, false); // 4923.45678
check_nbr(7.4, nbr3.shift_left(-5), 0, [8,7,6,5,4,3,2,9,4,0], 0, false, 10, false); // 0.0492345678
check_nbr(7.5, nbr4.shift_left(2), 492, [8,7,6,5,4,3], 5, false, 10, false); // 492.3(45678)
check_nbr(7.6, nbr4.shift_left(3), 492345, [5,4,8,7,6], 5, false, 10, false); // 492345.(67845)
check_nbr(7.7, nbr4.shift_left(7), 4923456784567n, [7,6,5,4,8], 5, false, 10, false); // 4923456784567.(84567)
check_nbr(7.8, nbr4.shift_left(-12), 4, [8,7,6,5,4,3,2,9], 5, false, 10, false); // 4.923(45678)
}
/**
* Returns a string representation of this Nbr.
* Nbr.conversion_config must be set before calling this method.
* Note: This method updates this Nbr's last_nbr_to_string_radix_pos and last_nbr_to_string_rest_len as a side
* effect.
*/
toString()
{
if (this.int_part === undefined)
{
this.last_nbr_to_string_radix_pos = 1;
this.last_nbr_to_string_rest_len = 0;
return '?';
}
// Create sign character if needed
var sign = (
Nbr.conversion_config.out_number_signed_type == ConversionConfig.SIGNED_TYPE.SIGNED && this.is_negative ?
'-' : '');
// Convert integer part to string
var s = this.int_part.toString(this.digit_base);
// If fixed width, prepend '0' digits
if (Nbr.conversion_config.out_numbers_are_fixed_width)
{
if (s.length <= Nbr.conversion_config.out_number_n_int_digits)
{
s = s.padStart(Nbr.conversion_config.out_number_n_int_digits, '0');
}
else
{
s = '#'.repeat(Nbr.conversion_config.out_number_n_int_digits);
}
}
// Set the position of the radix char to the end of the integer part
this.last_nbr_to_string_radix_pos = s.length;
// Add the radix char and fractional part if there is one
if (Nbr.conversion_config.out_number_has_frac &&
(this.frac_digit_vec.length > 0 || Nbr.conversion_config.out_numbers_are_fixed_width))
{
// Convert the fractional digits
var s2 = '';
for (var i = this.frac_digit_vec.length - 1; i >= 0; i--)
{
if (i + 1 == this.repetend_len)
{
s2 += Nbr.conversion_config.out_number_repetend_prefix;
}
s2 += this.frac_digit_vec[i].toString(this.digit_base);
}
if (this.repetend_len > 0)
{
s2 += Nbr.conversion_config.out_number_repetend_suffix;
}
// Add zero padding if fixed width
if (Nbr.conversion_config.out_numbers_are_fixed_width)
{
s2 = s2.padEnd(Nbr.conversion_config.out_number_n_frac_digits, '0');
}
// Append the radix char and fractional digits to the integer digits
s += Nbr.conversion_config.out_radix_char + s2;
}
// Prepare the full prefix consisting of the incomplete marker, sign, and user defined prefix
var full_prefix = (this.is_incomplete ? '\u2248' : '') +
sign +
Nbr.conversion_config.out_number_prefix;
// Convert all digits to uppercase, prepend the full prefix, and append the user defined suffix
s = full_prefix + s.toUpperCase() + Nbr.conversion_config.out_number_suffix;
// Add the length of the full prefix to the position of the radix char in the output string, and calculate the
// length of the rest of the string
this.last_nbr_to_string_radix_pos += full_prefix.length;
this.last_nbr_to_string_rest_len = s.length - this.last_nbr_to_string_radix_pos;
return s;
}
/**
* Returns true if this Nbr is zero, otherwise false. Works also for non-normalized Nbrs.
*/
is_zero()
{
return this.int_part == 0 && this.frac_digit_vec.every(x => x == 0);
}
/**
* Normalizes this Nbr and also returns the result. Normalization removes unnecessary fractional zeros, corrects -0
* to +0, and eliminates the repetend if it is all zeros or all max digits, but does not shorten the repetend or
* repetend prefix (this is not a problem, since change_base always creates minimal repetends and prefixes, and add
* and sub reject all repetends).
*/
normalize()
{
if (this.int_part !== undefined)
{
if (this.repetend_len > 0)
{
var repetend = this.frac_digit_vec.subarray(0, this.repetend_len);
var is_largest_repetend = false;
// If the repetend is the smallest possible (all zeros) or largest possible (all max digits, e.g. nines
// in base 10) then remove it, and increment the rest of the magnitude if the repetend was the largest
// possible
if (repetend.every(x => x == 0) || (is_largest_repetend = repetend.every(x => x == this.digit_base - 1)))
{
this.frac_digit_vec = this.frac_digit_vec.slice(this.repetend_len);
this.repetend_len = 0;
if (is_largest_repetend)
{
this.inc_magnitude();
}
}
}
if (this.repetend_len == 0)
{
// Find index of first non-zero digit at start of frac_digit_vec (i.e. skip trailing zeros)
var start_i = this.frac_digit_vec.findIndex(x => x != 0);
// If no non-zero digit found, then set index of first non-zero digit beyond end of frac_digit_vec
if (start_i == -1) start_i = this.frac_digit_vec.length;
// Cut away the trailing zeros if any
if (start_i > 0)
{
this.frac_digit_vec = this.frac_digit_vec.slice(start_i);
}
// if the Nbr is zero, make it non-negative
if (this.int_part == 0 && this.frac_digit_vec.length == 0)
{
this.is_negative = false;
}
}
}
return this;
}
/**
* Converts this Nbr to a new base and optionally limits the number of fractional digits (default is unlimited using
* a repetend if needed). Also returns the converted Nbr. The result is normalized, but the input Nbr doesn't need
* to be.
*/
change_base(new_digit_base, new_n_frac_digits)
{
if (this.int_part === undefined)
{
return this;
}
// Make a copy of the existing fractional part including any repetend
var remaining_frac = new Nbr(0, this.digit_base, this.frac_digit_vec, false, this.repetend_len);
// Set the new base and clear the repetend of the result - leave the integer part and sign unchanged - the
// fractional part will be set later
this.digit_base = new_digit_base;
this.repetend_len = 0;
// Convert fractional part by repeatedly "left-shifting" the remaining fractional part (in the old base) by 1
// new digit position, clipping off the integer overflow, and storing it as the next digit of the result
if (new_n_frac_digits !== undefined)
{
this.frac_digit_vec = new Uint8Array(new_n_frac_digits);
for (var i = new_n_frac_digits - 1; i >= 0 && !remaining_frac.is_zero(); i--)
{
remaining_frac = remaining_frac.mul_pos_int(new_digit_base);
this.frac_digit_vec[i] = Number(remaining_frac.int_part);
remaining_frac.int_part = 0n;
}
// If the remaining fractional part still isn't zero, then perform rounding of the result