sparrow 0.9.0
Loading...
Searching...
No Matches
sparrow::decimal< T > Class Template Reference

#include <decimal.hpp>

Public Types

using integer_type = T
 

Public Member Functions

constexpr decimal ()
 Default constructor for non-placeholder integer types.
 
constexpr decimal ()
 Default constructor for placeholder integer types.
 
constexpr decimal (T value, int scale)
 Constructor from value and scale.
 
constexpr bool operator== (const decimal &other) const
 Equality comparison operator.
 
constexpr operator float () const
 Conversion to float.
 
constexpr operator double () const
 Conversion to double.
 
constexpr operator long double () const
 Conversion to long double.
 
 operator std::string () const
 Conversion to string representation.
 
constexpr T storage () const
 Gets the raw storage value.
 
constexpr int scale () const
 Gets the decimal scale.
 

Detailed Description

template<decimal_integer_type T>
class sparrow::decimal< T >
Examples
/home/runner/work/sparrow/sparrow/include/sparrow/utils/decimal.hpp.

Definition at line 61 of file decimal.hpp.

Member Typedef Documentation

◆ integer_type

template<decimal_integer_type T>
using sparrow::decimal< T >::integer_type = T

Constructor & Destructor Documentation

◆ decimal() [1/3]

template<decimal_integer_type T>
requires (!is_int_placeholder_v<T>)
sparrow::decimal< T >::decimal ( )
constexpr

Default constructor for non-placeholder integer types.

Creates a decimal representing zero with scale 0.

Precondition
T must not be an integer placeholder type
Postcondition
storage() returns 0
scale() returns 0
Decimal represents the value 0.0
Examples
/home/runner/work/sparrow/sparrow/include/sparrow/utils/decimal.hpp.

Definition at line 259 of file decimal.hpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ decimal() [2/3]

template<decimal_integer_type T>
sparrow::decimal< T >::decimal ( )
constexpr

Default constructor for placeholder integer types.

Creates a decimal with default-initialized storage and scale. Used for placeholder types that don't have well-defined zero values.

Precondition
T must be an integer placeholder type
Postcondition
storage() returns default-constructed T
scale() returns default-constructed int (typically 0)

◆ decimal() [3/3]

template<decimal_integer_type T>
sparrow::decimal< T >::decimal ( T value,
int scale )
constexpr

Constructor from value and scale.

Creates a decimal representing the value: value * 10^(-scale).

Parameters
valueThe integer coefficient of the decimal number
scaleThe decimal scale (number of digits after decimal point)
Postcondition
storage() returns value
scale() returns scale
Decimal represents the value: value * 10^(-scale)

Definition at line 275 of file decimal.hpp.

Here is the call graph for this function:

Member Function Documentation

◆ operator double()

template<decimal_integer_type T>
requires (!is_int_placeholder_v<T>)
sparrow::decimal< T >::operator double ( ) const
explicitconstexpr

Conversion to double.

Converts the decimal to a double-precision floating-point value. Provides better precision than float conversion but may still lose precision for very large or high-precision decimals.

Returns
Double-precision approximation of the decimal value
Precondition
T must not be an integer placeholder type
Postcondition
Return value approximates storage() * 10^(-scale())
Better precision than float conversion
Note
For int256_t, value is first cast to int128_t to avoid overflow

Definition at line 295 of file decimal.hpp.

Here is the call graph for this function:

◆ operator float()

template<decimal_integer_type T>
requires (!is_int_placeholder_v<T>)
sparrow::decimal< T >::operator float ( ) const
explicitconstexpr

Conversion to float.

Converts the decimal to a floating-point value by computing storage() / 10^scale(). May lose precision for large values or high precision decimals.

Returns
Floating-point approximation of the decimal value
Precondition
T must not be an integer placeholder type
Postcondition
Return value approximates storage() * 10^(-scale())
May lose precision due to float's limited precision
Note
For int256_t, value is first cast to int128_t to avoid overflow

Definition at line 288 of file decimal.hpp.

Here is the call graph for this function:

◆ operator long double()

template<decimal_integer_type T>
requires (!is_int_placeholder_v<T>)
sparrow::decimal< T >::operator long double ( ) const
explicitconstexpr

Conversion to long double.

Converts the decimal to a long double floating-point value. Provides the highest precision among floating-point conversions but may still lose precision for extremely large or high-precision decimals.

Returns
Long double approximation of the decimal value
Precondition
T must not be an integer placeholder type
Postcondition
Return value approximates storage() * 10^(-scale())
Highest precision among floating-point conversions
Note
For int256_t, value is first cast to int128_t to avoid overflow

Definition at line 302 of file decimal.hpp.

Here is the call graph for this function:

◆ operator std::string()

template<decimal_integer_type T>
requires (!is_int_placeholder_v<T>)
sparrow::decimal< T >::operator std::string ( ) const
explicitnodiscard

Conversion to string representation.

Converts the decimal to its human-readable string representation with proper decimal point placement and sign handling.

Returns
String representation of the decimal value
Precondition
T must not be an integer placeholder type
Postcondition
Returns exact string representation of the decimal value
Positive scale adds decimal point and fractional digits
Negative scale appends trailing zeros
Negative values are prefixed with '-'
Zero values return "0" regardless of scale

Definition at line 309 of file decimal.hpp.

Here is the call graph for this function:

◆ operator==()

template<decimal_integer_type T>
bool sparrow::decimal< T >::operator== ( const decimal< T > & other) const
constexpr

Equality comparison operator.

Two decimals are equal if they have the same storage value and scale. Note: This does not perform mathematical equality (e.g., 1.0 vs 10.00 with different scales would not be considered equal).

Parameters
otherDecimal to compare with
Returns
true if both storage and scale are equal, false otherwise
Postcondition
Return value is true iff (storage() == other.storage() && scale() == other.scale())
Does not normalize values before comparison
Examples
/home/runner/work/sparrow/sparrow/include/sparrow/utils/decimal.hpp.

Definition at line 282 of file decimal.hpp.

Here is the call graph for this function:

◆ scale()

template<decimal_integer_type T>
int sparrow::decimal< T >::scale ( ) const
nodiscardconstexpr

Gets the decimal scale.

Returns
The scale factor used to interpret the storage value
Postcondition
Returns the exact scale passed to constructor or default scale
Positive scale indicates fractional digits
Negative scale indicates trailing zeros
Examples
/home/runner/work/sparrow/sparrow/include/sparrow/utils/decimal.hpp.

Definition at line 362 of file decimal.hpp.

Here is the caller graph for this function:

◆ storage()

template<decimal_integer_type T>
T sparrow::decimal< T >::storage ( ) const
nodiscardconstexpr

Gets the raw storage value.

Returns
The underlying integer value used for storage
Postcondition
Returns the exact value passed to constructor or default value
Examples
/home/runner/work/sparrow/sparrow/include/sparrow/utils/decimal.hpp.

Definition at line 356 of file decimal.hpp.


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