|
nfx-datatypes 0.1.1
Cross-platform C++ library with high-precision Int128 and Decimal datatypes
|
Cross-platform high-precision decimal type. More...
#include <nfx/datatypes/Decimal.h>
Public Types | |
| enum class | RoundingMode : std::uint8_t { ToNearest = 0 , ToNearestTiesAway , ToZero , ToPositiveInfinity , ToNegativeInfinity } |
| Rounding modes for decimal arithmetic operations. More... | |
Public Member Functions | |
| constexpr | Decimal () noexcept |
| Default constructor (zero value). | |
| Decimal (float value) noexcept | |
| Construct from single-precision floating-point value with IEEE 754-2008 input compatibility. | |
| Decimal (double value) noexcept | |
| Construct from double with IEEE 754-2008 input compatibility. | |
| Decimal (std::int32_t value) noexcept | |
| Construct from 32-bit integer. | |
| Decimal (std::int64_t value) noexcept | |
| Construct from 64-bit integer. | |
| Decimal (std::uint32_t value) noexcept | |
| Construct from 32-bit unsigned integer. | |
| Decimal (std::uint64_t value) noexcept | |
| Construct from 64-bit unsigned integer. | |
| Decimal (std::string_view str) | |
| Construct from string (exact parsing). | |
| Decimal (const Int128 &val) | |
| Construct from 128-bit integer with overflow detection. | |
| Decimal (const Decimal &other) noexcept=default | |
| Copy constructor. | |
| Decimal (Decimal &&other) noexcept=default | |
| Move constructor. | |
| ~Decimal ()=default | |
| Destructor. | |
| Decimal & | operator= (const Decimal &other) noexcept=default |
| Copy assignment operator. | |
| Decimal & | operator= (Decimal &&other) noexcept=default |
| Move assignment operator. | |
| std::strong_ordering | operator<=> (const Decimal &other) const noexcept |
| Three-way comparison operator (C++20). | |
| bool | operator== (const Decimal &other) const noexcept |
| Equality comparison operator. | |
| bool | operator== (float val) const noexcept |
| Equality comparison with float. | |
| bool | operator!= (float val) const noexcept |
| Inequality comparison with float. | |
| bool | operator< (float val) const noexcept |
| Less than comparison with float. | |
| bool | operator<= (float val) const noexcept |
| Less than or equal comparison with float. | |
| bool | operator> (float val) const noexcept |
| Greater than comparison with float. | |
| bool | operator>= (float val) const noexcept |
| Greater than or equal comparison with float. | |
| bool | operator== (double val) const noexcept |
| Equality comparison with double. | |
| bool | operator!= (double val) const noexcept |
| Inequality comparison with double. | |
| bool | operator< (double val) const noexcept |
| Less than comparison with double. | |
| bool | operator<= (double val) const noexcept |
| Less than or equal comparison with double. | |
| bool | operator> (double val) const noexcept |
| Greater than comparison with double. | |
| bool | operator>= (double val) const noexcept |
| Greater than or equal comparison with double. | |
| bool | operator== (std::int64_t val) const noexcept |
| Equality comparison with signed 64-bit integer. | |
| bool | operator!= (std::int64_t val) const noexcept |
| Inequality comparison with signed 64-bit integer. | |
| bool | operator< (std::int64_t val) const noexcept |
| Less than comparison with signed 64-bit integer. | |
| bool | operator<= (std::int64_t val) const noexcept |
| Less than or equal comparison with signed 64-bit integer. | |
| bool | operator> (std::int64_t val) const noexcept |
| Greater than comparison with signed 64-bit integer. | |
| bool | operator>= (std::int64_t val) const noexcept |
| Greater than or equal comparison with signed 64-bit integer. | |
| bool | operator== (std::uint64_t val) const noexcept |
| Equality comparison with unsigned 64-bit integer. | |
| bool | operator!= (std::uint64_t val) const noexcept |
| Inequality comparison with unsigned 64-bit integer. | |
| bool | operator< (std::uint64_t val) const noexcept |
| Less than comparison with unsigned 64-bit integer. | |
| bool | operator<= (std::uint64_t val) const noexcept |
| Less than or equal comparison with unsigned 64-bit integer. | |
| bool | operator> (std::uint64_t val) const noexcept |
| Greater than comparison with unsigned 64-bit integer. | |
| bool | operator>= (std::uint64_t val) const noexcept |
| Greater than or equal comparison with unsigned 64-bit integer. | |
| bool | operator== (std::int32_t val) const noexcept |
| Equality comparison with signed 32-bit integer. | |
| bool | operator!= (std::int32_t val) const noexcept |
| Inequality comparison with signed 32-bit integer. | |
| bool | operator< (std::int32_t val) const noexcept |
| Less than comparison with signed 32-bit integer. | |
| bool | operator<= (std::int32_t val) const noexcept |
| Less than or equal comparison with signed 32-bit integer. | |
| bool | operator> (std::int32_t val) const noexcept |
| Greater than comparison with signed 32-bit integer. | |
| bool | operator>= (std::int32_t val) const noexcept |
| Greater than or equal comparison with signed 32-bit integer. | |
| bool | operator== (const Int128 &val) const noexcept |
| Equality comparison with Int128. | |
| bool | operator!= (const Int128 &val) const noexcept |
| Inequality comparison with Int128. | |
| bool | operator< (const Int128 &val) const noexcept |
| Less than comparison with Int128. | |
| bool | operator<= (const Int128 &val) const noexcept |
| Less than or equal comparison with Int128. | |
| bool | operator> (const Int128 &val) const noexcept |
| Greater than comparison with Int128. | |
| bool | operator>= (const Int128 &val) const noexcept |
| Greater than or equal comparison with Int128. | |
| Decimal | operator+ (const Decimal &other) |
| Addition operator. | |
| Decimal | operator- (const Decimal &other) |
| Subtraction operator. | |
| Decimal | operator* (const Decimal &other) const |
| Multiplication operator. | |
| Decimal | operator/ (const Decimal &other) const |
| Division operator. | |
| Decimal & | operator+= (const Decimal &other) |
| Addition assignment operator. | |
| Decimal & | operator-= (const Decimal &other) |
| Subtraction assignment operator. | |
| Decimal & | operator*= (const Decimal &other) |
| Multiplication assignment operator. | |
| Decimal & | operator/= (const Decimal &other) |
| Division assignment operator. | |
| Decimal | operator- () const noexcept |
| Unary minus operator (negation). | |
| std::uint8_t | scale () const noexcept |
| Get decimal scale (number of decimal places). | |
| const std::uint32_t & | flags () const noexcept |
| Get flags value. | |
| std::uint32_t & | flags () noexcept |
| Get mutable flags value. | |
| const std::array< std::uint32_t, 3 > & | mantissa () const noexcept |
| Get mantissa array. | |
| std::array< std::uint32_t, 3 > & | mantissa () noexcept |
| Get mutable mantissa array. | |
| Decimal | abs () const noexcept |
| Get absolute value. | |
| Decimal | ceil () const noexcept |
| Round up to nearest integer. | |
| Decimal | floor () const noexcept |
| Round down to nearest integer. | |
| Decimal | round (std::int32_t decimalsPlacesCount=0, RoundingMode mode=RoundingMode::ToNearest) const noexcept |
| Round decimal to specified precision using configurable rounding mode. | |
| Decimal | sqrt () const |
| Compute square root using Newton-Raphson method. | |
| Decimal | trunc () const noexcept |
| Remove fractional part. | |
| double | toDouble () const noexcept |
| Convert to double (may lose precision). | |
| std::string | toString () const |
| Convert to string with exact precision. | |
| std::array< std::int32_t, 4 > | toBits () const noexcept |
| Get internal 32-bit representation. | |
| std::uint8_t | decimalPlacesCount () const noexcept |
| Count actual decimal places (excluding trailing zeros). | |
Static Public Member Functions | |
| static bool | fromString (std::string_view str, Decimal &result) noexcept |
| Parse string to decimal with error handling. | |
| static std::optional< Decimal > | fromString (std::string_view str) noexcept |
| Parse string to decimal with optional return. | |
Friends | |
| struct | std::numeric_limits< Decimal > |
Cross-platform high-precision decimal type.
Implements exact decimal arithmetic with:
Value Representation:
Financial Calculation Benefits:
IEEE 754-2008 binary64 Input Compatibility:
|
strong |
Rounding modes for decimal arithmetic operations.
Defines how rounding should be performed when reducing decimal precision. Each mode follows industry-standard rounding semantics:
|
inlineexplicitnoexcept |
Construct from single-precision floating-point value with IEEE 754-2008 input compatibility.
| value | Float value to convert |
Delegates to the double constructor for consistent behavior. Limited to IEEE 754 binary32 precision (~6-7 significant digits). Uses IEEE 754-2008 std::isnan and std::isinf for special value detection. NaN and Infinity values are converted to zero.

