High precision big number class This class implements the basic calculation and comparison operations of high-precision big number library based on mpdecimal C language APIs.
More...
#include <ssgx_decimal_t.h>
High precision big number class This class implements the basic calculation and comparison operations of high-precision big number library based on mpdecimal C language APIs.
1. The max precision supported is 99 digits, when the maximum precision is exceeded, it will be rounded
automatically, the default round type is ROUND_HALF_UP. Using class Precision can customize precision and rounding type for calculation result.
2. According
https://github.com/frohoff/jdk8u-dev-jdk/blob/da0da73ab82ed714dc5be94acd2f0d00fbdfe2e9/src/share/classes/java/math/BigDecimal.java#L637 the max exponent supported is 9999999999 (10 nines). it means the value range of Bigdecimal is [-9x10^999999999, 9x10^999999999]. But the actual result depends on the specific hardware performance, an exception will occur if the result exceeds limitations.
◆ BigDecimal() [1/4]
| ssgx::decimal_t::BigDecimal::BigDecimal |
( |
| ) |
|
|
explicit |
Construct a BigDecimal object and initialized it with "0".
◆ BigDecimal() [2/4]
| ssgx::decimal_t::BigDecimal::BigDecimal |
( |
const char * | str | ) |
|
|
explicit |
Construct a BigDecimal objet and initialized it with str.
- Parameters
-
| [in] | str | The string for decimal number |
- Note
- An exception will occur, if the string
- is greater than the max value, or
- is less than the min value, or
- is an invalid number, or
- is an unsupported numeric type, such as "infinity" and "Nan".
◆ BigDecimal() [3/4]
| ssgx::decimal_t::BigDecimal::BigDecimal |
( |
const BigDecimal & | num | ) |
|
The copy constructor Construct a BigDecimal objet and copy the value from num.
- Parameters
-
| [in] | num | The object which value will be copied to this new instance |
◆ BigDecimal() [4/4]
| ssgx::decimal_t::BigDecimal::BigDecimal |
( |
BigDecimal && | num | ) |
|
|
noexcept |
The move constructor Construct a BigDecimal object and move the value from num The num object's member pointer ptr_data_ will be set as null.
- Parameters
-
| [in,out] | num | The object which value will be copied to this new instance, and its member pointer ptr_data_ will be set as null |
◆ ~BigDecimal()
| virtual ssgx::decimal_t::BigDecimal::~BigDecimal |
( |
| ) |
|
|
virtual |
◆ Add() [1/2]
Addition of Numbers Return a new BigDecimal object which equal to (*this) + num.
- Parameters
-
| [in] | num | The BigDecimal object which will be added |
| [in] | prec | Used to control the precision of the result |
- Returns
- The addition result object
- Note
- 1.By default, the result will be rounded automatically when its number of digits exceeds the default maximum precision, the type of rounding is ROUND_HALF_UP.
2.If a Precision is set, the result will be rounded automatically when its number of digits exceeds
Precision.pre, the type of rounding is determined by Precision.round. 3. An exception will occur, if the result exceeds the range of BigDecimal.
@par Examples
@code
High precision big number class This class implements the basic calculation and comparison operations...
Definition ssgx_decimal_t.h:125
BigDecimal Add(const BigDecimal &num) const
Addition of Numbers Return a new BigDecimal object which equal to (*this) + num.
Precision control class The Precision class is used to configure the number of digits and rounding ty...
Definition ssgx_decimal_t.h:57
◆ Add() [2/2]
◆ Div() [1/2]
Division of Numbers. Return a new BigDecimal object which equal to (*this) / num.
- Parameters
-
| [in] | num | The BigDecimal object which is the divisor |
| [in] | prec | Precision control settings for the result |
- Returns
- The division result object
- Note
- 1.By default, the result will be rounded automatically when its number of digits exceeds the default maximum precision, the type of rounding is ROUND_HALF_UP.
2.If a Precision is set, the result will be rounded automatically when its number of digits exceeds
Precision.pre, the type of rounding is determined by Precision.round. 3. An exception will occur, if
a) the result exceeds the range of BigDecimal, or
b) 0 is the divisor, or
c) the dividend or divisor is infinity.
@par Examples
@code
BigDecimal Div(const BigDecimal &num) const
Division of Numbers. Return a new BigDecimal object which equal to (*this) / num.
◆ Div() [2/2]
◆ IsValidDecimal()
| static bool ssgx::decimal_t::BigDecimal::IsValidDecimal |
( |
const char * | expr | ) |
|
|
static |
To check a string is a valid expression of BigDecimal or not.
- Parameters
-
- Returns
- Return true if expr is a valid expression of BigDecimal, otherwise return fasle.
- Note
- An exception will occur if failed to malloc memory.
@par Examples
@code
static bool IsValidDecimal(const char *expr)
To check a string is a valid expression of BigDecimal or not.
◆ Multiply() [1/2]
Multiplication of Numbers. Return a new BigDecimal object which equal to (*this) * num.
- Parameters
-
| [in] | num | The BigDecimal object which is another multiplier |
| [in] | prec | Precision control settings for the result |
- Returns
- The multiplication result object
- Note
- 1.By default, the result will be rounded automatically when its number of digits exceeds the default maximum precision, the type of rounding is ROUND_HALF_UP.
2.If a Precision is set, the result will be rounded automatically when its number of digits exceeds
Precision.pre, the type of rounding is determined by Precision.round. 3. An exception will occur, if the result exceeds the range of BigDecimal.
@par Examples
@code
BigDecimal Multiply(const BigDecimal &num) const
Multiplication of Numbers. Return a new BigDecimal object which equal to (*this) * num.
◆ Multiply() [2/2]
◆ Neg()
| BigDecimal ssgx::decimal_t::BigDecimal::Neg |
( |
| ) |
const |
|
nodiscard |
Take the opposite number.
- Returns
- Return a new BigDecimal object which is the opposite of this object
- Note
- An exception will occur if operation is failed.
@par Examples
@code
BigDecimal Neg() const
Take the opposite number.
◆ operator!=()
| bool ssgx::decimal_t::BigDecimal::operator!= |
( |
const BigDecimal & | num | ) |
const |
Comparison operator: not equal to.
- Parameters
-
- Returns
- Return true if (*this) is not equal to num, otherwise return false.
- Note
- An exception will occur if
- one of the numbers is Infinity or Nan, or
- failed to malloc memory
◆ operator<()
| bool ssgx::decimal_t::BigDecimal::operator< |
( |
const BigDecimal & | num | ) |
const |
Comparison operator: less than.
- Parameters
-
- Returns
- Return true if (*this) is less than num, otherwise return false.
- Note
- An exception will occur if
- one of the numbers is Infinity or Nan, or
- failed to malloc memory
◆ operator<=()
| bool ssgx::decimal_t::BigDecimal::operator<= |
( |
const BigDecimal & | num | ) |
const |
Comparison operator: less than or equal to.
- Parameters
-
- Returns
- Return true if (*this) is less than or equal to num, otherwise return false.
- Note
- An exception will occur if
- one of the numbers is Infinity or Nan, or
- failed to malloc memory
◆ operator=() [1/2]
The move and assignment operator.
- Parameters
-
| [in,out] | num | The object which value will be copied to this new instance, and its member pointer ptr_data_ will be set as null |
- Returns
- A new BigDecimal object moved from num.
◆ operator=() [2/2]
The copy assignment operator.
- Parameters
-
| [in] | num | The object which value will be copied to this new instance |
- Returns
- Return a new BigDecimal object which has the same value as num
◆ operator==()
| bool ssgx::decimal_t::BigDecimal::operator== |
( |
const BigDecimal & | num | ) |
const |
Comparison operator: equal to.
- Parameters
-
- Returns
- Return true if (*this) is equal to num, otherwise return false.
- Note
- An exception will occur if
- one of the numbers is Infinity or Nan, or
- failed to malloc memory
◆ operator>()
| bool ssgx::decimal_t::BigDecimal::operator> |
( |
const BigDecimal & | num | ) |
const |
Comparison operator: greater than.
- Parameters
-
- Returns
- Return true if (*this) is greater than num, otherwise return false.
- Note
- An exception will occur if
- one of the numbers is Infinity or Nan, or
- failed to malloc memory
◆ operator>=()
| bool ssgx::decimal_t::BigDecimal::operator>= |
( |
const BigDecimal & | num | ) |
const |
Comparison operator: greater than or equal to.
- Parameters
-
- Returns
- Return true if (*this) is greater than or equal to num, otherwise return false.
- Note
- An exception will occur if
- one of the numbers is Infinity or Nan, or
- failed to malloc memory
◆ SetScale()
Set the number of significant decimal places for this BigDecimal.
- Parameters
-
| [in] | scale | The number of significant digits after the decimal point 1.scale > 0, Keep significant digits up to a certain decimal place 2.scale = 0, The result will be kept as an integer 3.scale < 0, Keep significant digits up to a certain integer place |
| [in] | round_type | Rounding policy, one of RoundType value. |
- Returns
- A new BigDecimal object which significant digits after the decimal point is scale
- Note
- An exception will occur if operation is failed.
@par Examples
@code
BigDecimal SetScale(int scale, RoundType round_type=RoundType::RoundHalfUp) const
Set the number of significant decimal places for this BigDecimal.
◆ Sub() [1/2]
Subtraction of Numbers. Return a new BigDecimal object which equal to (*this) - num.
- Parameters
-
| [in] | num | The BigDecimal object which is the subtrahend |
| [in] | prec | Precision control settings for the result |
- Returns
- The subtraction result object
- Note
- 1.By default, the result will be rounded automatically when its number of digits exceeds the default maximum precision, the type of rounding is ROUND_HALF_UP.
2.If a Precision is set, the result will be rounded automatically when its number of digits exceeds
Precision.pre, the type of rounding is is determined by Precision.round. 3. An exception will occur, if the result exceeds the range of BigDecimal.
@par Examples
@code
BigDecimal Sub(const BigDecimal &num) const
Subtraction of Numbers. Return a new BigDecimal object which equal to (*this) - num.
◆ Sub() [2/2]
◆ ToStr() [1/2]
| std::string ssgx::decimal_t::BigDecimal::ToStr |
( |
| ) |
const |
|
nodiscard |
Conversion to decimal string in default precision.
- Returns
- A decimal string
◆ ToStr() [2/2]
| std::string ssgx::decimal_t::BigDecimal::ToStr |
( |
int | scale, |
|
|
RoundType | round_type ) const |
|
nodiscard |
Conversion to decimal string is customized precision and rounding policy.
- Parameters
-
| [in] | scale | The number of significant digits after the decimal point 1.scale > 0, Keep significant digits up to a certain decimal place 2.scale = 0, The result will be kept as an integer 3.scale < 0, Keep significant digits up to a certain integer place |
| [in] | round_type | Rounding policy, one of RoundType value. |
- Returns
- A decimal string
@par Examples
@code
std::string str1 = a.ToStr(1, ROUND_HALF_UP);
std::string str2 = a.ToStr(1, ROUND_HALF_DOWN);
The documentation for this class was generated from the following file: