Safeheron-SGX-Native-Development-Framework v1.2.0
Loading...
Searching...
No Matches
ssgx::decimal_t::BigDecimal Class Reference

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>

Public Member Functions

 BigDecimal ()
 Construct a BigDecimal object and initialized it with "0".
 
 BigDecimal (const char *str)
 Construct a BigDecimal objet and initialized it with str.
 
 BigDecimal (const BigDecimal &num)
 The copy constructor Construct a BigDecimal objet and copy the value from num.
 
BigDecimaloperator= (const BigDecimal &num)
 The copy assignment operator.
 
 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.
 
BigDecimaloperator= (BigDecimal &&num) noexcept
 The move and assignment operator.
 
virtual ~BigDecimal ()
 Destruction.
 
BigDecimal Add (const BigDecimal &num) const
 Addition of Numbers Return a new BigDecimal object which equal to (*this) + num.
 
BigDecimal Add (const BigDecimal &num, const Precision &prec) const
 
BigDecimal Sub (const BigDecimal &num) const
 Subtraction of Numbers. Return a new BigDecimal object which equal to (*this) - num.
 
BigDecimal Sub (const BigDecimal &num, const Precision &prec) const
 
BigDecimal Multiply (const BigDecimal &num) const
 Multiplication of Numbers. Return a new BigDecimal object which equal to (*this) * num.
 
BigDecimal Multiply (const BigDecimal &num, const Precision &prec) const
 
BigDecimal Div (const BigDecimal &num) const
 Division of Numbers. Return a new BigDecimal object which equal to (*this) / num.
 
BigDecimal Div (const BigDecimal &num, const Precision &prec) const
 
bool operator== (const BigDecimal &num) const
 Comparison operator: equal to.
 
bool operator!= (const BigDecimal &num) const
 Comparison operator: not equal to.
 
bool operator> (const BigDecimal &num) const
 Comparison operator: greater than.
 
bool operator< (const BigDecimal &num) const
 Comparison operator: less than.
 
bool operator>= (const BigDecimal &num) const
 Comparison operator: greater than or equal to.
 
bool operator<= (const BigDecimal &num) const
 Comparison operator: less than or equal to.
 
BigDecimal SetScale (int scale, RoundType round_type=RoundType::RoundHalfUp) const
 Set the number of significant decimal places for this BigDecimal.
 
BigDecimal Neg () const
 Take the opposite number.
 
std::string ToStr () const
 Conversion to decimal string in default precision.
 
std::string ToStr (int scale, RoundType round_type) const
 Conversion to decimal string is customized precision and rounding policy.
 

Static Public Member Functions

static bool IsValidDecimal (const char *expr)
 To check a string is a valid expression of BigDecimal or not.
 

Detailed Description

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.

Constructor & Destructor Documentation

◆ 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]strThe string for decimal number
Note
An exception will occur, if the string
  1. is greater than the max value, or
  2. is less than the min value, or
  3. is an invalid number, or
  4. 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]numThe 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]numThe 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

Destruction.

Member Function Documentation

◆ Add() [1/2]

BigDecimal ssgx::decimal_t::BigDecimal::Add ( const BigDecimal & num) const
nodiscard

Addition of Numbers Return a new BigDecimal object which equal to (*this) + num.

Parameters
[in]numThe BigDecimal object which will be added
[in]precUsed 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
BigDecimal a("1.234");
BigDecimal b("2.5");
BigDecimal c = a.Add(b); // 3.734
Precision prec(20, ROUND_HALF_UP, 2);
BigDecimal d = a.Add(b); // 3.73
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]

BigDecimal ssgx::decimal_t::BigDecimal::Add ( const BigDecimal & num,
const Precision & prec ) const
nodiscard

◆ Div() [1/2]

BigDecimal ssgx::decimal_t::BigDecimal::Div ( const BigDecimal & num) const
nodiscard

Division of Numbers. Return a new BigDecimal object which equal to (*this) / num.

Parameters
[in]numThe BigDecimal object which is the divisor
[in]precPrecision 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 a("2.456");
BigDecimal b("2");
BigDecimal c = a.Div(b); // 1.228
Precision prec(20, ROUND_HALF_UP, 2);
BigDecimal d = a.Div(b); // 1.23
BigDecimal Div(const BigDecimal &num) const
Division of Numbers. Return a new BigDecimal object which equal to (*this) / num.

◆ Div() [2/2]

BigDecimal ssgx::decimal_t::BigDecimal::Div ( const BigDecimal & num,
const Precision & prec ) const
nodiscard

◆ 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
[in]exprA string
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
bool result1 = BigDecimal::IsValidDecimal("2e+4"); // true;
bool result2 = BigDecimal::IsValidDecimal("6(E3)"); // true;
bool result3 = BigDecimal::IsValidDecimal("d7868cdnjc"); // false;
bool result4 = BigDecimal::IsValidDecimal("Nan"); // false;
bool result4 = BigDecimal::IsValidDecimal("infinity"); // false;
static bool IsValidDecimal(const char *expr)
To check a string is a valid expression of BigDecimal or not.

◆ Multiply() [1/2]

BigDecimal ssgx::decimal_t::BigDecimal::Multiply ( const BigDecimal & num) const
nodiscard

Multiplication of Numbers. Return a new BigDecimal object which equal to (*this) * num.

