/
gnc-numeric.cpp
1296 lines (1172 loc) · 35.7 KB
/
gnc-numeric.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
/********************************************************************
* gnc-numeric.c -- an exact-number library for accounting use *
* Copyright (C) 2000 Bill Gribble *
* Copyright (C) 2004 Linas Vepstas <linas@linas.org> *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License as *
* published by the Free Software Foundation; either version 2 of *
* the License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License*
* along with this program; if not, contact: *
* *
* Free Software Foundation Voice: +1-617-542-5942 *
* 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
* Boston, MA 02110-1301, USA gnu@gnu.org *
* *
*******************************************************************/
extern "C"
{
#include "config.h"
#include <glib.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "qof.h"
}
#include <stdint.h>
#include <regex>
#include <sstream>
#include <cstdlib>
#include "gnc-numeric.hpp"
#include "gnc-rational.hpp"
static QofLogModule log_module = "qof";
static const int64_t pten[] = { 1, 10, 100, 1000, 10000, 100000, 1000000,
10000000, 100000000, 1000000000,
INT64_C(10000000000), INT64_C(100000000000),
INT64_C(1000000000000), INT64_C(10000000000000),
INT64_C(100000000000000),
INT64_C(10000000000000000),
INT64_C(100000000000000000),
INT64_C(1000000000000000000)};
#define POWTEN_OVERFLOW -5
int64_t
powten (unsigned int exp)
{
if (exp > 17)
exp = 17;
return pten[exp];
}
GncNumeric::GncNumeric(GncRational rr)
{
/* Can't use isValid here because we want to throw different exceptions. */
if (rr.num().isNan() || rr.denom().isNan())
throw std::underflow_error("Operation resulted in NaN.");
if (rr.num().isOverflow() || rr.denom().isOverflow())
throw std::overflow_error("Operation overflowed a 128-bit int.");
if (rr.num().isBig() || rr.denom().isBig())
{
GncRational reduced(rr.reduce());
rr = reduced.round_to_numeric(); // A no-op if it's already small.
}
m_num = static_cast<int64_t>(rr.num());
m_den = static_cast<int64_t>(rr.denom());
}
GncNumeric::GncNumeric(double d) : m_num(0), m_den(1)
{
if (isnan(d) || fabs(d) > 1e18)
{
std::ostringstream msg;
msg << "Unable to construct a GncNumeric from " << d << ".\n";
throw std::invalid_argument(msg.str());
}
constexpr auto max_denom = INT64_MAX / 10;
auto logval = log10(fabs(d));
int64_t den;
if (logval > 0.0)
den = powten(18 - static_cast<int>(floor(logval) + 1.0));
else
den = powten(17);
auto num = static_cast<int64_t>(floor(static_cast<double>(den) * d));
if (num == 0)
return;
GncNumeric q(num, den);
auto r = q.reduce();
m_num = r.num();
m_den = r.denom();
}
GncNumeric::GncNumeric(const std::string& str, bool autoround)
{
static const std::string numer_frag("(-?[0-9]+)");
static const std::string denom_frag("([0-9]+)");
static const std::string hex_frag("(0x[a-f0-9]+)");
static const std::string slash( "[ \\t]*/[ \\t]*");
/* The llvm standard C++ library refused to recognize the - in the
* numer_frag patter with the default ECMAScript syntax so we use the awk
* syntax.
*/
static const std::regex numeral(numer_frag, std::regex::awk);
static const std::regex hex(hex_frag, std::regex::awk);
static const std::regex numeral_rational(numer_frag + slash + denom_frag,
std::regex::awk);
static const std::regex hex_rational(hex_frag + slash + hex_frag,
std::regex::awk);
static const std::regex hex_over_num(hex_frag + slash + denom_frag,
std::regex::awk);
static const std::regex num_over_hex(numer_frag + slash + hex_frag,
std::regex::awk);
static const std::regex decimal(numer_frag + "[.,]" + denom_frag,
std::regex::awk);
std::smatch m;
/* The order of testing the regexes is from the more restrictve to the less
* restrictive, as less-restrictive ones will match patterns that would also
* match the more-restrictive and so invoke the wrong construction.
*/
if (str.empty())
throw std::invalid_argument("Can't construct a GncNumeric from an empty string.");
if (std::regex_search(str, m, hex_rational))
{
GncNumeric n(stoll(m[1].str(), nullptr, 16),
stoll(m[2].str(), nullptr, 16));
m_num = n.num();
m_den = n.denom();
return;
}
if (std::regex_search(str, m, hex_over_num))
{
GncNumeric n(stoll(m[1].str(), nullptr, 16),
stoll(m[2].str()));
m_num = n.num();
m_den = n.denom();
return;
}
if (std::regex_search(str, m, num_over_hex))
{
GncNumeric n(stoll(m[1].str()),
stoll(m[2].str(), nullptr, 16));
m_num = n.num();
m_den = n.denom();
return;
}
if (std::regex_search(str, m, numeral_rational))
{
GncNumeric n(stoll(m[1].str()), stoll(m[2].str()));
m_num = n.num();
m_den = n.denom();
return;
}
if (std::regex_search(str, m, decimal))
{
GncInt128 high(stoll(m[1].str()));
GncInt128 low(stoll(m[2].str()));
int64_t d = powten(m[2].str().length());
GncInt128 n = high * d + (high > 0 ? low : -low);
if (!autoround && n.isBig())
{
std::ostringstream errmsg;
errmsg << "Decimal string " << m[1].str() << "." << m[2].str()
<< "can't be represented in a GncNumeric without rounding.";
throw std::overflow_error(errmsg.str());
}
while (n.isBig() && d > 0)
{
n >>= 1;
d >>= 1;
}
if (n.isBig()) //Shouldn't happen, of course
{
std::ostringstream errmsg;
errmsg << "Decimal string " << m[1].str() << "." << m[2].str()
<< " can't be represented in a GncNumeric, even after reducing denom to " << d;
throw std::overflow_error(errmsg.str());
}
GncNumeric gncn(static_cast<int64_t>(n), d);
m_num = gncn.num();
m_den = gncn.denom();
return;
}
if (std::regex_search(str, m, hex))
{
GncNumeric n(stoll(m[1].str(), nullptr, 16),INT64_C(1));
m_num = n.num();
m_den = n.denom();
return;
}
if (std::regex_search(str, m, numeral))
{
GncNumeric n(stoll(m[1].str()), INT64_C(1));
m_num = n.num();
m_den = n.denom();
return;
}
std::ostringstream errmsg;
errmsg << "String " << str << " contains no recognizable numeric value.";
throw std::invalid_argument(errmsg.str());
}
GncNumeric::operator gnc_numeric() const noexcept
{
return {m_num, m_den};
}
GncNumeric::operator double() const noexcept
{
return static_cast<double>(m_num) / static_cast<double>(m_den);
}
GncNumeric
GncNumeric::operator-() const noexcept
{
GncNumeric b(*this);
b.m_num = - b.m_num;
return b;
}
GncNumeric
GncNumeric::inv() const noexcept
{
if (m_num == 0)
return *this;
if (m_num < 0)
return GncNumeric(-m_den, -m_num);
return GncNumeric(m_den, m_num);
}
GncNumeric
GncNumeric::abs() const noexcept
{
if (m_num < 0)
return -*this;
return *this;
}
GncNumeric
GncNumeric::reduce() const noexcept
{
return static_cast<GncNumeric>(GncRational(*this).reduce());
}
GncNumeric::round_param
GncNumeric::prepare_conversion(int64_t new_denom) const
{
if (new_denom == m_den || new_denom == GNC_DENOM_AUTO)
return {m_num, m_den, 0};
GncRational conversion(new_denom, m_den);
auto red_conv = conversion.reduce();
GncInt128 old_num(m_num);
auto new_num = old_num * red_conv.num();
auto rem = new_num % red_conv.denom();
new_num /= red_conv.denom();
if (new_num.isBig())
{
GncRational rr(new_num, new_denom);
GncNumeric nn(rr);
rr = rr.convert<RoundType::truncate>(new_denom);
return {static_cast<int64_t>(rr.num()), new_denom, 0};
}
return {static_cast<int64_t>(new_num),
static_cast<int64_t>(red_conv.denom()), static_cast<int64_t>(rem)};
}
int64_t
GncNumeric::sigfigs_denom(unsigned figs) const noexcept
{
int64_t num_abs{std::abs(m_num)};
bool not_frac = num_abs > m_den;
int64_t val{ not_frac ? num_abs / m_den : m_den / num_abs };
unsigned digits{};
while (val >= 10)
{
++digits;
val /= 10;
}
return not_frac ? powten(figs - digits - 1) : powten(figs + digits);
}
std::string
GncNumeric::to_string() const noexcept
{
std::ostringstream out;
out << *this;
return out.str();
}
GncNumeric
GncNumeric::to_decimal(unsigned int max_places) const
{
if (max_places > 17)
max_places = 17;
bool is_pwr_ten = true;
for (int pwr = 1; pwr <= 17 && m_den > powten(pwr); ++pwr)
if (m_den % powten(pwr))
{
is_pwr_ten = false;
break;
}
if (m_num == 0 || (is_pwr_ten && m_den < powten(max_places)))
return *this; // Nothing to do.
if (is_pwr_ten)
{
/* See if we can reduce m_num to fit in max_places */
auto excess = m_den / powten(max_places);
if (m_num % excess)
{
std::ostringstream msg;
msg << "GncNumeric " << *this
<< " could not be represented in " << max_places
<< " decimal places without rounding.\n";
throw std::range_error(msg.str());
}
return GncNumeric(m_num / excess, powten(max_places));
}
GncRational rr(*this);
rr = rr.convert<RoundType::never>(powten(max_places)); //May throw
/* rr might have gotten reduced a bit too much; if so, put it back: */
unsigned int pwr{1};
for (; pwr <= max_places && !(rr.denom() % powten(pwr)); ++pwr);
auto reduce_to = powten(pwr);
GncInt128 rr_num(rr.num()), rr_den(rr.denom());
if (rr_den % reduce_to)
{
auto factor(reduce_to / rr.denom());
rr_num *= factor;
rr_den *= factor;
}
while (rr_num % 10 == 0)
{
rr_num /= 10;
rr_den /= 10;
}
try
{
/* Construct from the parts to avoid the GncRational constructor's
* automatic rounding.
*/
return {static_cast<int64_t>(rr_num), static_cast<int64_t>(rr_den)};
}
catch (const std::invalid_argument& err)
{
std::ostringstream msg;
msg << "GncNumeric " << *this
<< " could not be represented as a decimal without rounding.\n";
throw std::range_error(msg.str());
}
catch (const std::overflow_error& err)
{
std::ostringstream msg;
msg << "GncNumeric " << *this
<< " overflows when attempting to convert it to decimal.\n";
throw std::range_error(msg.str());
}
}
void
GncNumeric::operator+=(GncNumeric b)
{
*this = *this + b;
}
void
GncNumeric::operator-=(GncNumeric b)
{
*this = *this - b;
}
void
GncNumeric::operator*=(GncNumeric b)
{
*this = *this * b;
}
void
GncNumeric::operator/=(GncNumeric b)
{
*this = *this / b;
}
int
GncNumeric::cmp(GncNumeric b)
{
if (m_den == b.denom())
{
auto b_num = b.num();
return m_num < b_num ? -1 : b_num < m_num ? 1 : 0;
}
GncRational an(*this), bn(b);
return an.cmp(bn);
}
GncNumeric
operator+(GncNumeric a, GncNumeric b)
{
if (a.num() == 0)
return b;
if (b.num() == 0)
return a;
GncRational ar(a), br(b);
auto rr = ar + br;
return static_cast<GncNumeric>(rr);
}
GncNumeric
operator-(GncNumeric a, GncNumeric b)
{
return a + (-b);
}
GncNumeric
operator*(GncNumeric a, GncNumeric b)
{
if (a.num() == 0 || b.num() == 0)
{
GncNumeric retval;
return retval;
}
GncRational ar(a), br(b);
auto rr = ar * br;
return static_cast<GncNumeric>(rr);
}
GncNumeric
operator/(GncNumeric a, GncNumeric b)
{
if (a.num() == 0)
{
GncNumeric retval;
return retval;
}
if (b.num() == 0)
throw std::underflow_error("Attempt to divide by zero.");
GncRational ar(a), br(b);
auto rr = ar / br;
return static_cast<GncNumeric>(rr);
}
template <typename T, typename I> T
convert(T num, I new_denom, int how)
{
auto rtype = static_cast<RoundType>(how & GNC_NUMERIC_RND_MASK);
unsigned int figs = GNC_HOW_GET_SIGFIGS(how);
auto dtype = static_cast<DenomType>(how & GNC_NUMERIC_DENOM_MASK);
bool sigfigs = dtype == DenomType::sigfigs;
if (dtype == DenomType::reduce)
num = num.reduce();
switch (rtype)
{
case RoundType::floor:
if (sigfigs)
return num.template convert_sigfigs<RoundType::floor>(figs);
else
return num.template convert<RoundType::floor>(new_denom);
case RoundType::ceiling:
if (sigfigs)
return num.template convert_sigfigs<RoundType::ceiling>(figs);
else
return num.template convert<RoundType::ceiling>(new_denom);
case RoundType::truncate:
if (sigfigs)
return num.template convert_sigfigs<RoundType::truncate>(figs);
else
return num.template convert<RoundType::truncate>(new_denom);
case RoundType::promote:
if (sigfigs)
return num.template convert_sigfigs<RoundType::promote>(figs);
else
return num.template convert<RoundType::promote>(new_denom);
case RoundType::half_down:
if (sigfigs)
return num.template convert_sigfigs<RoundType::half_down>(figs);
else
return num.template convert<RoundType::half_down>(new_denom);
case RoundType::half_up:
if (sigfigs)
return num.template convert_sigfigs<RoundType::half_up>(figs);
else
return num.template convert<RoundType::half_up>(new_denom);
case RoundType::bankers:
if (sigfigs)
return num.template convert_sigfigs<RoundType::bankers>(figs);
else
return num.template convert<RoundType::bankers>(new_denom);
case RoundType::never:
if (sigfigs)
return num.template convert_sigfigs<RoundType::never>(figs);
else
return num.template convert<RoundType::never>(new_denom);
default:
/* round-truncate just returns the numerator unchanged. The old
* gnc-numeric convert had no "default" behavior at rounding that
* had the same result, but we need to make it explicit here to
* run the rest of the conversion code.
*/
if (sigfigs)
return num.template convert_sigfigs<RoundType::truncate>(figs);
else
return num.template convert<RoundType::truncate>(new_denom);
}
}
/* =============================================================== */
/* This function is small, simple, and used everywhere below,
* lets try to inline it.
*/
GNCNumericErrorCode
gnc_numeric_check(gnc_numeric in)
{
if (G_LIKELY(in.denom != 0))
{
return GNC_ERROR_OK;
}
else if (in.num)
{
if ((0 < in.num) || (-4 > in.num))
{
in.num = (gint64) GNC_ERROR_OVERFLOW;
}
return (GNCNumericErrorCode) in.num;
}
else
{
return GNC_ERROR_ARG;
}
}
/* *******************************************************************
* gnc_numeric_zero_p
********************************************************************/
gboolean
gnc_numeric_zero_p(gnc_numeric a)
{
if (gnc_numeric_check(a))
{
return 0;
}
else
{
if ((a.num == 0) && (a.denom != 0))
{
return 1;
}
else
{
return 0;
}
}
}
/* *******************************************************************
* gnc_numeric_negative_p
********************************************************************/
gboolean
gnc_numeric_negative_p(gnc_numeric a)
{
if (gnc_numeric_check(a))
{
return 0;
}
else
{
if ((a.num < 0) && (a.denom != 0))
{
return 1;
}
else
{
return 0;
}
}
}
/* *******************************************************************
* gnc_numeric_positive_p
********************************************************************/
gboolean
gnc_numeric_positive_p(gnc_numeric a)
{
if (gnc_numeric_check(a))
{
return 0;
}
else
{
if ((a.num > 0) && (a.denom != 0))
{
return 1;
}
else
{
return 0;
}
}
}
/* *******************************************************************
* gnc_numeric_compare
* returns 1 if a>b, -1 if b>a, 0 if a == b
********************************************************************/
int
gnc_numeric_compare(gnc_numeric a, gnc_numeric b)
{
gint64 aa, bb;
if (gnc_numeric_check(a) || gnc_numeric_check(b))
{
return 0;
}
if (a.denom == b.denom)
{
if (a.num == b.num) return 0;
if (a.num > b.num) return 1;
return -1;
}
GncNumeric an (a), bn (b);
return an.cmp(bn);
}
/* *******************************************************************
* gnc_numeric_eq
********************************************************************/
gboolean
gnc_numeric_eq(gnc_numeric a, gnc_numeric b)
{
return ((a.num == b.num) && (a.denom == b.denom));
}
/* *******************************************************************
* gnc_numeric_equal
********************************************************************/
gboolean
gnc_numeric_equal(gnc_numeric a, gnc_numeric b)
{
if (gnc_numeric_check(a))
{
/* a is not a valid number, check b */
if (gnc_numeric_check(b))
/* Both invalid, consider them equal */
return TRUE;
else
/* a invalid, b valid */
return FALSE;
}
if (gnc_numeric_check(b))
/* a valid, b invalid */
return FALSE;
return gnc_numeric_compare (a, b) == 0;
}
/* *******************************************************************
* gnc_numeric_same
* would a and b be equal() if they were both converted to the same
* denominator?
********************************************************************/
int
gnc_numeric_same(gnc_numeric a, gnc_numeric b, gint64 denom,
gint how)
{
gnc_numeric aconv, bconv;
aconv = gnc_numeric_convert(a, denom, how);
bconv = gnc_numeric_convert(b, denom, how);
return(gnc_numeric_equal(aconv, bconv));
}
static int64_t
denom_lcd(gnc_numeric a, gnc_numeric b, int64_t denom, int how)
{
if (denom == GNC_DENOM_AUTO &&
(how & GNC_NUMERIC_DENOM_MASK) == GNC_HOW_DENOM_LCD)
{
GncInt128 ad(a.denom), bd(b.denom);
denom = static_cast<int64_t>(ad.lcm(bd));
}
return denom;
}
/* *******************************************************************
* gnc_numeric_add
********************************************************************/
gnc_numeric
gnc_numeric_add(gnc_numeric a, gnc_numeric b,
gint64 denom, gint how)
{
if (gnc_numeric_check(a) || gnc_numeric_check(b))
{
return gnc_numeric_error(GNC_ERROR_ARG);
}
denom = denom_lcd(a, b, denom, how);
try
{
if ((how & GNC_NUMERIC_DENOM_MASK) != GNC_HOW_DENOM_EXACT)
{
GncNumeric an (a), bn (b);
GncNumeric sum = an + bn;
return static_cast<gnc_numeric>(convert(sum, denom, how));
}
GncRational ar(a), br(b);
auto sum = ar + br;
if (denom == GNC_DENOM_AUTO &&
(how & GNC_NUMERIC_RND_MASK) != GNC_HOW_RND_NEVER)
return static_cast<gnc_numeric>(sum.round_to_numeric());
sum = convert(sum, denom, how);
if (sum.is_big() || !sum.valid())
return gnc_numeric_error(GNC_ERROR_OVERFLOW);
return static_cast<gnc_numeric>(sum);
}
catch (const std::overflow_error& err)
{
PWARN("%s", err.what());
return gnc_numeric_error(GNC_ERROR_OVERFLOW);
}
catch (const std::invalid_argument& err)
{
PWARN("%s", err.what());
return gnc_numeric_error(GNC_ERROR_ARG);
}
catch (const std::underflow_error& err)
{
PWARN("%s", err.what());
return gnc_numeric_error(GNC_ERROR_OVERFLOW);
}
catch (const std::domain_error& err)
{
PWARN("%s", err.what());
return gnc_numeric_error(GNC_ERROR_REMAINDER);
}
}
/* *******************************************************************
* gnc_numeric_sub
********************************************************************/
gnc_numeric
gnc_numeric_sub(gnc_numeric a, gnc_numeric b,
gint64 denom, gint how)
{
gnc_numeric nb;
if (gnc_numeric_check(a) || gnc_numeric_check(b))
{
return gnc_numeric_error(GNC_ERROR_ARG);
}
denom = denom_lcd(a, b, denom, how);
try
{
if ((how & GNC_NUMERIC_DENOM_MASK) != GNC_HOW_DENOM_EXACT)
{
GncNumeric an (a), bn (b);
auto sum = an - bn;
return static_cast<gnc_numeric>(convert(sum, denom, how));
}
GncRational ar(a), br(b);
auto sum = ar - br;
if (denom == GNC_DENOM_AUTO &&
(how & GNC_NUMERIC_RND_MASK) != GNC_HOW_RND_NEVER)
return static_cast<gnc_numeric>(sum.round_to_numeric());
sum = convert(sum, denom, how);
if (sum.is_big() || !sum.valid())
return gnc_numeric_error(GNC_ERROR_OVERFLOW);
return static_cast<gnc_numeric>(sum);
}
catch (const std::overflow_error& err)
{
PWARN("%s", err.what());
return gnc_numeric_error(GNC_ERROR_OVERFLOW);
}
catch (const std::invalid_argument& err)
{
PWARN("%s", err.what());
return gnc_numeric_error(GNC_ERROR_ARG);
}
catch (const std::underflow_error& err)
{
PWARN("%s", err.what());
return gnc_numeric_error(GNC_ERROR_OVERFLOW);
}
catch (const std::domain_error& err)
{
PWARN("%s", err.what());
return gnc_numeric_error(GNC_ERROR_REMAINDER);
}
}
/* *******************************************************************
* gnc_numeric_mul
********************************************************************/
gnc_numeric
gnc_numeric_mul(gnc_numeric a, gnc_numeric b,
gint64 denom, gint how)
{
if (gnc_numeric_check(a) || gnc_numeric_check(b))
{
return gnc_numeric_error(GNC_ERROR_ARG);
}
denom = denom_lcd(a, b, denom, how);
try
{
if ((how & GNC_NUMERIC_DENOM_MASK) != GNC_HOW_DENOM_EXACT)
{
GncNumeric an (a), bn (b);
auto prod = an * bn;
return static_cast<gnc_numeric>(convert(prod, denom, how));
}
GncRational ar(a), br(b);
auto prod = ar * br;
if (denom == GNC_DENOM_AUTO &&
(how & GNC_NUMERIC_RND_MASK) != GNC_HOW_RND_NEVER)
return static_cast<gnc_numeric>(prod.round_to_numeric());
prod = convert(prod, denom, how);
if (prod.is_big() || !prod.valid())
return gnc_numeric_error(GNC_ERROR_OVERFLOW);
return static_cast<gnc_numeric>(prod);
}
catch (const std::overflow_error& err)
{
PWARN("%s", err.what());
return gnc_numeric_error(GNC_ERROR_OVERFLOW);
}
catch (const std::invalid_argument& err)
{
PWARN("%s", err.what());
return gnc_numeric_error(GNC_ERROR_ARG);
}
catch (const std::underflow_error& err)
{
PWARN("%s", err.what());
return gnc_numeric_error(GNC_ERROR_OVERFLOW);
}
catch (const std::domain_error& err)
{
PWARN("%s", err.what());
return gnc_numeric_error(GNC_ERROR_REMAINDER);
}
}
/* *******************************************************************
* gnc_numeric_div
********************************************************************/
gnc_numeric
gnc_numeric_div(gnc_numeric a, gnc_numeric b,
gint64 denom, gint how)
{
if (gnc_numeric_check(a) || gnc_numeric_check(b))
{
return gnc_numeric_error(GNC_ERROR_ARG);
}
denom = denom_lcd(a, b, denom, how);
try
{
if ((how & GNC_NUMERIC_DENOM_MASK) != GNC_HOW_DENOM_EXACT)
{
GncNumeric an (a), bn (b);
auto quot = an / bn;
return static_cast<gnc_numeric>(convert(quot, denom, how));
}
GncRational ar(a), br(b);
auto quot = ar / br;
if (denom == GNC_DENOM_AUTO &&
(how & GNC_NUMERIC_RND_MASK) != GNC_HOW_RND_NEVER)
return static_cast<gnc_numeric>(quot.round_to_numeric());
quot = static_cast<gnc_numeric>(convert(quot, denom, how));
if (quot.is_big() || !quot.valid())
return gnc_numeric_error(GNC_ERROR_OVERFLOW);
return static_cast<gnc_numeric>(quot);
}
catch (const std::overflow_error& err)
{
PWARN("%s", err.what());
return gnc_numeric_error(GNC_ERROR_OVERFLOW);
}
catch (const std::invalid_argument& err)
{
PWARN("%s", err.what());
return gnc_numeric_error(GNC_ERROR_ARG);
}
catch (const std::underflow_error& err) //Divide by zero
{
PWARN("%s", err.what());
return gnc_numeric_error(GNC_ERROR_OVERFLOW);
}
catch (const std::domain_error& err)
{
PWARN("%s", err.what());
return gnc_numeric_error(GNC_ERROR_REMAINDER);
}
}
/* *******************************************************************
* gnc_numeric_neg
* negate the argument
********************************************************************/
gnc_numeric
gnc_numeric_neg(gnc_numeric a)
{
if (gnc_numeric_check(a))
{
return gnc_numeric_error(GNC_ERROR_ARG);
}
return gnc_numeric_create(- a.num, a.denom);
}
/* *******************************************************************
* gnc_numeric_abs
* return the absolute value of the argument
********************************************************************/
gnc_numeric
gnc_numeric_abs(gnc_numeric a)
{
if (gnc_numeric_check(a))
{
return gnc_numeric_error(GNC_ERROR_ARG);
}
return gnc_numeric_create(ABS(a.num), a.denom);
}
/* *******************************************************************
* gnc_numeric_convert
********************************************************************/
gnc_numeric
gnc_numeric_convert(gnc_numeric in, int64_t denom, int how)
{
try
{
return convert(GncNumeric(in), denom, how);
}
catch (const std::overflow_error& err)
{
return gnc_numeric_error(GNC_ERROR_OVERFLOW);
}
}
/* *******************************************************************
* reduce a fraction by GCF elimination. This is NOT done as a
* part of the arithmetic API unless GNC_HOW_DENOM_REDUCE is specified
* as the output denominator.
********************************************************************/
gnc_numeric
gnc_numeric_reduce(gnc_numeric in)
{
if (gnc_numeric_check(in))
{
return gnc_numeric_error(GNC_ERROR_ARG);
}
if (in.denom < 0) /* Negative denoms multiply num, can't be reduced. */
return in;
try
{
GncNumeric an (in);
return static_cast<gnc_numeric>(an.reduce());
}
catch (const std::overflow_error& err)
{