|
explicitnoexcept |
Construct from double with IEEE 754-2008 input compatibility.
| value | Double value to convert |

|
inlineexplicitnoexcept |
Construct from 32-bit integer.
| value | Integer value to convert |

|
inlineexplicitnoexcept |
Construct from 64-bit integer.
| value | Integer value to convert |

|
inlineexplicitnoexcept |
Construct from 32-bit unsigned integer.
| value | Unsigned integer value to convert |

|
inlineexplicitnoexcept |
Construct from 64-bit unsigned integer.
| value | Unsigned integer value to convert |

|
inlineexplicit |
Construct from string (exact parsing).
| str | String representation (e.g., "123.456") |
| std::invalid_argument | if string is not a valid decimal |

|
explicit |
Construct from 128-bit integer with overflow detection.
| val | Int128 value to convert |
| std::overflow_error | if Int128 value exceeds Decimal's 96-bit mantissa capacity |
Converts Int128 to Decimal integer representation (scale = 0). Range Handling:
Overflow Behavior:
Examples:

|
defaultnoexcept |
Copy constructor.
| other | The Decimal object to copy from |

|
defaultnoexcept |
Move constructor.
| other | The Decimal object to move from |

|
inlinenodiscardnoexcept |
Get absolute value.


|
inlinenodiscardnoexcept |
Round up to nearest integer.


