一款用于金融应用的高性能、高精度、零分配的固定小数点库
High performance, high precision, zero allocation fixed-point decimal number for financial applications.
Supported version: Go >= 1.23
go get github.com/quagmt/udecimalDecimal value while keeping the original value unchanged, making it safe to be shared across goroutines.shopspring/decimal library to ensure correctness.NOTE: This library does not perform implicit rounding. If the result of an operation exceeds the maximum precision, extra digits are truncated. All rounding methods must be explicitly invoked. (see Rounding Methods for more details)
…There are already a couple of decimal libraries available in Go, such as shopspring/decimal, cockroachdb/apd, govalues/decimal, etc. However, each of these libraries has its own limitations, for example:
This library is designed to address these limitations, providing both high performance and zero allocation while maintaining an acceptable range of precision, which is suitable for most financial applications.
Rounding numbers can often be challenging and confusing due to the variety of methods available. Each method serves specific purposes, and it's common for developers to make mistakes or incorrect assumptions about how rounding should be performed. For example, the result of round(1.45) could be either 1.4 or 1.5, depending on the rounding method used.
This issue is particularly critical in financial applications, where even minor rounding errors can accumulate and lead to significant financial losses. To mitigate such errors, this library intentionally avoids implicit rounding. If the result of an operation exceeds the maximum precision specified by developers beforehand, extra digits are truncated. Developers need to explicitly choose the rounding method they want to use. The supported rounding methods are:
package main
import (
"fmt"
"github.com/quagmt/udecimal"
)
func main() {
// Create a new decimal number
a, _ := udecimal.NewFromFloat64(1.345) // a = 1.345
// Rounding
fmt.Println(a.RoundBank(2)) // banker's rounding: 1.345 -> 1.34
fmt.Println(a.RoundAwayFromZero(2)) // round away from zero: 1.345 -> 1.35
fmt.Println(a.RoundHAZ(2)) // half away from zero: 1.345 -> 1.35
fmt.Println(a.RoundHTZ(2)) // half towards zero: 1.345 -> 1.34
}As mentioned above, this library is not always memory allocation free. However, those cases where we need to allocate memory are incredibly rare. To understand why, let's take a look at how the Decimal type is implemented.
The Decimal type represents a fixed-point decimal number. It consists of three components: sign, coefficient, and prec. The number is represented as:
// decimal value = (neg == true ? -1 : 1) * coef * 10^(-prec)
type Decimal struct {
coef bint
neg bool
prec uint8 // 0 neg = false, coef = 123456, prec = 3
// -123.456 = -123456 / 10^-3
// -> neg = true, coef = 123456, prec = 3You can notice that coef data type is bint, which is a custom data type:
type bint struct {
// For coefficients exceeding u128
bigInt *big.Int
// For coefficients less than 2^128
u128 u128
}The bint type can store coefficients up to 2^128 - 1 using u128. Arithmetic operations with u128 are fast and require no memory allocation. If result of an arithmetic operation exceeds u128 capacity, the whole operation will be performed using big.Int API. Such operations are slower and do involve memory allocation. However, those cases are rare in financial applications due to the extensive range provided by a 128-bit unsigned integer, for example:
If precision is 0, the decimal range it can store is:
[-340282366920938463463374607431768211455, 340282366920938463463374607431768211455](approximately -340 to 340 undecillion)
If precision is 19, the decimal range becomes:
[-34028236692093846346.3374607431768211455, 34028236692093846346.3374607431768211455] (approximately -34 to 34 quintillion)
Therefore, in most cases you can expect high performance and no memory allocation when using this library.
This library is inspired by these repositories:
暂无开放 Issues,或尚未同步最近议题。