Safeheron-SGX-Native-Development-Framework v1.2.0
Loading...
Searching...
No Matches
ssgx_decimal_t.h
Go to the documentation of this file.
1#ifndef SAFEHERON_SGX_TRUSTED_DECIMAL_H
2#define SAFEHERON_SGX_TRUSTED_DECIMAL_H
3
4#include <string>
5
6struct mpd_context_t;
7struct mpd_t;
8
9namespace ssgx {
10
33namespace decimal_t {
34
39enum class RoundType {
40 RoundUp = 0, /* round away from 0 */
41 RoundDown = 1, /* round toward 0 (truncate) */
42 RoundCeiling = 2, /* round toward +infinity */
43 RoundFloor = 3, /* round toward -infinity */
44 RoundHalfUp = 4, /* 0.5 is rounded up */
45 RoundHalfDown = 5, /* 0.5 is rounded down */
46 RoundHalfEven = 6, /* 0.5 is rounded to even */
47 Round05Up = 7, /* round zero or five away from 0 */
48 RoundTrunc = 8 /* truncate, but set infinity */
49};
50
57class Precision {
58 public:
94 Precision(RoundType round_type, int scale);
99
100 public:
101 friend class BigDecimal;
102
103 private:
104 [[nodiscard]] mpd_context_t* Get() const;
105 mpd_context_t* ptr_ctx_;
106 int scale_;
107};
108
126 public:
130 explicit BigDecimal();
131
143 explicit BigDecimal(const char* str);
144
151
158
166 BigDecimal(BigDecimal&& num) noexcept;
167
175
179 virtual ~BigDecimal();
180
208 [[nodiscard]] BigDecimal Add(const BigDecimal& num) const;
209 [[nodiscard]] BigDecimal Add(const BigDecimal& num, const Precision& prec) const;
210
238 [[nodiscard]] BigDecimal Sub(const BigDecimal& num) const;
239 [[nodiscard]] BigDecimal Sub(const BigDecimal& num, const Precision& prec) const;
240
268 [[nodiscard]] BigDecimal Multiply(const BigDecimal& num) const;
269 [[nodiscard]] BigDecimal Multiply(const BigDecimal& num, const Precision& prec) const;
270
301 [[nodiscard]] BigDecimal Div(const BigDecimal& num) const;
302 [[nodiscard]] BigDecimal Div(const BigDecimal& num, const Precision& prec) const;
303
313 bool operator==(const BigDecimal& num) const;
314
324 bool operator!=(const BigDecimal& num) const;
325
335 bool operator>(const BigDecimal& num) const;
336
346 bool operator<(const BigDecimal& num) const;
347
357 bool operator>=(const BigDecimal& num) const;
358
368 bool operator<=(const BigDecimal& num) const;
369
392 [[nodiscard]] BigDecimal SetScale(int scale, RoundType round_type = RoundType::RoundHalfUp) const;
393
408 [[nodiscard]] BigDecimal Neg() const;
409
428 static bool IsValidDecimal(const char* expr);
429
434 [[nodiscard]] std::string ToStr() const;
435
455 [[nodiscard]] std::string ToStr(int scale, RoundType round_type) const;
456
457 private:
458 int Compare(const BigDecimal& num) const;
459 static void SetDefaultContext(mpd_context_t* ptr_ctx);
460
461 private:
462 mpd_t* ptr_data_;
463};
464
465} // namespace decimal_t
466} // namespace ssgx
467
468#endif // SAFEHERON_SGX_TRUSTED_DECIMAL_H
High precision big number class This class implements the basic calculation and comparison operations...
Definition ssgx_decimal_t.h:125
static bool IsValidDecimal(const char *expr)
To check a string is a valid expression of BigDecimal or not.
BigDecimal Div(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 & operator=(BigDecimal &&num) noexcept
The move and assignment operator.
bool operator>=(const BigDecimal &num) const
Comparison operator: greater than or equal to.
BigDecimal(const BigDecimal &num)
The copy constructor Construct a BigDecimal objet and copy the value from num.
BigDecimal Add(const BigDecimal &num, const Precision &prec) const
BigDecimal()
Construct a BigDecimal object and initialized it with "0".
bool operator==(const BigDecimal &num) const
Comparison operator: equal to.
BigDecimal Sub(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.
bool operator>(const BigDecimal &num) const
Comparison operator: greater than.
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.
BigDecimal Multiply(const BigDecimal &num, const Precision &prec) const
BigDecimal(BigDecimal &&num) noexcept
The move constructor Construct a BigDecimal object and move the value from num The num object's membe...
bool operator<=(const BigDecimal &num) const
Comparison operator: less than or equal to.
bool operator<(const BigDecimal &num) const
Comparison operator: less than.
virtual ~BigDecimal()
Destruction.
BigDecimal(const char *str)
Construct a BigDecimal objet and initialized it with str.
BigDecimal & operator=(const BigDecimal &num)
The copy assignment operator.
bool operator!=(const BigDecimal &num) const
Comparison operator: not equal to.
BigDecimal Add(const BigDecimal &num) const
Addition of Numbers Return a new BigDecimal object which equal to (*this) + num.
BigDecimal Multiply(const BigDecimal &num) const
Multiplication of Numbers. Return a new BigDecimal object which equal to (*this) * num.
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.
Precision control class The Precision class is used to configure the number of digits and rounding ty...
Definition ssgx_decimal_t.h:57
Precision(RoundType round_type, int scale)
Constructor.
RoundType
Rounding type for precision control.
Definition ssgx_decimal_t.h:39
Definition ssgx_attestation_t.h:6