|
nodiscardnoexcept |
Count actual decimal places (excluding trailing zeros).
Examples:


|
inlinenodiscardnoexcept |
Get flags value.


|
inlinenodiscardnoexcept |
Get mutable flags value.

|
inlinenodiscardnoexcept |
Round down to nearest integer.


|
staticnodiscardnoexcept |
Parse string to decimal with optional return.
| str | String to parse (e.g., "123.45", "-0.001", "42") |
This is a convenience wrapper around the two-parameter fromString method. Supports the same input formats:

|
staticnodiscardnoexcept |
Parse string to decimal with error handling.
| str | String to parse (e.g., "123.45", "-0.001", "42") |
| result | Output decimal value |
Parses string representations of decimal numbers with comprehensive format support. Supported input formats:


|
inlinenodiscardnoexcept |
Get mantissa array.


|
inlinenodiscardnoexcept |
Get mutable mantissa array.

|
inlinenoexcept |
Inequality comparison with Int128.
| val | Right operand |
|
inlinenoexcept |
Inequality comparison with double.
| val | Right operand |
|
inlinenoexcept |
Inequality comparison with float.
| val | Right operand |
|
inlinenoexcept |
Inequality comparison with signed 32-bit integer.
| val | Right operand |
|
inlinenoexcept |
Inequality comparison with signed 64-bit integer.
| val | Right operand |
|
inlinenoexcept |
Inequality comparison with unsigned 64-bit integer.
| val | Right operand |
Multiplication operator.
| other | The Decimal value to multiply by |

Multiplication assignment operator.
| other | The Decimal value to multiply by |

Addition operator.
| other | The Decimal value to add |

Addition assignment operator.
| other | The Decimal value to add |

|
inlinenoexcept |
Unary minus operator (negation).

Subtraction operator.
| other | The Decimal value to subtract |

Subtraction assignment operator.
| other | The Decimal value to subtract |

Division operator.
| other | Divisor |
| std::overflow_error | if divisor is zero (no NaN/Infinity representation) |