Parameters
[in]numThe BigDecimal object which is another multiplier
[in]precPrecision 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 a("1.2345");
BigDecimal b("1.5");
BigDecimal c = a.Multiply(b); // 1.85175
Precision prec(20, ROUND_HALF_UP, 3);
BigDecimal d = a.Multiply(b); // 1.852
BigDecimal Multiply(const BigDecimal &num) const
Multiplication of Numbers. Return a new BigDecimal object which equal to (*this) * num.

◆ Multiply() [2/2]

BigDecimal ssgx::decimal_t::BigDecimal::Multiply ( const BigDecimal & num,
const Precision & prec ) const
nodiscard

◆ 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 a("2.456");
BigDecimal b = a.Neg(); //b is -2.456
BigDecimal Neg() const
Take the opposite number.

◆ operator!=()

bool ssgx::decimal_t::BigDecimal::operator!= ( const BigDecimal & num) const

Comparison operator: not equal to.

Parameters
[in]numThe BigDecimal object to compare
Returns
Return true if (*this) is not equal to num, otherwise return false.
Note
An exception will occur if
  1. one of the numbers is Infinity or Nan, or
  2. failed to malloc memory

◆ operator<()

bool ssgx::decimal_t::BigDecimal::operator< ( const BigDecimal & num) const

Comparison operator: less than.

Parameters
[in]numThe BigDecimal object to compare
Returns
Return true if (*this) is less than num, otherwise return false.
Note
An exception will occur if
  1. one of the numbers is Infinity or Nan, or
  2. failed to malloc memory

◆ operator<=()

bool ssgx::decimal_t::BigDecimal::operator<= ( const BigDecimal & num) const

Comparison operator: less than or equal to.

Parameters
[in]numThe BigDecimal object to compare
Returns
Return true if (*this) is less than or equal to num, otherwise return false.
Note
An exception will occur if
  1. one of the numbers is Infinity or Nan, or
  2. failed to malloc memory

◆ operator=() [1/2]

BigDecimal & ssgx::decimal_t::BigDecimal::operator= ( BigDecimal && num)
noexcept

The move and assignment operator.

Parameters
[in,out]numThe 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]

BigDecimal & ssgx::decimal_t::BigDecimal::operator= ( const BigDecimal & num)

The copy assignment operator.

Parameters
[in]numThe 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
[in]numThe BigDecimal object to compare
Returns
Return true if (*this) is equal to num, otherwise return false.
Note
An exception will occur if
  1. one of the numbers is Infinity or Nan, or
  2. failed to malloc memory

◆ operator>()

bool ssgx::decimal_t::BigDecimal::operator> ( const BigDecimal & num) const

Comparison operator: greater than.

Parameters
[in]numThe BigDecimal object to compare
Returns
Return true if (*this) is greater than num, otherwise return false.
Note
An exception will occur if
  1. one of the numbers is Infinity or Nan, or
  2. failed to malloc memory

◆ operator>=()

bool ssgx::decimal_t::BigDecimal::operator>= ( const BigDecimal & num) const

Comparison operator: greater than or equal to.

Parameters
[in]numThe BigDecimal object to compare
Returns
Return true if (*this) is greater than or equal to num, otherwise return false.
Note
An exception will occur if
  1. one of the numbers is Infinity or Nan, or
  2. failed to malloc memory

◆ SetScale()

BigDecimal ssgx::decimal_t::BigDecimal::SetScale ( int scale,
RoundType round_type = RoundType::RoundHalfUp ) const
nodiscard

Set the number of significant decimal places for this BigDecimal.

Parameters
[in]scaleThe 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_typeRounding 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 a("1.23456");
BigDecimal b = a.SetScale(2, ROUND_HALF_UP); // 1.23;
BigDecimal c = a.SetScale(3, ROUND_HALF_UP); // 1.235;
BigDecimal d = a.SetScale(10, ROUND_HALF_UP); // 1.2345600000;
BigDecimal SetScale(int scale, RoundType round_type=RoundType::RoundHalfUp) const
Set the number of significant decimal places for this BigDecimal.

◆ Sub() [1/2]

BigDecimal ssgx::decimal_t::BigDecimal::Sub ( const BigDecimal & num) const
nodiscard

Subtraction of Numbers. Return a new BigDecimal object which equal to (*this) - num.

Parameters
[in]numThe BigDecimal object which is the subtrahend
[in]precPrecision 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 a("10.768");
BigDecimal b("1.2");
BigDecimal c = a.Sub(b); // 9.568
Precision prec(20, ROUND_HALF_UP, 2);
BigDecimal d = a.Sub(b); // 9.57
BigDecimal Sub(const BigDecimal &num) const
Subtraction of Numbers. Return a new BigDecimal object which equal to (*this) - num.

◆ Sub() [2/2]

BigDecimal ssgx::decimal_t::BigDecimal::Sub ( const BigDecimal & num,
const Precision & prec ) const
nodiscard

◆ 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]scaleThe 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_typeRounding policy, one of RoundType value.
Returns
A decimal string
@par Examples
@code
BigDecimal a("2.456");
std::string str1 = a.ToStr(1, ROUND_HALF_UP); // 2.5
std::string str2 = a.ToStr(1, ROUND_HALF_DOWN); // 2.4

The documentation for this class was generated from the following file: