GnuCash 7574033+
Loading...
Searching...
No Matches
Gnc_numeric_arithmetic_operators

Normal arithmetic operators. More...

Functions

GncNumeric operator+ (GncNumeric a, GncNumeric b)
GncNumeric operator+ (GncNumeric a, int64_t b)
GncNumeric operator+ (int64_t a, GncNumeric b)
GncNumeric operator- (GncNumeric a, GncNumeric b)
GncNumeric operator- (GncNumeric a, int64_t b)
GncNumeric operator- (int64_t a, GncNumeric b)
GncNumeric operator* (GncNumeric a, GncNumeric b)
GncNumeric operator* (GncNumeric a, int64_t b)
GncNumeric operator* (int64_t a, GncNumeric b)
GncNumeric operator/ (GncNumeric a, GncNumeric b)
GncNumeric operator/ (GncNumeric a, int64_t b)
GncNumeric operator/ (int64_t a, GncNumeric b)

Detailed Description

Normal arithmetic operators.

The class arithmetic operators are implemented in terms of these operators. They use GncRational operators internally then call the GncNumeric(GncRational&) constructor which will silently round half-down to obtain int64_t members.

These operators can throw std::overflow_error, std::underflow_error, or std::invalid argument as indicated in the class GncNumeric documentation.

Parameters
aThe right-side operand
bThe left-side operand
Returns
A GncNumeric computed from the operation.

Function Documentation

◆ operator*() [1/3]

GncNumeric operator* ( GncNumeric a,
GncNumeric b )

Definition at line 637 of file gnc-numeric.cpp.

638{
639 if (a.num() == 0 || b.num() == 0)
640 {
641 GncNumeric retval;
642 return retval;
643 }
644 GncRational ar(a), br(b);
645 auto rr = ar * br;
646 return static_cast<GncNumeric>(rr);
647}
The primary numeric class for representing amounts and values.
int64_t num() const noexcept
Accessor for numerator value.
Rational number class using GncInt128 for the numerator and denominator.

◆ operator*() [2/3]

GncNumeric operator* ( GncNumeric a,
int64_t b )
inline

Definition at line 319 of file gnc-numeric.hpp.

320{
321 return a * GncNumeric(b, 1);
322}

◆ operator*() [3/3]

GncNumeric operator* ( int64_t a,
GncNumeric b )
inline

Definition at line 323 of file gnc-numeric.hpp.

324{
325 return b * GncNumeric(a, 1);
326}

◆ operator+() [1/3]

GncNumeric operator+ ( GncNumeric a,
GncNumeric b )

Definition at line 619 of file gnc-numeric.cpp.

620{
621 if (a.num() == 0)
622 return b;
623 if (b.num() == 0)
624 return a;
625 GncRational ar(a), br(b);
626 auto rr = ar + br;
627 return static_cast<GncNumeric>(rr);
628}

◆ operator+() [2/3]

GncNumeric operator+ ( GncNumeric a,
int64_t b )
inline

Definition at line 301 of file gnc-numeric.hpp.

302{
303 return a + GncNumeric(b, 1);
304}

◆ operator+() [3/3]

GncNumeric operator+ ( int64_t a,
GncNumeric b )
inline

Definition at line 305 of file gnc-numeric.hpp.

306{
307 return b + GncNumeric(a, 1);
308}

◆ operator-() [1/3]

GncNumeric operator- ( GncNumeric a,
GncNumeric b )

Definition at line 631 of file gnc-numeric.cpp.

632{
633 return a + (-b);
634}

◆ operator-() [2/3]

GncNumeric operator- ( GncNumeric a,
int64_t b )
inline

Definition at line 310 of file gnc-numeric.hpp.

311{
312 return a - GncNumeric(b, 1);
313}

◆ operator-() [3/3]

GncNumeric operator- ( int64_t a,
GncNumeric b )
inline

Definition at line 314 of file gnc-numeric.hpp.

315{
316 return b - GncNumeric(a, 1);
317}

◆ operator/() [1/3]

GncNumeric operator/ ( GncNumeric a,
GncNumeric b )

Definition at line 650 of file gnc-numeric.cpp.

651{
652 if (a.num() == 0)
653 {
654 GncNumeric retval;
655 return retval;
656 }
657 if (b.num() == 0)
658 throw std::underflow_error("Attempt to divide by zero.");
659
660 GncRational ar(a), br(b);
661 auto rr = ar / br;
662 return static_cast<GncNumeric>(rr);
663}

◆ operator/() [2/3]

GncNumeric operator/ ( GncNumeric a,
int64_t b )
inline

Definition at line 328 of file gnc-numeric.hpp.

329{
330 return a / GncNumeric(b, 1);
331}

◆ operator/() [3/3]

GncNumeric operator/ ( int64_t a,
GncNumeric b )
inline

Definition at line 332 of file gnc-numeric.hpp.

333{
334 return b / GncNumeric(a, 1);
335}