Division assignment operator.
| other | Divisor |
| std::overflow_error | if divisor is zero (no NaN/Infinity representation) |

|
noexcept |
Less than comparison with Int128.
| val | Right operand |
|
inlinenoexcept |
Less than comparison with double.
| val | Right operand |
|
inlinenoexcept |
Less than comparison with float.
| val | Right operand |
|
inlinenoexcept |
Less than comparison with signed 32-bit integer.
| val | Right operand |
|
inlinenoexcept |
Less than comparison with signed 64-bit integer.
| val | Right operand |
|
inlinenoexcept |
Less than comparison with unsigned 64-bit integer.
| val | Right operand |
|
inlinenoexcept |
Less than or equal comparison with Int128.
| val | Right operand |
|
inlinenoexcept |
Less than or equal comparison with double.
| val | Right operand |
|
inlinenoexcept |
Less than or equal comparison with float.
| val | Right operand |
|
inlinenoexcept |
Less than or equal comparison with signed 32-bit integer.
| val | Right operand |
|
inlinenoexcept |
Less than or equal comparison with signed 64-bit integer.
| val | Right operand |
|
inlinenoexcept |
Less than or equal comparison with unsigned 64-bit integer.
| val | Right operand |
|
noexcept |
Three-way comparison operator (C++20).
| other | The Decimal value to compare with |

Copy assignment operator.
| other | The Decimal object to copy from |

Move assignment operator.
| other | The Decimal object to move from |

|
noexcept |
Equality comparison operator.
| other | The Decimal value to compare with |

|
noexcept |
Equality comparison with Int128.
| val | Right operand |
|
inlinenoexcept |
Equality comparison with double.
| val | Right operand |
|
inlinenoexcept |
Equality comparison with float.
| val | Right operand |
|
inlinenoexcept |
Equality comparison with signed 32-bit integer.
| val | Right operand |
|
inlinenoexcept |
Equality comparison with signed 64-bit integer.
| val | Right operand |
|
inlinenoexcept |
Equality comparison with unsigned 64-bit integer.
| val | Right operand |
|
inlinenoexcept |
Greater than comparison with Int128.
| val | Right operand |
|
inlinenoexcept |
Greater than comparison with double.
| val | Right operand |
|
inlinenoexcept |
Greater than comparison with float.
| val | Right operand |
|
inlinenoexcept |
Greater than comparison with signed 32-bit integer.
| val | Right operand |
|
inlinenoexcept |
Greater than comparison with signed 64-bit integer.
| val | Right operand |
|
inlinenoexcept |
Greater than comparison with unsigned 64-bit integer.
| val | Right operand |
|
inlinenoexcept |
Greater than or equal comparison with Int128.
| val | Right operand |
|
inlinenoexcept |
Greater than or equal comparison with double.
| val | Right operand |
|
inlinenoexcept |
Greater than or equal comparison with float.
| val | Right operand |
|
inlinenoexcept |
Greater than or equal comparison with signed 32-bit integer.
| val | Right operand |
|
inlinenoexcept |
Greater than or equal comparison with signed 64-bit integer.
| val | Right operand |
|
inlinenoexcept |
Greater than or equal comparison with unsigned 64-bit integer.
| val | Right operand |
|
nodiscardnoexcept |
Round decimal to specified precision using configurable rounding mode.
| decimalsPlacesCount | Number of decimal places to round to (default: 0 for integer rounding) |
| mode | Rounding mode to apply (default: RoundingMode::ToNearest for banker's rounding) |


|
nodiscardnoexcept |
Get decimal scale (number of decimal places).


|
nodiscard |
Compute square root using Newton-Raphson method.
| std::domain_error | if called on negative value |
Uses iterative Newton-Raphson algorithm: x_{n+1} = (x_n + value/x_n) / 2 Converges quadratically for positive values. Returns exact result for perfect squares within precision limits.


|
nodiscardnoexcept |
Get internal 32-bit representation.


|
nodiscardnoexcept |
Convert to double (may lose precision).


|
nodiscard |
Convert to string with exact precision.


|
inlinenodiscardnoexcept |
Remove fractional part.

