-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBigReal.cpp
2331 lines (1985 loc) · 55.3 KB
/
BigReal.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
/*
* /File: BigReal.cpp /
* /Brief: BigInt and BigReal
* /Creator: Leonid Artemev | Leartiz /
* /Date: 2020 /
*/
#include <utility>
#include <cmath>
#include <sstream>
#include <algorithm>
#include "BigReal.h"
#include "BigInt.h"
namespace lez
{
BigReal::BigReal() :
b_whole_part(1U, static_cast<std::int8_t>(0)), // zero
b_fractional_part(1U, static_cast<std::int8_t>(0)),
b_am_dig_after_com(start_amount_digits_after_comma()),
b_is_neg(false)
{
}
BigReal::BigReal(const BigReal& val_) :
b_whole_part(val_.b_whole_part),
b_fractional_part(val_.b_fractional_part),
b_am_dig_after_com(val_.b_am_dig_after_com),
b_is_neg(val_.b_is_neg)
{
}
BigReal::BigReal(BigReal&& val_) :
b_whole_part(std::move(val_.b_whole_part)),
b_fractional_part(std::move(val_.b_fractional_part)),
b_am_dig_after_com(val_.b_am_dig_after_com),
b_is_neg(val_.b_is_neg)
{
}
BigReal::BigReal(std::int32_t val_) :
BigReal()
{
*this = to_big_real(val_);
}
BigReal::BigReal(std::size_t val_) :
BigReal()
{
*this = to_big_real(val_);
}
BigReal::BigReal(std::int64_t val_) :
BigReal()
{
*this = to_big_real(val_);
}
BigReal::BigReal(std::uint64_t val_) :
BigReal()
{
*this = to_big_real(val_);
}
BigReal::BigReal(const BigInt& val_) :
BigReal()
{
*this = to_big_real(val_);
}
BigReal::BigReal(float val_) :
BigReal()
{
*this = to_big_real(val_);
}
BigReal::BigReal(double val_) :
BigReal()
{
*this = to_big_real(val_);
}
// ----------------------------------------------------------------------------
// assignment operator
BigReal& BigReal::operator=(std::int32_t val_)
{
*this = to_big_real(val_);
return *this;
}
BigReal& BigReal::operator=(std::size_t val_)
{
*this = to_big_real(val_);
return *this;
}
BigReal& BigReal::operator=(std::int64_t val_)
{
*this = to_big_real(val_);
return *this;
}
BigReal& BigReal::operator=(std::uint64_t val_)
{
*this = to_big_real(val_);
return *this;
}
BigReal& BigReal::operator=(const BigInt& val_)
{
*this = to_big_real(val_);
return *this;
}
BigReal& BigReal::operator=(float val_)
{
*this = to_big_real(val_);
return *this;
}
BigReal& BigReal::operator=(double val_)
{
*this = to_big_real(val_);
return *this;
}
BigReal& BigReal::operator=(BigReal val_)
{
BigReal::swap(*this, val_);
return *this;
}
// ----------------------------------------------------------------------------
// to type
std::int32_t BigReal::to_int32() const
{
BigInt bi;
BigInt::to_big_int(bi, to_string_whole_part());
return bi.to_int32();
}
std::size_t BigReal::to_size() const
{
BigInt bi;
BigInt::to_big_int(bi, to_string_whole_part());
return bi.to_size();
}
std::int64_t BigReal::to_int64() const
{
BigInt bi;
BigInt::to_big_int(bi, to_string_whole_part());
return bi.to_int64();
}
std::uint64_t BigReal::to_uint64() const
{
BigInt bi;
BigInt::to_big_int(bi, to_string_whole_part());
return bi.to_uint64();
}
BigInt BigReal::to_big_int() const
{
BigInt bi;
BigInt::to_big_int(bi, to_string_whole_part());
return bi;
}
float BigReal::to_float() const
{
std::stringstream ss(to_string());
float val = 0.0;
ss >> val;
return val;
}
double BigReal::to_double() const
{
std::stringstream ss(to_string());
double val = 0.0;
ss >> val;
return val;
}
// ----------------------------------------------------------------------------
// extra
std::size_t BigReal::amount_places() const
{
return b_whole_part.size() + b_fractional_part.size();
}
std::size_t BigReal::amount_places_whole_part() const
{
return b_whole_part.size();
}
std::size_t BigReal::amount_places_fractional_part() const
{
return b_fractional_part.size();
}
bool BigReal::is_neg() const
{
return b_is_neg;
}
bool BigReal::is_zero() const
{
for (const auto& val : b_whole_part)
if (val != static_cast<std::int8_t>(0))
return false;
for (const auto& val : b_fractional_part)
if (val != static_cast<std::int8_t>(0))
return false;
return true;
}
void BigReal::reverse_neg()
{
if (b_is_neg)
reset_neg();
else
set_neg();
}
void BigReal::set_neg()
{
b_is_neg = !is_zero();
}
void BigReal::reset_neg()
{
b_is_neg = false;
}
void BigReal::reset()
{
*this = BigReal();
}
// ----------------------------------------------------------------------------
// operators
BigReal::operator bool() const
{
return !is_zero();
}
BigReal::operator std::int32_t() const
{
return to_int32();
}
BigReal::operator std::size_t() const
{
return to_size();
}
BigReal::operator std::int64_t() const
{
return to_int64();
}
BigReal::operator std::uint64_t() const
{
return to_uint64();
}
BigReal::operator BigInt() const
{
return to_big_int();
}
BigReal::operator float() const
{
return to_float();
}
BigReal::operator double() const
{
return to_double();
}
// ----------------------------------------------------------------------------
// unary
const BigReal BigReal::operator-() const
{
BigReal br(*this);
br.b_is_neg = (br.b_is_neg) ? false : !br.is_zero();
return br;
}
const BigReal BigReal::operator+() const
{
return *this; // cp
}
BigReal& BigReal::operator--()
{
return operator-=(1);
}
BigReal& BigReal::operator++()
{
return operator+=(1);
}
const BigReal BigReal::operator--(int)
{
BigReal br = *this;
operator-=(1);
return br;
}
const BigReal BigReal::operator++(int)
{
BigReal br = *this;
operator+=(1);
return br;
}
// ----------------------------------------------------------------------------
// arithmetic +
const BigReal BigReal::operator+(std::int32_t r_val_) const
{
BigReal r_val = to_big_real(r_val_);
return operator+(r_val);
}
const BigReal BigReal::operator+(std::size_t r_val_) const
{
BigReal r_val = to_big_real(r_val_);
return operator+(r_val);
}
const BigReal BigReal::operator+(std::int64_t r_val_) const
{
BigReal r_val = to_big_real(r_val_);
return operator+(r_val);
}
const BigReal BigReal::operator+(std::uint64_t r_val_) const
{
BigReal r_val = to_big_real(r_val_);
return operator+(r_val);
}
const BigReal BigReal::operator+(const BigInt& r_val_) const
{
BigReal r_val = to_big_real(r_val_);
return operator+(r_val);
}
const BigReal BigReal::operator+(float r_val_) const
{
BigReal r_val = to_big_real(r_val_);
return operator+(r_val);
}
const BigReal BigReal::operator+(double r_val_) const
{
BigReal r_val = to_big_real(r_val_);
return operator+(r_val);
}
const BigReal BigReal::operator+(const BigReal& r_val_) const
{
BigReal res;
std::size_t max_am_dig_after_com = std::max(b_am_dig_after_com, r_val_.b_am_dig_after_com);
if (is_neg() == r_val_.is_neg()) // only pos or neg
{
res = addition_positive_or_negative_numbers(*this, r_val_); // without check
res.set_amount_digits_after_comma(max_am_dig_after_com);
return res;
}
if (!is_neg() && r_val_.is_neg())
{
res = subtraction(*this, r_val_); // pos + neg
res.set_amount_digits_after_comma(max_am_dig_after_com);
return res;
}
res = subtraction(r_val_, *this); // neg + pos
res.set_amount_digits_after_comma(max_am_dig_after_com);
return res;
}
BigReal BigReal::addition_positive_or_negative_numbers(const BigReal& l_val_, const BigReal& r_val_)
{
BigReal res;
BigInt l_val_fp, r_val_fp, res_fp;
BigInt l_val_wp, r_val_wp, res_wp;
// numbers as strings
std::string l_val_fp_str = l_val_.to_string_fractional_part();
std::string r_val_fp_str = r_val_.to_string_fractional_part();
append_end_zeros_by_general_size(l_val_fp_str, r_val_fp_str);
BigInt::to_big_int(l_val_fp, l_val_fp_str);
BigInt::to_big_int(r_val_fp, r_val_fp_str);
BigInt::to_big_int(l_val_wp, l_val_.to_string_unsigned_whole_part());
BigInt::to_big_int(r_val_wp, r_val_.to_string_unsigned_whole_part());
res_fp = l_val_fp + r_val_fp;
res_wp = l_val_wp + r_val_wp;
if (is_carry_after_addition_fractional_part(l_val_fp, r_val_fp, res_fp))
{
++res_wp;
res_fp = remove_first_number_from_big_int(res_fp);
}
res = to_big_real_from_big_int(res_wp, res_fp);
if (l_val_.is_neg() && r_val_.is_neg()) // should be equal
{
res.set_neg();
}
return res;
}
BigReal BigReal::subtraction(const BigReal& l_val_, const BigReal& r_val_)
{
BigReal res;
bool is_l_val_more = logical_more(l_val_ , r_val_);
if (is_l_val_more)
{
res = subtraction_in_order(l_val_, r_val_);
return res;
}
res = subtraction_in_order(r_val_, l_val_);
res.set_neg();
return res;
}
BigReal BigReal::subtraction_in_order(const BigReal& l_val_, const BigReal& r_val_)
{
BigReal res;
BigInt l_val_fp, r_val_fp, res_fp;
BigInt l_val_wp, r_val_wp, res_wp;
// numbers as strings
std::string l_val_fp_str = l_val_.to_string_fractional_part();
std::string r_val_fp_str = r_val_.to_string_fractional_part();
append_end_zeros_by_general_size(l_val_fp_str, r_val_fp_str);
BigInt::to_big_int(l_val_fp, l_val_fp_str);
BigInt::to_big_int(r_val_fp, r_val_fp_str);
BigInt::to_big_int(l_val_wp, l_val_.to_string_unsigned_whole_part());
BigInt::to_big_int(r_val_wp, r_val_.to_string_unsigned_whole_part());
bool is_carry = false;
if (r_val_fp > l_val_fp)
{
l_val_fp_str.insert(0U, 1U, '1');
BigInt::to_big_int(l_val_fp, l_val_fp_str);
is_carry = true;
--l_val_wp;
}
res_fp = BigInt::subtraction_without_remove_leading_zeros(l_val_fp, r_val_fp); // save leading zeros
if (is_carry)
res_fp = remove_first_number_from_big_int(res_fp);
res_wp = l_val_wp - r_val_wp;
res = to_big_real_from_big_int(res_wp, res_fp);
return res;
}
bool BigReal::is_carry_after_addition_fractional_part(const BigInt& l_val_,
const BigInt& r_val_,
const BigInt& res_)
{
std::size_t max_ap = std::max(l_val_.amount_places(), r_val_.amount_places());
if (res_.amount_places() > max_ap)
return true;
return false;
}
BigInt BigReal::remove_first_number_from_big_int(const BigInt& val_)
{
std::string val = val_.to_string();
std::size_t pos_for_remove = 0U;
if (val.front() == '-')
++pos_for_remove;
val.erase(pos_for_remove, 1U);
return BigInt::to_big_int(val);
}
BigReal BigReal::to_big_real_from_big_int(const BigInt& wp_val_, const BigInt& fp_val_)
{
std::string str_wp = wp_val_.to_string();
remove_leading_zeros(str_wp);
std::string str_fp = fp_val_.to_string_unsigned(); // only positive
remove_end_zeros(str_fp);
std::string str_res;
str_res += str_wp;
str_res += '.';
str_res += str_fp;
return BigReal::to_big_real(str_res);
}
// ----------------------------------------------------------------------------
// arithmetic -
const BigReal BigReal::operator-(std::int32_t r_val_) const
{
BigReal r_val = to_big_real(r_val_);
return operator-(r_val);
}
const BigReal BigReal::operator-(std::size_t r_val_) const
{
BigReal r_val = to_big_real(r_val_);
return operator-(r_val);
}
const BigReal BigReal::operator-(std::int64_t r_val_) const
{
BigReal r_val = to_big_real(r_val_);
return operator-(r_val);
}
const BigReal BigReal::operator-(std::uint64_t r_val_) const
{
BigReal r_val = to_big_real(r_val_);
return operator-(r_val);
}
const BigReal BigReal::operator-(const BigInt& r_val_) const
{
BigReal r_val = to_big_real(r_val_);
return operator-(r_val);
}
const BigReal BigReal::operator-(float r_val_) const
{
BigReal r_val = to_big_real(r_val_);
return operator-(r_val);
}
const BigReal BigReal::operator-(double r_val_) const
{
BigReal r_val = to_big_real(r_val_);
return operator-(r_val);
}
const BigReal BigReal::operator-(const BigReal& r_val_) const
{
BigReal cp_r_val = r_val_;
if (!is_neg() && !cp_r_val.is_neg())
{
cp_r_val.b_is_neg = true;
return operator+(cp_r_val);
}
if (is_neg() && cp_r_val.is_neg())
{
cp_r_val.b_is_neg = false;
return operator+(cp_r_val);
}
if (!is_neg() && cp_r_val.is_neg())
{
cp_r_val.b_is_neg = false;
return operator+(cp_r_val);
}
cp_r_val.b_is_neg = true;
return operator+(cp_r_val);
}
// ----------------------------------------------------------------------------
// with edit lval
BigReal& BigReal::operator+=(std::int32_t r_val_)
{
*this = this->operator+(r_val_);
return *this;
}
BigReal& BigReal::operator+=(std::size_t r_val_)
{
*this = this->operator+(r_val_);
return *this;
}
BigReal& BigReal::operator+=(std::int64_t r_val_)
{
*this = this->operator+(r_val_);
return *this;
}
BigReal& BigReal::operator+=(std::uint64_t r_val_)
{
*this = this->operator+(r_val_);
return *this;
}
BigReal& BigReal::operator+=(const BigInt& r_val_)
{
*this = this->operator+(r_val_);
return *this;
}
BigReal& BigReal::operator+=(float r_val_)
{
*this = this->operator+(r_val_);
return *this;
}
BigReal& BigReal::operator+=(double r_val_)
{
*this = this->operator+(r_val_);
return *this;
}
BigReal& BigReal::operator+=(const BigReal& r_val_)
{
*this = this->operator+(r_val_);
return *this;
}
BigReal& BigReal::operator-=(std::int32_t r_val_)
{
*this = this->operator-(r_val_);
return *this;
}
BigReal& BigReal::operator-=(std::size_t r_val_)
{
*this = this->operator-(r_val_);
return *this;
}
BigReal& BigReal::operator-=(std::int64_t r_val_)
{
*this = this->operator-(r_val_);
return *this;
}
BigReal& BigReal::operator-=(std::uint64_t r_val_)
{
*this = this->operator-(r_val_);
return *this;
}
BigReal& BigReal::operator-=(const BigInt& r_val_)
{
*this = this->operator-(r_val_);
return *this;
}
BigReal& BigReal::operator-=(float r_val_)
{
*this = this->operator-(r_val_);
return *this;
}
BigReal& BigReal::operator-=(double r_val_)
{
*this = this->operator-(r_val_);
return *this;
}
BigReal& BigReal::operator-=(const BigReal& r_val_)
{
*this = this->operator-(r_val_);
return *this;
}
// ----------------------------------------------------------------------------
// friend arithmetic + or -
const BigReal operator+(std::int32_t l_val_, const BigReal& r_val_)
{
return r_val_.operator+(l_val_);
}
const BigReal operator+(std::size_t l_val_, const BigReal& r_val_)
{
return r_val_.operator+(l_val_);
}
const BigReal operator+(std::int64_t l_val_, const BigReal& r_val_)
{
return r_val_.operator+(l_val_);
}
const BigReal operator+(std::uint64_t l_val_, const BigReal& r_val_)
{
return r_val_.operator+(l_val_);
}
const BigReal operator+(const BigInt& l_val_, const BigReal& r_val_)
{
return r_val_.operator+(l_val_);
}
const BigReal operator+(float l_val_, const BigReal& r_val_)
{
return r_val_.operator+(l_val_);
}
const BigReal operator+(double l_val_, const BigReal& r_val_)
{
return r_val_.operator+(l_val_);
}
const BigReal operator-(std::int32_t l_val_, const BigReal& r_val_)
{
BigReal l_val = BigReal::to_big_real(l_val_);
return l_val.operator-(r_val_);
}
const BigReal operator-(std::size_t l_val_, const BigReal& r_val_)
{
BigReal l_val = BigReal::to_big_real(l_val_);
return l_val.operator-(r_val_);
}
const BigReal operator-(std::int64_t l_val_, const BigReal& r_val_)
{
BigReal l_val = BigReal::to_big_real(l_val_);
return l_val.operator-(r_val_);
}
const BigReal operator-(std::uint64_t l_val_, const BigReal& r_val_)
{
BigReal l_val = BigReal::to_big_real(l_val_);
return l_val.operator-(r_val_);
}
const BigReal operator-(const BigInt& l_val_, const BigReal& r_val_)
{
BigReal l_val = BigReal::to_big_real(l_val_);
return l_val.operator-(r_val_);
}
const BigReal operator-(float l_val_, const BigReal& r_val_)
{
BigReal l_val = BigReal::to_big_real(l_val_);
return l_val.operator-(r_val_);
}
const BigReal operator-(double l_val_, const BigReal& r_val_)
{
BigReal l_val = BigReal::to_big_real(l_val_);
return l_val.operator-(r_val_);
}
// ----------------------------------------------------------------------------
// arithmetic *
const BigReal BigReal::operator*(std::int32_t r_val_) const
{
BigReal r_val = BigReal::to_big_real(r_val_);
return operator*(r_val);
}
const BigReal BigReal::operator*(std::size_t r_val_) const
{
BigReal r_val = BigReal::to_big_real(r_val_);
return operator*(r_val);
}
const BigReal BigReal::operator*(std::int64_t r_val_) const
{
BigReal r_val = BigReal::to_big_real(r_val_);
return operator*(r_val);
}
const BigReal BigReal::operator*(std::uint64_t r_val_) const
{
BigReal r_val = BigReal::to_big_real(r_val_);
return operator*(r_val);
}
const BigReal BigReal::operator*(const BigInt& r_val_) const
{
BigReal r_val = BigReal::to_big_real(r_val_);
return operator*(r_val);
}
const BigReal BigReal::operator*(float r_val_) const
{
BigReal r_val = BigReal::to_big_real(r_val_);
return operator*(r_val);
}
const BigReal BigReal::operator*(double r_val_) const
{
BigReal r_val = BigReal::to_big_real(r_val_);
return operator*(r_val);
}
const BigReal BigReal::operator*(const BigReal& r_val_) const
{
std::string l_val_str = to_string_whole_part() + to_string_fractional_part();
std::string r_val_str = r_val_.to_string_whole_part() + r_val_.to_string_fractional_part();
BigInt l_val = BigInt::to_big_int(l_val_str);
BigInt r_val = BigInt::to_big_int(r_val_str);
l_val *= r_val;
std::string res_str = l_val.to_string();
std::size_t offset = b_fractional_part.size() + r_val_.b_fractional_part.size();
set_comma_in_result_string_after_multiplication(res_str, offset);
std::size_t max_am_dig_after_com = std::max(b_am_dig_after_com, r_val_.b_am_dig_after_com);
BigReal res = BigReal::to_big_real(res_str);
res.set_amount_digits_after_comma(max_am_dig_after_com);
return res;
}
void BigReal::set_comma_in_result_string_after_multiplication(std::string& res_str_, std::size_t offset_from_end_for_com_)
{
if (res_str_ == "0") // private case
{
res_str_.insert(1U, ".0");
return;
}
if (res_str_.length() <= offset_from_end_for_com_)
{
res_str_.insert(0U, offset_from_end_for_com_ - res_str_.length(), '0');
res_str_.insert(0U, "0.");
return;
}
res_str_.insert(res_str_.length() - offset_from_end_for_com_, 1U, '.'); // it works?
}
// ----------------------------------------------------------------------------
// arithmetic /
const BigReal BigReal::operator/(std::int32_t r_val_) const
{
BigReal r_val = BigReal::to_big_real(r_val_);
return operator/(r_val);
}
const BigReal BigReal::operator/(std::size_t r_val_) const
{
BigReal r_val = BigReal::to_big_real(r_val_);
return operator/(r_val);
}
const BigReal BigReal::operator/(std::int64_t r_val_) const
{
BigReal r_val = BigReal::to_big_real(r_val_);
return operator/(r_val);
}
const BigReal BigReal::operator/(std::uint64_t r_val_) const
{
BigReal r_val = BigReal::to_big_real(r_val_);
return operator/(r_val);
}
const BigReal BigReal::operator/(const BigInt& r_val_) const
{
BigReal r_val = BigReal::to_big_real(r_val_);
return operator/(r_val);
}
const BigReal BigReal::operator/(float r_val_) const
{
BigReal r_val = BigReal::to_big_real(r_val_);
return operator/(r_val);
}
const BigReal BigReal::operator/(double r_val_) const
{
BigReal r_val = BigReal::to_big_real(r_val_);
return operator/(r_val);
}
const BigReal BigReal::operator/(const BigReal& r_val_) const
{
std::size_t max_size_fp = std::max(b_fractional_part.size(), r_val_.b_fractional_part.size());
std::string l_val_str = to_string_unsigned_whole_part() + to_string_fractional_part(); // unsigned
std::string r_val_str = r_val_.to_string_unsigned_whole_part() + r_val_.to_string_fractional_part();
append_end_zeros(l_val_str, max_size_fp - b_fractional_part.size());
append_end_zeros(r_val_str, max_size_fp - r_val_.b_fractional_part.size());
BigInt l_val = BigInt::to_big_int(l_val_str);
BigInt r_val = BigInt::to_big_int(r_val_str);
BigInt res_wp = l_val / r_val; // whole part, division by zero is zero
BigInt rem_of_div = l_val % r_val; // division by zero is zero
std::size_t counter_am_dig_after_com = 0U;
std::size_t max_am_dig_after_com = std::max(b_am_dig_after_com, r_val_.b_am_dig_after_com);
BigInt part_res_fp;
std::string res_fp_str; // fractional
while (counter_am_dig_after_com < max_am_dig_after_com && !rem_of_div.is_zero())
{
rem_of_div *= 10;
part_res_fp = rem_of_div / r_val; // 0-9
rem_of_div %= r_val;
res_fp_str.insert(res_fp_str.length(), part_res_fp.to_string());
++counter_am_dig_after_com;
}
if (res_fp_str.empty())
res_fp_str.push_back('0');
std::string res_str = res_wp.to_string() + "." + res_fp_str;
BigReal res = BigReal::to_big_real(res_str);
res.set_amount_digits_after_comma(max_am_dig_after_com);
if (b_is_neg == r_val_.b_is_neg)
{
res.b_is_neg = false;
return res;
}
res.b_is_neg = !res.is_zero();
return res;
}
// ----------------------------------------------------------------------------
// arithmetic %
const BigReal BigReal::operator%(std::int32_t r_val_) const
{
BigReal r_val = BigReal::to_big_real(r_val_);
return operator%(r_val);
}
const BigReal BigReal::operator%(std::size_t r_val_) const
{
BigReal r_val = BigReal::to_big_real(r_val_);
return operator%(r_val);
}
const BigReal BigReal::operator%(std::int64_t r_val_) const
{
BigReal r_val = BigReal::to_big_real(r_val_);
return operator%(r_val);
}
const BigReal BigReal::operator%(std::uint64_t r_val_) const
{
BigReal r_val = BigReal::to_big_real(r_val_);
return operator%(r_val);
}
const BigReal BigReal::operator%(const BigInt& r_val_) const
{
BigReal r_val = BigReal::to_big_real(r_val_);
return operator%(r_val);
}
const BigReal BigReal::operator%(float r_val_) const
{
BigReal r_val = BigReal::to_big_real(r_val_);
return operator%(r_val);
}
const BigReal BigReal::operator%(double r_val_) const
{
BigReal r_val = BigReal::to_big_real(r_val_);
return operator%(r_val);
}
const BigReal BigReal::operator%(const BigReal& r_val_) const
{
BigReal gen_res = operator/(r_val_); // if r_val_ is neg, then ge_res will be neg, division by zero is zero
gen_res.b_whole_part.clear();
gen_res.b_whole_part.push_back(static_cast<std::int8_t>(0)); // set zero
gen_res *= r_val_; // close to the remainder...
return gen_res; // nrvo
}
// ----------------------------------------------------------------------------
// arithmetic *, /, % for lval
BigReal& BigReal::operator*=(std::int32_t r_val_)
{
*this = this->operator*(r_val_);
return *this;
}
BigReal& BigReal::operator*=(std::size_t r_val_)
{
*this = this->operator*(r_val_);
return *this;
}
BigReal& BigReal::operator*=(std::int64_t r_val_)
{
*this = this->operator*(r_val_);
return *this;
}
BigReal& BigReal::operator*=(std::uint64_t r_val_)
{
*this = this->operator*(r_val_);
return *this;
}
BigReal& BigReal::operator*=(const BigInt& r_val_)
{
*this = this->operator*(r_val_);
return *this;
}