-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnum7.cpp
6210 lines (6178 loc) · 312 KB
/
num7.cpp
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
/** DEVELOPED ON AMD Ryzen 5 Mobile 3550H 16GB DDR4 DRAM AND WINDOWS 10 */
/**
* @file num7.cpp
* @author Giovanni Cipriani <[email protected]>
* @date 2024
* @brief num7 ISO C++14 Standard 64-BIT LIBRARY, ARBITRARY-PRECISION GENERAL PURPOSE ARITHMETIC-LOGIC DECIMAL CLASS FOR WINDOWS
*
* @see https://github.com/giocip/WINDOWS_num7
*/
#include "num7.h"
///////////////////////////////////////////////// num7 NAMESPACE START
namespace num7 { // STARTING CURLY BRACKET num7 namespace
/// COMMON DATA TYPE
//class NUM;
int Error = 0; //GLOBAL VARIABLE ERROR HANDLING => num7.cpp
//////////////////////////////////////////////// NUM IN-LINE FUNCTIONS /////////////////////////////////////////////////
/////// -2 ///////
NUM::NUM(long int n) {
static NUM num;
num = i64NUM(n);
this->C = num.C; num.C = NULL; this->E = num.E; this->S = num.S; this->CE = num.CE; num.CE = NULL;
this->len_I = num.len_I; this->len_F = num.len_F;
return;
}
/////// -1 ///////
NUM::NUM(long long int n) {
static NUM num;
num = i64NUM(n);
this->C = num.C; num.C = NULL; this->E = num.E; this->S = num.S; this->CE = num.CE; num.CE = NULL;
this->len_I = num.len_I; this->len_F = num.len_F;
return;
}
/////// 0 ///////
NUM::NUM(double n) {
this->S = 0; this->C = NULL; this->E = 0; this->CE = NULL; this->len_I = 0; this->len_F = 0;
char s[32];
snprintf(s, 32, "%.15f", n);
raise("double IS NOT CONSTRUCTION VALID DATA, use NUM.from_double() instead!", s);
*this = 0;
return;
}
/////// 1 ///////
NUM::NUM(int n) { //CONSTRUCTOR METHOD DEFAULT TO ZERO, int OVERLOADED
if (!n) {
char* str = (char*)malloc(8 * sizeof(char)); //NULL, DOT, 'e' //RAM DYNAMIC ALLOCATION
if (!str) raise_exit("OUT OF RAM MEMORY => NUM CONSTRUCTOR", "0 => ZERO");
strcpy(str, "0.0e0"); this->CE = str; //ZERO EQUALIZATION => 0.0e0
this->E = 0; this->S = 0; this->len_I = 1; this->len_F = 1;
this->C = (char*)malloc(4 * sizeof(char)); //RAM DYNAMIC ALLOCATION
if (!(this->C)) raise_exit("OUT OF RAM MEMORY => NUM CONSTRUCTOR", "0 => ZERO");
strcpy(this->C, "0.0");
return;
}
char** p;
const char* N = i32str(n);
char* str = num2exp(N);
if (n < 0) { strcpy(str, str + 1); this->CE = str; this->S = 1; }
else { this->CE = str; this->S = 0; }
p = split(str, "e");
if (!p) { raise("ARGUMENT VALUE, num2exp => NUM CONSTRUCTOR", str); *this = 0; return; }
this->C = (char*)malloc(((i64)strlen(p[0]) + 32) * sizeof(char)); //RAM DYNAMIC ALLOCATION
if (!(this->C)) raise_exit("OUT OF RAM MEMORY => NUM CONSTRUCTOR", N);
strcpy(this->C, p[0]);
this->E = (i64)atoll(p[1]); free(p[0]);
p = split(this->C, "."); this->len_I = (i64)strlen(p[0]); this->len_F = (i64)strlen(p[1]);
free(p[0]);
return;
//char s[50]; //INTEGER RESCUE CONSTRUCTOR (SLOWER)
//strcpy(s, i32str(n)); strcat(s, ".0");
//NUM num;
//num = s;
//this->C = num.C; num.C = NULL; this->E = num.E; this->S = num.S; this->CE = num.CE; num.CE = NULL;
//this->len_I = num.len_I; this->len_F = num.len_F;
}
/////// 2 ///////
NUM::NUM(string s) { //CONSTRUCTOR METHOD, string OVERLOADED
static NUM num;
num = s.c_str();
this->C = num.C; num.C = NULL; this->E = num.E; this->S = num.S; this->CE = num.CE; num.CE = NULL;
this->len_I = num.len_I; this->len_F = num.len_F;
}
/////// 3 ///////
NUM::NUM(const char* s) { //CONSTRUCTOR METHOD
//cout << "CONSTRUCTOR IN ACTION..." << endl;
static char** p; p = NULL;
static char* p2; p2 = NULL;
if (!s) { raise("ARGUMENT VALUE, NULL => NUM CONSTRUCTOR", "(null)"); *this = 0; return; } //RESET TO ZERO
char* str = (char*)malloc(((i64)strlen(s) + 32) * sizeof(char)); //NULL, SIGN, DOT, 'e', 'E', 20 DIGIT EXPONENT //RAM DYNAMIC ALLOCATION
if (!str) raise_exit("OUT OF RAM MEMORY => NUM CONSTRUCTOR", s);
strcpy(str, s);
strip(str, " \t\n"); //CLEAR TAB AND SPACE LEFT AND RIGHT
rm_c(str, '_'); //REMOVE DIGIT SEPARATOR CHARACTER (_)
if (str[0] == '-') { strcpy(str, str + 1); this->S = 1; } //CHECKING SIGN ...
else if (str[0] == '+') { strcpy(str, str + 1); this->S = 0; }
else this->S = 0;
if (is_strfmt_float(str)) {
stripf0(str);
if (this->S && !strcmp(str, "0.0")) { free(str); raise("ARGUMENT VALUE, ZERO CAN NOT BE SIGNED => NUM CONSTRUCTOR", s); *this = 0; return; } //CHECK -0.0
this->CE = str;
char* t = num2exp(str);
strcpy(str, t); free(t);
p = split(str, "e");
if (!p) { raise("ARGUMENT VALUE, num2exp => NUM CONSTRUCTOR", str); *this = 0; return; }
this->C = (char*)malloc(((i64)strlen(p[0]) + 32) * sizeof(char)); //RAM DYNAMIC ALLOCATION
if (!(this->C)) raise_exit("OUT OF RAM MEMORY => NUM CONSTRUCTOR", s);
strcpy(C, p[0]);
if (OFI31(p[1])) { raise("EXPONENT OVERFLOW => NUM CONSTRUCTOR", p[1]); *this = 0; return; }
this->E = (i64)atoll(p[1]); free(p[0]);
p = split(this->C, "."); this->len_I = (i64)strlen(p[0]); this->len_F = (i64)strlen(p[1]); free(p[0]);
return;
}
else if (is_strfmt_exp(str)) {
this->CE = str;
p2 = lower(str); strcpy(str, p2); free(p2); //'e' or 'E' ALLOWED ("-1e-6" OR "-1E-6")
p = split(str, "e");
if (!p) { raise("ARGUMENT VALUE, is_strfmt_exp => NUM CONSTRUCTOR", str); *this = 0; return; }
this->C = (char*)malloc(((i64)strlen(p[0]) + 32) * sizeof(char)); //RAM DYNAMIC ALLOCATION
if (!(this->C)) raise_exit("OUT OF RAM MEMORY => NUM CONSTRUCTOR", s);
strcpy(this->C, p[0]);
if (OFI31(p[1])) { raise("EXPONENT OVERFLOW => NUM CONSTRUCTOR", p[1]); *this = 0; return; }
this->E = (i64)atoll(p[1]); free(p[0]);
stripf0(this->C); //000100.00e-00300 => 100.0e-300
if (!strcmp(this->C, "0.0")) {
if (this->S) { //CHECK FOR INVALID => -0.0
free(str); free(this->C);
raise("ARGUMENT VALUE, ZERO CAN NOT BE SIGNED => NUM CONSTRUCTOR", s);
*this = 0;
return;
}
this->E = 0; //ZERO EQUALIZATION => 0.0e0
}
strcpy(this->CE, this->C); strcat(this->CE, "e"); strcat(this->CE, i64str(E));
p = split(this->C, ".");
this->len_I = (i64)strlen(p[0]);
this->len_F = (i64)strlen(p[1]);
free(p[0]);
return;
}
else { //CONSTRUCTOR ERROR DETECTION - RESET TO ZERO!
free(str);
raise("ARGUMENT VALUE => NUM CONSTRUCTOR", s);
char* str = (char*)malloc(8 * sizeof(char)); //NULL, DOT, 'e' //RAM DYNAMIC ALLOCATION
if (!str) raise_exit("OUT OF RAM MEMORY => NUM CONSTRUCTOR", "0 => ZERO");
strcpy(str, "0.0e0"); this->CE = str; //ZERO EQUALIZATION => 0.0e0
this->E = 0; this->S = 0; this->len_I = 1; this->len_F = 1;
this->C = (char*)malloc(4 * sizeof(char)); //RAM DYNAMIC ALLOCATION
if (!(this->C)) raise_exit("OUT OF RAM MEMORY => NUM CONSTRUCTOR", "0 => ZERO");
strcpy(this->C, "0.0");
return;
}
}
NUM::NUM(const NUM& arg) { //COPY CONSTRUCTOR METHOD USED WHEN A NUM VAR IS BOTH DECLARED AND BY A VARIABLE ASSIGNED. (ALSO BY A FUNCTION OR WHEN PASSING A FUNCTION ARGUMENT)
//cout << "COPY CONSTRUCTOR METHOD: " << endl; n.print();
C = (char*)malloc(((i64)strlen(arg.C) + 1) * sizeof(char)); //RAM DYNAMIC ALLOCATION
if (!C) raise_exit("OUT OF RAM MEMORY => NUM COPY CONSTRUCTOR", arg.CE);
CE = (char*)malloc(((i64)strlen(arg.CE) + 1) * sizeof(char)); //RAM DYNAMIC ALLOCATION
if (!CE) raise_exit("OUT OF RAM MEMORY => NUM COPY CONSTRUCTOR", arg.CE);
strcpy(C, arg.C); strcpy(CE, arg.CE); //1=NULL
S = arg.S; E = arg.E; len_I = arg.len_I; len_F = arg.len_F;
}
NUM::~NUM() {
free(C); free(CE); this->C = NULL; this->CE = NULL;
//cout << "DESTRUCTION ENDED!" << endl;
} //DESTRUCTOR METHOD
/// NUM IN-LINE /// ASSIGNMENT ALLOWS RIGHT OPERAND TO BE NUM, CODE: NUM a, b("3.14"); a = b; print("a = ", a, " b = "); print(b, "\n"); //a = 3.14 b = 3.14
NUM& NUM::operator=(NUM& b) {
if (this == &b) return *this;
free(C); free(CE); //FREE OLD RAM MEMORY
this->C = (char*)malloc(((i64)strlen(b.C) + 1) * sizeof(char)); //RAM DYNAMIC ALLOCATION
if (!C) raise_exit("OUT OF RAM MEMORY => NUM operator=", b.sprint_fields());
this->CE = (char*)malloc(((i64)strlen(b.CE) + 1) * sizeof(char)); //RAM DYNAMIC ALLOCATION
if (!CE) raise_exit("OUT OF RAM MEMORY => NUM operator=", b.sprint_fields());
strcpy(this->C, b.C); this->E = b.E; this->S = b.S; strcpy(this->CE, b.CE);//strcpy C CE
this->len_I = b.len_I; this->len_F = b.len_F;
return *this;
}
/// NUM IN-LINE /// ASSIGNMENT ALLOWS RIGHT OPERAND TO BE int, CODE: NUM a; a = -1234567890; print("a = ", a, "\n"); //a = -1234567890.0
NUM& NUM::operator=(int b) {
static char s[16];
strcpy(s, i32str(b)); strcat(s, ".0");
NUM B(s); //ALLOCATE NEW RAM MEMORY
free(C); free(CE); //FREE OLD RAM MEMORY
C = CE = NULL;
this->C = B.C; this->E = B.E; this->S = B.S; this->CE = B.CE; this->len_I = B.len_I; this->len_F = B.len_F;
B.C = NULL; B.CE = NULL; //FREE NEW RAM MEMORY DESTRUCTOR
return *this;
}
/// NUM IN-LINE /// ASSIGNMENT ALLOWS RIGHT OPERAND TO BE i64, CODE: NUM a; a = -9223372036854775807 - 1; print("a = ", a, "\n"); //a = -9223372036854775808.0
NUM& NUM::operator=(i64 b) {
static char s[32];
strcpy(s, i64str(b)); strcat(s, ".0");
NUM B(s); //ALLOCATE NEW RAM MEMORY
free(C); free(CE); //FREE OLD RAM MEMORY
C = CE = NULL;
this->C = B.C; this->E = B.E; this->S = B.S; this->CE = B.CE; this->len_I = B.len_I; this->len_F = B.len_F;
B.C = NULL; B.CE = NULL; //FREE NEW RAM MEMORY DESTRUCTOR
return *this;
}
/// NUM IN-LINE /// ASSIGNMENT ALLOWS RIGHT OPERAND TO BE STRING, CODE: NUM a; a = "-1234567890.0"; print("a = ", a, "\n"); //a = -1234567890.0
NUM& NUM::operator=(const char* b) {
NUM B(b); //ALLOCATE NEW RAM MEMORY
free(C); free(CE); //FREE OLD RAM MEMORY
C = CE = NULL;
this->C = B.C; this->E = B.E; this->S = B.S; this->CE = B.CE; this->len_I = B.len_I; this->len_F = B.len_F;
B.C = NULL; B.CE = NULL; //FREE NEW RAM MEMORY DESTRUCTOR
return *this;
}
/// NUM IN-LINE /// ASSIGNMENT ALLOWS RIGHT OPERAND TO BE string, CODE: NUM a; a = string("-1234567890.0"); print("a = ", a, "\n"); //a = -1234567890.0
NUM& NUM::operator=(string b) {
NUM B(b); //ALLOCATE NEW RAM MEMORY
free(C); free(CE); //FREE OLD RAM MEMORY
C = CE = NULL;
this->C = B.C; this->E = B.E; this->S = B.S; this->CE = B.CE; this->len_I = B.len_I; this->len_F = B.len_F;
B.C = NULL; B.CE = NULL; //FREE NEW RAM MEMORY DESTRUCTOR
return *this;
}
/// NUM IN-LINE /// BOOLEAN EVALUATION, CODE: NUM a = 0; print(a ? "true" : "false", "\n"); //false
NUM::operator bool() const {
return strcmp(this->C, "0.0") ? 1 : 0;
}
/// NUM IN-LINE /// UNARY OPERATOR PLUS (+), CODE: NUM a; a = 3; print("a = ", +a, "\n"); //a = 3.0
NUM& NUM::operator+() {
return *this;
}
/// NUM IN-LINE /// UNARY PRE-INCREMENT (++), CODE: NUM a; a = 3; print("a = ", ++a, "\n"); //a = 4.0
NUM& NUM::operator++() {
return *this = *this + 1;
}
/// NUM IN-LINE /// UNARY OPERATOR MINUS (-), CODE: NUM a; a = 3; print("a = ", -a, "\n"); //a = -3.0
NUM& NUM::operator-() {
this->S ? this->S = 0 : this->S = 1;
return *this;
}
/// NUM IN-LINE /// UNARY PRE-DECREMENT (--), CODE: NUM a; a = 3; print("a = ", --a, "\n"); //a = 2.0
NUM& NUM::operator--() {
return *this = *this - 1;
}
/// NUM IN-LINE /// EQUALITY (==), CODE: NUM a(2), b(add("1.9", "0.1")); print(a == b, "\n"); //1 (true)
int NUM::operator==(NUM& b) {
return eq(this, &b);
}
/// NUM IN-LINE /// EQUALITY (==), CODE: NUM a(1); a++; print(a == "2.0", "\n"); //1 (true)
int NUM::operator==(const char* b) {
NUM B(b);
return eq(this, &B);
}
/// NUM IN-LINE /// EQUALITY (==), CODE: NUM a(1); a++; print(a == string("2.0"), "\n"); //1 (true)
int NUM::operator==(string b) {
NUM B(b.c_str());
return eq(this, &B);
}
/// NUM IN-LINE /// EQUALITY (==), CODE: NUM a(1); a++; print(a == 2, "\n"); //1 (true)
int NUM::operator==(i64 b) {
NUM B(b);
return eq(this, &B);
}
/// NUM IN-LINE /// NOT EQUALITY, CODE: NUM a(-2), b(add("-1.9", "-0.1")); print(a != b, "\n"); //0 (false)
int NUM::operator!=(NUM& b) {
return ne(this, &b);
}
/// NUM IN-LINE /// NOT EQUALITY, CODE: NUM a("2.1"), b(add("2.0", "0.1")); print(a != b, "\n"); //0 (false)
int NUM::operator!=(const char* b) {
NUM B(b);
return ne(this, &B);
}
/// NUM IN-LINE /// NOT EQUALITY, CODE: NUM a(2); string b("2.1"); print(a != b, "\n"); //1 (true)
int NUM::operator!=(string b) {
NUM B(b);
return ne(this, &B);
}
/// NUM IN-LINE /// NOT EQUALITY, CODE: NUM a(2); i64 b(3); print(a != b, "\n"); //1 (true)
int NUM::operator!=(i64 b) {
NUM B(b);
return ne(this, &B);
}
int NUM::operator>(NUM& b) {
return gt(this, &b);
}
/// NUM IN-LINE /// GREATER THAN, CODE: NUM a(-2); print(a > "-2.1", "\n"); //1 (true)
int NUM::operator>(const char* b) {
NUM B(b);
return gt(this, &B);
}
/// NUM IN-LINE /// GREATER THAN, CODE: NUM a(-2); print(a > string("-2.1"), "\n"); //1 (true)
int NUM::operator>(string b) {
NUM B(b);
return gt(this, &B);
}
/// NUM IN-LINE /// GREATER THAN, CODE: NUM a(-2); print(a > -3, "\n"); //1 (true)
int NUM::operator>(i64 b) {
NUM B(b);
return gt(this, &B);
}
/// NUM IN-LINE /// LESS OR EQUAL THAN, CODE: NUM a(-2), b(sub("-1.9", "-0.2")); print(a <= b, "\n"); //1 (true)
int NUM::operator<=(NUM& b) {
return le(this, &b);
}
/// NUM IN-LINE /// LESS OR EQUAL THAN, CODE: NUM a(-2); print(a <= "2.0", "\n"); //1 (true)
int NUM::operator<=(const char* b) {
NUM B(b);
return le(this, &B);
}
/// NUM IN-LINE /// LESS OR EQUAL THAN, CODE: NUM a(-2); print(a <= string("-2.1"), "\n"); //0 (false)
int NUM::operator<=(string b) {
NUM B(b);
return le(this, &B);
}
/// NUM IN-LINE /// LESS OR EQUAL THAN, CODE: NUM a(-2); print(a <= -3, "\n"); //0 (false)
int NUM::operator<=(i64 b) {
NUM B(b);
return le(this, &B);
}
/// NUM IN-LINE /// LESS THAN, CODE: NUM a("-2.1"), b(sub("-1.9", "-0.2")); print(a < b, "\n"); //1 (true)
int NUM::operator<(NUM& b) {
return lt(this, &b);
}
/// NUM IN-LINE /// LESS THAN, CODE: NUM a("-2.1"); print(a < "2.09", "\n"); //1 (true)
int NUM::operator<(const char* b) {
NUM B(b);
return lt(this, &B);
}
/// NUM IN-LINE /// LESS THAN, CODE: NUM a("-2.1"); print(a < string("-2.9"), "\n"); //0 (false)
int NUM::operator<(string b) {
NUM B(b);
return lt(this, &B);
}
/// NUM IN-LINE /// LESS THAN, CODE: NUM a("-2.1"); print(a < -3, "\n"); //0 (false)
int NUM::operator<(i64 b) {
NUM B(b);
return lt(this, &B);
}
/// NUM IN-LINE /// GREATER OR EQUAL THAN, CODE: NUM a(-2), b(sub("-1.9", "-0.2")); print(a >= b, "\n"); //0 (false)
int NUM::operator>=(NUM& b) {
return ge(this, &b);
}
/// NUM IN-LINE /// GREATER OR EQUAL THAN, CODE: NUM a(-2); print(a >= "3.0", "\n"); //0 (false)
int NUM::operator>=(const char* b) {
NUM B(b);
return ge(this, &B);
}
/// NUM IN-LINE /// GREATER OR EQUAL THAN, CODE: NUM a("-2.0001"); print(a >= string("-2.0009"), "\n"); //1 (true)
int NUM::operator>=(string b) {
NUM B(b);
return ge(this, &B);
}
/// NUM IN-LINE /// GREATER OR EQUAL THAN, CODE: NUM a("-2.9999999999999999999"); print(a >= -3, "\n"); //1 (true)
int NUM::operator>=(i64 b) {
NUM B(b);
return ge(this, &B);
}
/// NUM IN-LINE /// ADDITION, CODE: NUM a("3.0"), b("3.14"), sum = a + b; print(sum, "\n"); //6.14
NUM& NUM::operator+(NUM& b) {
static NUM SUM;
if (!strcmp(this->C, "0.0")) return SUM = b;
if (!strcmp(b.C, "0.0")) return SUM = *this;
return SUM = addf_signed(this, &b);
}
/// NUM IN-LINE /// ADDITION, CODE: NUM a("3.0"); const char* b("3.14"); NUM sum = a + b; print(sum, "\n"); //6.14
NUM& NUM::operator+(const char* b) {
static NUM SUM;
NUM B(b);
if (!strcmp(this->C, "0.0")) return SUM = B;
if (!strcmp(B.C, "0.0")) return SUM = *this;
return addf_signed(this, &B);
}
/// NUM IN-LINE /// ADDITION, CODE: NUM a("3.0"); string b("3.14"); NUM sum = a + b; print(sum, "\n"); //6.14
NUM& NUM::operator+(string b) {
static NUM SUM;
NUM B(b);
if (!strcmp(this->C, "0.0")) return SUM = B;
if (!strcmp(B.C, "0.0")) return SUM = *this;
return addf_signed(this, &B);
}
/// NUM IN-LINE /// ADDITION, CODE: NUM a("3.14"); i64 b(3); NUM sum = a + b; print(sum, "\n"); //6.14
NUM& NUM::operator+(i64 b) {
static NUM SUM;
NUM B(b);
if (!strcmp(this->C, "0.0")) return SUM = B;
if (!b) return SUM = *this;
return addf_signed(this, &B);
}
/// NUM IN-LINE /// ADDITION AND ASSIGNMENT, CODE: NUM a("3.14"), b("3.06"); a += b; print(a, "\n"); //6.2
NUM& NUM::operator+=(NUM& b) {
NUM a(*this);
return *this = a + b;
}
/// NUM IN-LINE /// ADDITION AND ASSIGNMENT, CODE: NUM a("3.14"); a += "3.06"; print(a, "\n"); //6.2
NUM& NUM::operator+=(const char* b) {
NUM a(*this), B(b);
return *this = a + B;
}
/// NUM IN-LINE /// ADDITION AND ASSIGNMENT, CODE: NUM a("3.14"); a += string("3.06"); print(a, "\n"); //6.2
NUM& NUM::operator+=(string b) {
NUM a(*this), B(b);
return *this = a + B;
}
/// NUM IN-LINE /// ADDITION AND ASSIGNMENT, CODE: NUM a("3.14"); a += 3; print(a, "\n"); //6.14
NUM& NUM::operator+=(i64 b) {
NUM a(*this), B(b);
return *this = a + B;
}
/// NUM IN-LINE /// SUBTRACTION, CODE: NUM a("3.0"), b("3.14"), dif = a - b; print(dif, "\n"); //-0.14
NUM& NUM::operator-(NUM& b) {
static NUM DIF;
if (!*this) { if (strcmp(b.C, "0.0")) b.S = b.S ? 0 : 1; return DIF = b; } //FLIPPED SIGN (OPPOSED) -0.0 NOT ALLOWED!
if (!b) { return DIF = *this; }
return subf_signed(this, &b);
}
/// NUM IN-LINE /// SUBTRACTION, CODE: NUM a("3.0"), dif = a - "3.14"; print(dif, "\n"); //-0.14
NUM& NUM::operator-(const char* b) {
static NUM DIF;
NUM B(b);
if (!*this) { if (strcmp(B.C, "0.0")) B.S = B.S ? 0 : 1; return DIF = B; } //FLIPPED SIGN (OPPOSED) -0.0 NOT ALLOWED!
if (!B) { return DIF = *this; }
return subf_signed(this, &B);
}
/// NUM IN-LINE /// SUBTRACTION, CODE: NUM a("3.0"), dif = a - string("3.14"); print(dif, "\n"); //-0.14
NUM& NUM::operator-(string b) {
static NUM DIF;
NUM B(b);
if (!*this) { if (strcmp(B.C, "0.0")) B.S = B.S ? 0 : 1; return DIF = B; } //FLIPPED SIGN (OPPOSED) -0.0 NOT ALLOWED!
if (!B) { return DIF = *this; }
return subf_signed(this, &B);
}
/// NUM IN-LINE /// SUBTRACTION, CODE: NUM a("3.0"), dif = a - 3; print(dif, "\n"); //0.0
NUM& NUM::operator-(i64 b) {
static NUM DIF;
NUM B(b);
if (!*this) { if (strcmp(B.C, "0.0")) B.S = B.S ? 0 : 1; return DIF = B; } //FLIPPED SIGN (OPPOSED) -0.0 NOT ALLOWED!
if (!b) { return DIF = *this; }
return subf_signed(this, &B);
}
/// NUM IN-LINE /// SUBTRACTION AND ASSIGNMENT, CODE: NUM a("3.14"), b("3.06"); a -= b; print(a, "\n"); //0.08
NUM& NUM::operator-=(NUM& b) {
NUM a(*this);
return *this = a - b;
}
/// NUM IN-LINE /// SUBTRACTION AND ASSIGNMENT, CODE: NUM a("3.14"; a -= "3.06"; print(a, "\n"); //0.08
NUM& NUM::operator-=(const char* b) {
NUM a(*this), B(b);
return *this = a - B;
}
/// NUM IN-LINE /// SUBTRACTION AND ASSIGNMENT, CODE: NUM a("3.14"); a -= string("3.06"); print(a, "\n"); //0.08
NUM& NUM::operator-=(string b) {
NUM a(*this), B(b);
return *this = a - B;
}
/// NUM IN-LINE /// SUBTRACTION AND ASSIGNMENT, CODE: NUM a("3.14"); a -= 3; print(a, "\n"); //0.14
NUM& NUM::operator-=(i64 b) {
NUM a(*this), B(b);
return *this = a - B;
}
/// NUM IN-LINE /// MULTIPLICATION, CODE: NUM a("3.14"), b("2.71"), pro = a * b; print(pro, "\n"); //8.5094
NUM& NUM::operator*(NUM& b) {
static NUM ZERO, pro;
if (!strcmp(this->C, "0.0")) return ZERO;
if (!strcmp(b.C, "0.0")) return ZERO;
return pro = mulf_signed(this, &b);
}
/// NUM IN-LINE /// MULTIPLICATION, CODE: NUM a("3.14"), pro = a * "2.71"; print(pro, "\n"); //8.5094
NUM& NUM::operator*(const char* b) {
static NUM ZERO;
NUM B(b);
if (!strcmp(this->C, "0.0")) return ZERO;
if (!strcmp(B.C, "0.0")) return ZERO;
return mulf_signed(this, &B);
}
/// NUM IN-LINE /// MULTIPLICATION, CODE: NUM a("3.14"), pro = a * string("2.71"); print(pro, "\n"); //8.5094
NUM& NUM::operator*(string b) {
static NUM ZERO;
NUM B(b);
if (!strcmp(this->C, "0.0")) return ZERO;
if (!strcmp(B.C, "0.0")) return ZERO;
return mulf_signed(this, &B);
}
/// NUM IN-LINE /// MULTIPLICATION, CODE: NUM a("-3.14"), pro = a * -4; print(pro, "\n"); //12.56
NUM& NUM::operator*(i64 b) {
static NUM ZERO;
NUM B(b);
if (!strcmp(this->C, "0.0")) return ZERO;
if (!b) return ZERO;
return mulf_signed(this, &B);
}
/// NUM IN-LINE /// MULTIPLICATION AND ASSIGNMENT, CODE: NUM a("-3.14"), b("-2.71"); a *= b; print(a, "\n"); //8.5094
NUM& NUM::operator*=(NUM& b) {
NUM a(*this);
return *this = a * b;
}
/// NUM IN-LINE /// MULTIPLICATION AND ASSIGNMENT, CODE: NUM a("-3.14"); a *= "2.71"; print(a, "\n"); //-8.5094
NUM& NUM::operator*=(const char* b) {
NUM a(*this), B(b);
return *this = a * B;
}
/// NUM IN-LINE /// MULTIPLICATION AND ASSIGNMENT, CODE: NUM a("+3.14"); a *= string("-2.71"); print(a, "\n"); //-8.5094
NUM& NUM::operator*=(string b) {
NUM a(*this), B(b);
return *this = a * B;
}
/// NUM IN-LINE /// MULTIPLICATION AND ASSIGNMENT, CODE: NUM a("+3.14"); a *= -2; print(a, "\n"); //-6.28
NUM& NUM::operator*=(i64 b) {
NUM a(*this), B(b);
return *this = a * B;
}
/// NUM IN-LINE /// DIVISION, CODE: NUM a("3.14"), b("2.71"), quo = a / b; print(quo, "\n"); //1.15867158671586715867158671586715
NUM& NUM::operator/(NUM& b) {
static NUM ZERO;
if (!strcmp(this->C, "0.0")) return ZERO; //(*this) ZERO DIVIDEND => ZERO QUOTIENT
if (!strcmp(b.C, "0.0")) {
raise("DIVISION BY ZERO => operator/", b.sprint_fields());
return ZERO;
}
return divf_signed(this, &b);
}
/// NUM IN-LINE /// DIVISION, CODE: NUM a("3.14"), quo = a / "2.71" ; print(quo.round(), "\n"); //1.16
NUM& NUM::operator/(const char* b) {
static NUM ZERO;
NUM B(b);
if (!strcmp(this->C, "0.0")) return ZERO; //(*this) ZERO DIVIDEND => ZERO QUOTIENT
if (!strcmp(B.C, "0.0")) {
raise("DIVISION BY ZERO => operator/", B.sprint_fields());
return ZERO;
}
return divf_signed(this, &B);
}
/// NUM IN-LINE /// DIVISION, CODE: NUM a("-3.14"), quo = a / string("2.71") ; print(quo.round(), "\n"); //-1.16
NUM& NUM::operator/(string b) {
static NUM ZERO;
NUM B(b);
if (!strcmp(this->C, "0.0")) return ZERO; //(*this) ZERO DIVIDEND => ZERO QUOTIENT
if (!strcmp(B.C, "0.0")) {
raise("DIVISION BY ZERO => operator/", B.sprint_fields());
return ZERO;
}
return divf_signed(this, &B);
}
/// NUM IN-LINE /// DIVISION, CODE: NUM a("-3.14"), quo = a / -2; print(quo.round(), "\n"); //1.57
NUM& NUM::operator/(i64 b) {
static NUM ZERO;
NUM B(b);
if (!strcmp(this->C, "0.0")) return ZERO; //(*this) ZERO DIVIDEND => ZERO QUOTIENT
if (!b) {
raise("DIVISION BY ZERO => operator/", B.sprint_fields());
return ZERO;
}
return divf_signed(this, &B);
}
/// NUM IN-LINE /// DIVISION AND ASSIGNMENT, CODE: NUM a("3.14"), b("-8.0"); a /= b; print(a, "\n"); //-0.3925
NUM& NUM::operator/=(NUM& b) {
NUM a(*this);
return *this = a / b;
}
/// NUM IN-LINE /// DIVISION AND ASSIGNMENT, CODE: NUM a("-3.14"); a /= "-8.0"; print(a, "\n"); //0.3925
NUM& NUM::operator/=(const char* b) {
NUM a(*this), B(b);
return *this = a / B;
}
/// NUM IN-LINE /// DIVISION AND ASSIGNMENT, CODE: NUM a("-3.14"); a /= string("8.0"); print(a, "\n"); //-0.3925
NUM& NUM::operator/=(string b) {
NUM a(*this), B(b);
return *this = a / B;
}
/// NUM IN-LINE /// DIVISION AND ASSIGNMENT, CODE: NUM a("+3.14"); a /= +8; print(a, "\n"); //0.3925
NUM& NUM::operator/=(i64 b) {
NUM a(*this), B(b);
return *this = a / B;
}
/// NUM IN-LINE /// MODULUS, CODE: NUM a("3.14"), b("2.71"), rem = a % b; print(rem, "\n"); //0.43
NUM& NUM::operator%(NUM& b) {
static NUM ZERO;
if (!strcmp(this->C, "0.0")) return ZERO; //(*this) ZERO DIVIDEND => ZERO QUOTIENT
if (!strcmp(b.C, "0.0")) {
raise("DIVISION BY ZERO => operator%", b.sprint_fields());
return ZERO;
}
return num7::mod(*this, b);
}
/// NUM IN-LINE /// MODULUS, CODE: NUM a("3.14"), rem = a % "-3.00"; print(rem, "\n"); //0.14
NUM& NUM::operator%(const char* b) {
static NUM ZERO;
NUM B(b);
if (!strcmp(this->C, "0.0")) return ZERO; //(*this) ZERO DIVIDEND => ZERO QUOTIENT
if (!strcmp(B.C, "0.0")) {
raise("DIVISION BY ZERO => operator%", B.sprint_fields());
return ZERO;
}
return num7::mod(*this, B);
}
/// NUM IN-LINE /// MODULUS, CODE: NUM a("-3.14"), rem = a % string("-3.00"); print(rem, "\n"); //-0.14
NUM& NUM::operator%(string b) {
static NUM ZERO;
NUM B(b);
if (!strcmp(this->C, "0.0")) return ZERO; //(*this) ZERO DIVIDEND => ZERO QUOTIENT
if (!strcmp(B.C, "0.0")) {
raise("DIVISION BY ZERO => operator%", B.sprint_fields());
return ZERO;
}
return num7::mod(*this, B);
}
/// NUM IN-LINE /// MODULUS, CODE: NUM a("-11.00"), rem = a % 3; print(rem, "\n"); //-2.0
NUM& NUM::operator%(i64 b) {
static NUM ZERO;
NUM B(b);
if (!strcmp(this->C, "0.0")) return ZERO; //(*this) ZERO DIVIDEND => ZERO QUOTIENT
if (!b) {
raise("DIVISION BY ZERO => operator%", B.sprint_fields());
return ZERO;
}
return num7::mod(*this, B);
}
/// NUM IN-LINE /// MODULUS AND ASSIGNMENT, CODE: NUM a("3.14"), b("-8.0"); a %= b; print(a, "\n"); //3.14
NUM& NUM::operator%=(NUM& b) {
NUM a(*this);
return *this = a % b;
}
/// NUM IN-LINE /// MODULUS AND ASSIGNMENT, CODE: NUM a("13.14"); a %= "-8.0"; print(a, "\n"); //5.14
NUM& NUM::operator%=(const char* b) {
NUM a(*this), B(b);
return *this = a % B;
}
/// NUM IN-LINE /// MODULUS AND ASSIGNMENT, CODE: NUM a("-13.14"); a %= string("-8.0"); print(a, "\n"); //-5.14
NUM& NUM::operator%=(string b) {
NUM a(*this), B(b);
return *this = a % B;
}
/// NUM IN-LINE /// MODULUS AND ASSIGNMENT, CODE: NUM a("-25.00"); a %= 8; print(a, "\n"); //-1.0
NUM& NUM::operator%=(i64 b) {
NUM a(*this), B(b);
return *this = a % B;
}
/// NUM IN-LINE /// POWER, CODE: NUM a(2), b(128), power = a ^ b; print(power, "\n"); //340282366920938463463374607431768211456.0
NUM& NUM::operator^(NUM& e) {
if (!e.is_integer()) { raise("EXPONENT, MUST BE INTEGER VALUE => operator^ ", e.sprint_fields()); return *this; }
return pwrf_signed(this, e.to_i64());
}
/// NUM IN-LINE /// POWER, CODE: NUM a(2), power = a ^ "256.0"; print(power, "\n"); //115792089237316195423570985008687907853269984665640564039457584007913129639936.0
NUM& NUM::operator^(const char* e) {
NUM E(e);
if (!E.is_integer()) { raise("EXPONENT, MUST BE INTEGER VALUE => operator^ ", E.sprint_fields()); return *this; }
return pwrf_signed(this, E.to_i64());
}
/// NUM IN-LINE /// POWER, CODE: NUM a(2), power = a ^ string("512.0"); print(power, "\n"); //13407807929942597099574024998205846127479365820592393377723561443721764030073546976801874298166903427690031858186486050853753882811946569946433649006084096.0
NUM& NUM::operator^(string e) {
NUM E(e);
if (!E.is_integer()) { raise("EXPONENT, MUST BE INTEGER VALUE => operator^ ", E.sprint_fields()); return *this; }
return pwrf_signed(this, E.to_i64());
}
/// NUM IN-LINE /// POWER, CODE: NUM a(-2), power = a ^ -5; print(power, "\n"); //-0.03125
NUM& NUM::operator^(i64 e) {
return pwrf_signed(this, e);
}
/// NUM IN-LINE /// POWER AND ASSIGNMENT, CODE: NUM a("25.00"), b(-8); a ^= b; print(a, "\n"); //0.0000000000065536
NUM& NUM::operator^=(NUM& e) {
return *this = *this ^ e;
}
/// NUM IN-LINE /// POWER AND ASSIGNMENT, CODE: NUM a("25.00"), b(-8); a ^= b; print(a, "\n"); //0.0000000000065536
NUM& NUM::operator^=(const char* e) {
return *this = *this ^ e;
}
/// NUM IN-LINE /// POWER AND ASSIGNMENT, CODE: NUM a("25.00"); a ^= string("-8.0"); print(a, "\n"); //0.0000000000065536
NUM& NUM::operator^=(string e) {
return *this = *this ^ e;
}
/// NUM IN-LINE /// POWER AND ASSIGNMENT, CODE: NUM a("-25.00"); a ^= 8; print(a, "\n"); //152587890625.0
NUM& NUM::operator^=(i64 e) {
return *this = *this ^ e;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////// NUM IN-LINE HELPER FUNCTIONS /////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// NUM IN-LINE /// RETURN OBJECT *this COPIED ON b, CODE: NUM a("-3.14"), b("2.71"); print(b, " => "); a.copy(b); print(b, "\n"); //2.71 => -3.14
NUM& NUM::copy(NUM& b) {
return b = *this;
}
/// NUM IN-LINE /// RETURN OBJECT ADDRESS LIKE address(), CODE: NUM a(2 + 3); NUM* ap = a.id(); print(*ap, "\n"); //5.0
NUM* NUM::id() {
return this;
}
/// NUM IN-LINE /// RETURN OBJECT ADDRESS LIKE id(), CODE: NUM a(2 + 3); NUM* ap = a.address(); print(*ap, "\n"); //5.0
NUM* NUM::address() {
return this;
}
/// NUM IN-LINE /// NEED: free(p[0]) (C_I => p[0]) RETURN COEFFICIENT INTEGER PART OF NUM, CODE: NUM a("5.123"); char** p = a.I_(); a.print_fields(); print(p[0], "\n"); free(p[0]); //5
char** NUM::I_() {
return split(C, ".");;
}
/// NUM IN-LINE /// NEED: free(p[0]) (C_F => p[1]) RETURN COEFFICIENT FLOATING-POINT PART OF NUM, CODE: NUM a("5.123"); char** p = a.F_(); a.print_fields(); print(p[1], "\n"); free(p[0]); //123
char** NUM::F_() {
return split(C, ".");;
}
/// NUM IN-LINE /// NEED: free() RETURN NUM AS STORED EXPONENTIAL NUMBER STRING (SIGNED), CODE: NUM a("-5.123"); a.print_fields(); char* p = a.get_NUM(); print(p, "\n"); free(p); //-5.123e0
char* NUM::get_NUM() {
char* s = (char*)malloc(((i64)strlen(CE) + 2) * sizeof(char)); //SIGN + NULL //RAM DYNAMIC ALLOCATION
if (!s) raise_exit("OUT OF RAM MEMORY => get_NUM", s);
strcpy(s, S ? "-" : ""); strcat(s, CE);
return s;
}
/// NUM IN-LINE /// NEED: free() CONVERT NUM TO BINARY FORMAT NUMBER STRING (UNSIGNED) (LIKE to_bin(), CODE: NUM a("15.0"); a.print_fields(); char* ram = a.bits(); print(ram, "\n"); free(ram); //1111
char* NUM::bits(int bin) { //DEFAULT ARGs: (int bin = 1)
return this->to_bin(bin);
}
/// NUM IN-LINE /// NEED: free() RETURN NUM STRING FORMATTED WITH SEPARATORS AND FRACTIONAL PART d ZERO PADDED WHEN REQUIRED, CODE: NUM a("1003.2"); char* ram = a.format(); print(ram, "\n"); free(ram); //1,003.20
char* NUM::format(i64 d, char SEP1000, int SIGN) { //DEFAULT ARGs: (i64 d = 2, char SEP1000 = ',', int SIGN = 0)
return num7::format(*this, d, SEP1000, SIGN);
}
/// NUM IN-LINE /// NEED: free() RETURN Num STRING NUMBER FORMATTED WITH FRACTIONAL PART d ZERO PADDED WHEN REQUIRED, CODE: NUM a("1003.2"); char* ram = a.format0(); print(ram, "\n"); free(ram); //1003.20
char* NUM::format0(i64 d) { //DEFAULT ARGs: (i64 d = 2)
return num7::format0(*this, d);
}
/// NUM IN-LINE /// NEED: free() RETURN NUM INTEGER STRING WITHOUT .0 IF INTEGER VALUE, CODE: NUM a("3.0"); char *p = a.format_int(); print(p, "\n"); free(p); //3
char* NUM::format_int(void) {
return this->is_integer() ? this->format0(0) : this->to_string();
}
/// NUM IN-LINE /// BOOLEAN FOR ZERO NUMBER VALUE CHECK, CODE: NUM a(0); print(a.is_zero(), " "); a--; print(a.is_zero(), "\n"); //1 0
int NUM::is_zero() {
return num7::is_zero(this);
}
/// NUM IN-LINE /// BOOLEAN FOR NUMBER POSITIVE VALUE CHECK, CODE: NUM a(1); print(a.is_positive(), " "); a--; print(a.is_positive(), "\n"); //1 0
int NUM::is_positive() {
return num7::is_zero(this) || this->S ? 0 : 1;
}
/// NUM IN-LINE /// BOOLEAN FOR NUMBER NEGATIVE VALUE CHECK, CODE: NUM a(-1); print(a.is_negative(), " "); a++; print(a.is_negative(), "\n"); //1 0
int NUM::is_negative() {
return this->S && !num7::is_zero(this) ? 1 : 0;
}
/// NUM IN-LINE /// BOOLEAN FOR NUMBER INTEGER VALUE CHECK, CODE: NUM a("3.000001"); print(a.is_integer(), "\n"); //0
int NUM::is_integer() {
return num7::is_int(this);
}
/// NUM IN-LINE /// BOOLEAN FOR NUMBER FLOATING VALUE CHECK, CODE: NUM a("3.000001"); print(a.is_floating(), "\n"); //1
int NUM::is_floating() {
return num7::is_float(this);
}
/// NUM IN-LINE /// BOOLEAN FOR NUMBER EVEN VALUE CHECK (2.0), CODE: NUM a("4.00"); print(a.is_even(), "\n"); //1
int NUM::is_even() {
return num7::is_even(this);
}
/// NUM IN-LINE /// BOOLEAN FOR NUMBER ODD VALUE CHECK (3.0), CODE: NUM a("5.00"); print(a.is_odd(), "\n"); //1
int NUM::is_odd() {
return num7::is_odd(this);
}
/// NUM IN-LINE /// CHECKER FOR PRIME NUMBERS, CODE: NUM a("257.0"); print(a.is_prime() ? "is prime!" : "is NOT prime.", "\n"); //is prime!
int NUM::is_prime() {
return num7::is_prime(this);
}
/// NUM IN-LINE /// NEED: free() CONVERT NUM TO BINARY FORMAT NUMBER STRING (UNSIGNED), CODE: NUM a("5.00"); char* ram; ram = a.to_bin(); print(ram, "\n"); free(ram); //101
char* NUM::to_bin(int bin) { //DEFAULT ARGs: (int bin = 1)
if (this->S) { raise("NEGATIVE VALUE => to_bin ", this->sprint_fields()); return NULL; }
if (this->is_floating()) { raise("VALUE MUST BE INTEGER => to_bin ", this->sprint_fields()); return NULL; }
char* ram1 = exp2num(*this);
ram1[strlen(ram1) - 2] = '\0';
char* ram2 = num7::bits(ram1, bin); free(ram1);
return ram2;
}
/// NUM IN-LINE /// NEED: free() CONVERT NUM TO HEXADECIMAL FORMAT NUMBER STRING (UNSIGNED), CODE: NUM a("250.00"); char* ram; ram = a.to_hex(); print(ram, "\n"); free(ram); //FA
char* NUM::to_hex() {
if (this->S) { raise("NEGATIVE VALUE => to_hex ", this->sprint_fields()); return NULL; }
if (this->is_floating()) { raise("VALUE MUST BE INTEGER => to_bin ", this->sprint_fields()); return NULL; }
char* ram1 = exp2num(*this);
ram1[strlen(ram1) - 2] = '\0';
char* ram2 = num7::bits(ram1, false); free(ram1);
return ram2;
}
/// NUM IN-LINE /// RETURN SCIENTIFIC NOTATION NUMBER OBJECT, CODE: NUM a("123.0e45"), b("678.0e9"), c = a * b; NUM d = c.into_sci(); d.print_fields(); //S=0 CE=8.3394e58 C=8.3394 E=58 len_I=1 len_F=4 SCIENTIFIC NOTATION
NUM& NUM::into_sci() {
static NUM N;
char* n = exp2num(*this);
N = n; free(n);
return N;
}
/// NUM IN-LINE /// NORMALIZE OBJECT ITSELF INTO SCIENTIFIC NOTATION NUMBER , CODE: NUM a("123.0e45"), b("678.0e9"), c = a * b; c.into_exp(); c.print_fields(); //S=0 CE=8.3394e58 C=8.3394 E=58 len_I=1 len_F=4 SCIENTIFIC NOTATION
NUM& NUM::into_exp() {
char* n = exp2num(*this);
*this = n; free(n);
return *this;
}
/// NUM IN-LINE /// CONVERT NUM TO i32 (SIGNED 32-BIT INTEGER) RANGE BETWEEN -2147483648 TO 2147483647, CODE: NUM a("123.0e3"); int A = a.to_i32(); print(A, "\n"); //123000
i32 NUM::to_i32() {
static char* p, * ram;
static i32 L, result;
NUM MIN("-2147483648.0"), MAX("2147483647.0");
if (*this < MIN || *this > MAX) {
raise("VALUE, OUT OF RANGE (-2147483648 +2147483647) => to_i32", this->sprint_fields()); return result = 0;
}
ram = exp2num(*this);
L = (i32)strlen(ram);
if (strcmp(ram + L - 2, ".0")) { //STRING NUM MUST BE ".0" SUFFIXED
raise("VALUE, MUST BE INTEGER => to_i32", this->sprint_fields()); return result = 0;
}
result = (i32)strtoll(ram, &p, 10); free(ram);
return result;
}
/// NUM IN-LINE /// CONVERT NUM TO i64 (SIGNED 64-BIT INTEGER) RANGE BETWEEN -9223372036854775808 TO 9223372036854775807, CODE: NUM a("123.0e12"); i64 A = a.to_i64(); print(A, "\n"); //123000000000000
i64 NUM::to_i64() {
static char* p, * ram;
static i64 L, result;
NUM MIN("-9223372036854775808.0"), MAX("9223372036854775807.0");
if (*this < MIN || *this > MAX) {
raise("VALUE, OUT OF RANGE (-9223372036854775808 +9223372036854775807) => to_i64", this->sprint_fields()); return result = 0;
}
ram = exp2num(*this);
L = (i64)strlen(ram);
if (strcmp(ram + L - 2, ".0")) { //STRING NUM MUST BE ".0" SUFFIXED
raise("VALUE, MUST BE INTEGER => to_i64", this->sprint_fields()); return result = 0;
}
result = (i64)strtoll(ram, &p, 10); free(ram);
return result;
}
/// NUM IN-LINE /// CONVERT NUM TO I64 (UNSIGNED 64-BIT INTEGER) RANGE BETWEEN 0 TO 18446744073709551615, CODE: NUM a("18446744073709551614.0"); printf("%llu\n", a.to_I64() + 1); //18446744073709551615
I64 NUM::to_I64() {
static char* ram;
static I64 L, result;
NUM MIN("0.0"), MAX("18446744073709551615.0");
if (*this < MIN || *this > MAX) {
raise("VALUE, OUT OF RANGE (0-18446744073709551615) => to_I64", this->sprint_fields()); return result = 0;
}
ram = exp2num(*this);
L = (I64)strlen(ram);
if (strcmp(ram + L - 2, ".0")) { //STRING NUM MUST BE ".0" SUFFIXED
raise("VALUE, MUST BE INTEGER => to_I64", this->sprint_fields()); return result = 0;
}
ram[strlen(ram) - 2] = '\0';
result = (I64)I64int(ram); free(ram);
return result;
}
/// NUM IN-LINE /// NEED: free() CONVERT NUM TO INTERNAL EXPONENTIAL NOTATION NUMBER STRING, CODE: NUM a("-123.0e-3"); char* ram = a.to_exp(); print(ram, "\n"); free(ram); //-123.0e-3
char* NUM::to_exp() {
char* ram = (char*)malloc((len_I + len_F + 8) * sizeof(char)); //'.', 'e', '+', '-', 'NULL' //RAM DYNAMIC ALLOCATION
if (!ram) raise_exit("OUT OF RAM MEMORY => to_exp", this->sprint_fields());
strcpy(ram, S ? "-" : ""); strcat(ram, this->CE);
return ram;
}
/// NUM IN-LINE /// NEED: free() CONVERT NUM TO SCIENTIFIC NOTATION NUMBER STRING, CODE: NUM a("-123.0e-3"); char* ram = a.to_sci(); print(ram, "\n"); free(ram); //-1.23e-1
char* NUM::to_sci() {
static char* n;
n = exp2num(*this);
NUM N(n); free(n);
char* ram = (char*)malloc((len_I + len_F + 8) * sizeof(char)); //'.', 'e', '+', '-', 'NULL' //RAM DYNAMIC ALLOCATION
if (!ram) raise_exit("OUT OF RAM MEMORY => to_sci", this->sprint_fields());
strcpy(ram, S ? "-" : ""); strcat(ram, N.CE);
return ram;
}
/// NUM IN-LINE /// NEED: free() CONVERT NUM TO STANDARD NOTATION NUMBER STRING (SIGNED), CODE: NUM a("123.0e3"); char* ram = a.to_string(); print(ram, "\n"); free(ram); //123000.0
char* NUM::to_string() {
static char* ram;
ram = exp2num(*this);
return ram;
}
/// NUM IN-LINE => CONVERT NUM TO double TYPE (LOSS PRECISION) CODE: printf("%.9f\n", add("0.000000001", "0.000000002").to_double()); //0.000000003
double NUM::to_double() {
static char* ram;
static double d;
char* end;
ram = exp2num(*this);
d = strtod(ram, &end);
free(ram);
return d;
}
/// NUM IN-LINE /// PRINT NUM STANDARD FORMAT, CODE: NUM a("2.0"); a.print("\n"); //2.0
int NUM::print(const char* postfix, int SIGN) { //(const char* postfix = "", int SIGN = 0)
static int n;
char* t = exp2num(*this);
char* s = (char*)malloc((strlen(t) + 1) * sizeof(char)); //RAM DYNAMIC ALLOCATION
if (!s) raise_exit("OUT OF RAM MEMORY => print", this->sprint_fields());
strcpy(s, t); free(t);
if (SIGN) {
if (this->S && s[0] != '-') { n = printf("-%s%s", s, postfix); free(s); return n; }
else if (this->S) { n = printf("%s%s", s, postfix); free(s); return n; }
else if (strcmp(s, "0.0")) { n = printf("+%s%s", s, postfix); free(s); return n; }
n = printf("%s%s", s, postfix);
}
else n = printf("%s%s", s, postfix);
free(s);
return n;
}
/// NUM IN-LINE /// PRINT NUM EXPONENTIAL FORMAT (SIGNED), CODE: NUM a("-123.0e-3"); a.print_exp("\n"); //-123.0e-3
int NUM::print_exp(const char* postfix) { //DEFAULT ARGs: (const char* postfix = "")
return printf("%s", (string(S ? "-" : "") + CE + postfix).c_str());
}
/// NUM IN-LINE /// PRINT NUM FIELDS, CODE: NUM a("-123.0e-3"); a.print_fields(); //S=1 CE=123.0e-3 C=123.0 E=-3 len_I=3 len_F=1
int NUM::print_fields() {
string tot = string("S=") + i64str(S) + string(" CE=") + CE + string(" C=") + C;
tot += string(" E=") + i64str(E);
tot += string(" len_I=") + i64str(len_I);
tot += string(" len_F=") + i64str(len_F);
return printf("%s\n", tot.c_str());
}
/// NUM IN-LINE /// (DEBUG) RETURN STRING NUM STRUCTURE ATTRIBUTES, CODE: NUM a("-123.0e-3"); char* s = a.sprint_fields(); print(s, "\n"); //S=1 CE=123.0e-3 C=123.0 E=-3 len_I=3 len_F=1
char* NUM::sprint_fields() const {
static char buffer[128];
snprintf(buffer, 128, "S=%lld CE=%s C=%s E=%lld len_I=%lld len_F=%lld", (long long int)S, CE, C, (long long int)E, (long long int)len_I, (long long int)len_F);
buffer[127] = '\0'; //STRING TERMINATOR CONSTRAINT
return buffer;
}
/// NUM IN-LINE /// RETURN OBJECT TRUNCATION TO THE SPECIFIED NUMBER DECIMALS (LOSS PRECISION), CODE: NUM a("123.456"); a = a.trunk(2); print(a, "\n"); //123.45
NUM& NUM::trunk(i64 decs) { //DEFAULT ARGs: (i64 decs)
static NUM T;
i64 SIGN = this->S;
char* t0 = exp2str(CE);
char* t = trunks(t0, decs); free(t0);
char* ce = (char*)malloc(((i64)strlen(t) + (i64)strlen(i64str(this->E)) + 16) * sizeof(char)); //RAM DYNAMIC ALLOCATION
if (!ce) raise_exit("OUT OF RAM MEMORY => trunk", this->sprint_fields());
t0 = num2exp(t);
strcpy(ce, t0); free(t0);
T = ce; free(t); free(ce);
T.S = SIGN;
T.S = (strcmp(T.C, "0.0") ? T.S : 0);
return T;
}
/// NUM IN-LINE /// RETURN ROUND FLOOR OF THE OBJECT DECIMAL DIGITS TO THE SPECIFIED NUMBER (LOSS PRECISION), CODE: NUM a("123.456"); a = a.round_floor(0); print(a, "\n"); //123.0
NUM& NUM::round_floor(i64 decs) { //DEFAULT ARGs: (i64 decs = 0)
return trunk(decs);
}
/// NUM IN-LINE /// RETURN ROUND CEIL OF THE OBJECT TO THE SPECIFIED DIGIT NUMBER (LOSS PRECISION), CODE: NUM a("123.456"); a = a.round_ceil(0); print(a, "\n"); //124.0
NUM& NUM::round_ceil(i64 decs) { //DEFAULT ARGs: (i64 decs = 0)
static NUM t, t2;
NUM n_10("10.0");
//n_10 = (n_10 ^ decs);
n_10 = n_10.shift(decs - 1);
char* ce = exp2str(n_10.CE);
char* i = invfs(ce, decs < 0 ? -decs : decs); free(ce);
NUM e(i); free(i);
ce = exp2str(this->CE);
char* temp = trunks(ce, decs); free(ce);
n_10 = temp; free(temp);
t = addf(&n_10, &e);
t2 = addf(this, &e);
t.S = this->S;
return eq_abs(&t, &t2) ? t2 = *this : t;
}
/// NUM IN-LINE /// RETURN STANDARD ROUND OF THE OBJECT DECIMAL DIGITS TO THE SPECIFIED NUMBER (LOSS PRECISION), CODE: NUM a("000123.12345678900"); a.round(5).print("\n"); //123.12346
NUM& NUM::round(i64 d) { //DEFAULT ARGs: (i64 d = 2)
/**
* \brief (COMMON STANDARD) HALF-UP ROUNDING OF A FLOATING-POINT NUM OBJECT
* \param d DECIMALS
* \code
* RUN-TIME ERROR CHECK: YES
* NEED: NONE
* CALL: trunk
* TIME: [...]
* LIMIT: RAM MEMORY
*
* EXAMPLES:
* NUM a("000123.12345678900"); a.round(5).print("\n"); //123.12346
* NUM a("000123.12345678900"); a.round(0).print("\n"); //123.0
* NUM a("-000123.12345678900"); a.round(-2).print("\n"); //-100.0
* NUM a("-000123.12345678900"); a.round(-3).print("\n"); //-1000.0
* \endcode
*/
//*this = "-26.005"; //DEBUG
static NUM T;
NUM ten("10.0");
i64 SIGN = this->S;
T = *this; //OBJECT COPY
T.S = 0; //POSITIVE COMPUTATION...
//T = T + (ten ^ (-d)) * "0.5";
T = T + (ten.shift(-d - 1)) * "0.5";
T = T.trunk(d);
T.S = SIGN; //RESET SIGN
T.S = strcmp(T.C, "0.0") ? T.S : 0; //RESET SIGN ONLY FOR ZERO
return T;
}
/// NUM IN-LINE /// BANK ROUND OF THE OBJECT DECIMAL DIGITS TO THE SPECIFIED NUMBER (LOSS PRECISION), CODE: NUM a("000123.12345678900"); a.round_bank(4).print("\n"); //123.1234
NUM& NUM::round_bank(i64 d) { //DEFAULT ARGs: (i64 d = 2)
static char* as;
static char** asp;
static NUM result;
//*this = "250.995"; d = -2; //TEST CODE
result = *this; //SAVE THE OBJECT
result.S = 0; //POSITIVE COMPUTING...
as = exp2num(result);
asp = split(as, ".");
if (d > 0 && d >= (i64)strlen(asp[1])) { free(asp[0]); free(as); return result = *this; } //CHECKING DECIMAL PART AND RIGHT LIMIT
if (d > 0) { //DECIMAL PART ROUNDING HALF TO EVEN
if (!((asp[1][d - 1] - '0') % 2) && asp[1][d] == '5') { //75.305=>75.3 (-75.305=>-75.3) SYMMETRIC LOGIC (d=2)
free(asp[0]); free(as); //1.25=>1.2 (-1.25=>-1.2) (d=1)
NUM ten("10.0");
//result -= (ten ^ -d) * "0.5";
result -= ten.shift(-d - 1) * "0.5";
result = result.trunk(d);
result.S = this->S; //RESET ORIGINAL SIGN
if (!strcmp(result.C, "0.0")) result.S = 0; //ZERO WITHOUT MINUS SIGN
return result;
}
}
else if (d == 0) { //0.5=>0.0 (-0.5=>0.0) 2.5=>2.0 (-2.5=>-2.0) SYMMETRIC LOGIC (d=0)
i64 asp0_len = (i64)strlen(asp[0]);
if (!((asp[0][asp0_len - 1] - '0') % 2) && asp[1][0] == '5') {
free(asp[0]); free(as);