Some checks failed
Build SimApp / build (amd64) (push) Waiting to run
Build SimApp / build (arm64) (push) Waiting to run
CodeQL / Analyze (push) Waiting to run
Build & Push / build (push) Waiting to run
Run Gosec / Gosec (push) Waiting to run
Lint / golangci-lint (push) Waiting to run
Checks dependencies and mocks generation / Check go mod tidy (push) Waiting to run
Checks dependencies and mocks generation / Check up to date mocks (push) Waiting to run
System Tests / setup (push) Waiting to run
System Tests / test-system (push) Blocked by required conditions
System Tests / test-system-legacy (push) Blocked by required conditions
Tests / Code Coverage / split-test-files (push) Waiting to run
Tests / Code Coverage / tests (00) (push) Blocked by required conditions
Tests / Code Coverage / tests (01) (push) Blocked by required conditions
Tests / Code Coverage / tests (02) (push) Blocked by required conditions
Tests / Code Coverage / tests (03) (push) Blocked by required conditions
Tests / Code Coverage / test-integration (push) Waiting to run
Tests / Code Coverage / test-e2e (push) Waiting to run
Tests / Code Coverage / repo-analysis (push) Blocked by required conditions
Tests / Code Coverage / test-sim-nondeterminism (push) Waiting to run
Tests / Code Coverage / test-clientv2 (push) Waiting to run
Tests / Code Coverage / test-core (push) Waiting to run
Tests / Code Coverage / test-depinject (push) Waiting to run
Tests / Code Coverage / test-errors (push) Waiting to run
Tests / Code Coverage / test-math (push) Waiting to run
Tests / Code Coverage / test-schema (push) Waiting to run
Tests / Code Coverage / test-collections (push) Waiting to run
Tests / Code Coverage / test-cosmovisor (push) Waiting to run
Tests / Code Coverage / test-confix (push) Waiting to run
Tests / Code Coverage / test-store (push) Waiting to run
Tests / Code Coverage / test-log (push) Waiting to run
Tests / Code Coverage / test-x-tx (push) Waiting to run
Tests / Code Coverage / test-x-nft (push) Waiting to run
Tests / Code Coverage / test-x-circuit (push) Waiting to run
Tests / Code Coverage / test-x-feegrant (push) Waiting to run
Tests / Code Coverage / test-x-evidence (push) Waiting to run
Tests / Code Coverage / test-x-upgrade (push) Waiting to run
Tests / Code Coverage / test-tools-benchmark (push) Waiting to run
Build & Push SDK Proto Builder / build (push) Has been cancelled
144 lines
3.7 KiB
Go
144 lines
3.7 KiB
Go
// Package math provides helper functions for doing mathematical calculations and parsing for the group module.
|
|
package math
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/cockroachdb/apd/v2"
|
|
|
|
errorsmod "cosmossdk.io/errors"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/group/errors"
|
|
)
|
|
|
|
// Dec is a wrapper struct around apd.Decimal that does no mutation of apd.Decimal's when performing
|
|
// arithmetic, instead creating a new apd.Decimal for every operation ensuring usage is safe.
|
|
//
|
|
// Using apd.Decimal directly can be unsafe because apd operations mutate the underlying Decimal,
|
|
// but when copying the big.Int structure can be shared between Decimal instances causing corruption.
|
|
// This was originally discovered in regen0-network/mainnet#15.
|
|
type Dec struct {
|
|
dec apd.Decimal
|
|
}
|
|
|
|
func NewPositiveDecFromString(s string) (Dec, error) {
|
|
d, err := NewDecFromString(s)
|
|
if err != nil {
|
|
return Dec{}, errors.ErrInvalidDecString.Wrap(err.Error())
|
|
}
|
|
if !d.IsPositive() {
|
|
return Dec{}, errors.ErrInvalidDecString.Wrapf("expected a positive decimal, got %s", s)
|
|
}
|
|
return d, nil
|
|
}
|
|
|
|
func NewNonNegativeDecFromString(s string) (Dec, error) {
|
|
d, err := NewDecFromString(s)
|
|
if err != nil {
|
|
return Dec{}, errors.ErrInvalidDecString.Wrap(err.Error())
|
|
}
|
|
if d.IsNegative() {
|
|
return Dec{}, errors.ErrInvalidDecString.Wrapf("expected a non-negative decimal, got %s", s)
|
|
}
|
|
return d, nil
|
|
}
|
|
|
|
func (x Dec) IsPositive() bool {
|
|
return !x.dec.Negative && !x.dec.IsZero()
|
|
}
|
|
|
|
// NewDecFromString returns a new Dec from a string
|
|
// It only support finite numbers, not NaN, +Inf, -Inf
|
|
func NewDecFromString(s string) (Dec, error) {
|
|
d, _, err := apd.NewFromString(s)
|
|
if err != nil {
|
|
return Dec{}, errors.ErrInvalidDecString.Wrap(err.Error())
|
|
}
|
|
|
|
if d.Form != apd.Finite {
|
|
return Dec{}, errors.ErrInvalidDecString.Wrapf("expected a finite decimal, got %s", s)
|
|
}
|
|
|
|
return Dec{*d}, nil
|
|
}
|
|
|
|
func (x Dec) String() string {
|
|
return x.dec.Text('f')
|
|
}
|
|
|
|
func NewDecFromInt64(x int64) Dec {
|
|
var res Dec
|
|
res.dec.SetInt64(x)
|
|
return res
|
|
}
|
|
|
|
// Add returns a new Dec with value `x+y` without mutating any argument and error if
|
|
// there is an overflow.
|
|
func (x Dec) Add(y Dec) (Dec, error) {
|
|
var z Dec
|
|
_, err := apd.BaseContext.Add(&z.dec, &x.dec, &y.dec)
|
|
return z, errorsmod.Wrap(err, "decimal addition error")
|
|
}
|
|
|
|
// Sub returns a new Dec with value `x-y` without mutating any argument and error if
|
|
// there is an overflow.
|
|
func (x Dec) Sub(y Dec) (Dec, error) {
|
|
var z Dec
|
|
_, err := apd.BaseContext.Sub(&z.dec, &x.dec, &y.dec)
|
|
return z, errorsmod.Wrap(err, "decimal subtraction error")
|
|
}
|
|
|
|
func (x Dec) Int64() (int64, error) {
|
|
return x.dec.Int64()
|
|
}
|
|
|
|
func (x Dec) Cmp(y Dec) int {
|
|
return x.dec.Cmp(&y.dec)
|
|
}
|
|
|
|
func (x Dec) Equal(y Dec) bool {
|
|
return x.dec.Cmp(&y.dec) == 0
|
|
}
|
|
|
|
func (x Dec) IsNegative() bool {
|
|
return x.dec.Negative && !x.dec.IsZero()
|
|
}
|
|
|
|
// Add adds x and y
|
|
func Add(x, y Dec) (Dec, error) {
|
|
return x.Add(y)
|
|
}
|
|
|
|
var dec128Context = apd.Context{
|
|
Precision: 34,
|
|
MaxExponent: apd.MaxExponent,
|
|
MinExponent: apd.MinExponent,
|
|
Traps: apd.DefaultTraps,
|
|
}
|
|
|
|
// Quo returns a new Dec with value `x/y` (formatted as decimal128, 34 digit precision) without mutating any
|
|
// argument and error if there is an overflow.
|
|
func (x Dec) Quo(y Dec) (Dec, error) {
|
|
var z Dec
|
|
_, err := dec128Context.Quo(&z.dec, &x.dec, &y.dec)
|
|
return z, errorsmod.Wrap(err, "decimal quotient error")
|
|
}
|
|
|
|
func (x Dec) IsZero() bool {
|
|
return x.dec.IsZero()
|
|
}
|
|
|
|
// SubNonNegative subtracts the value of y from x and returns the result with
|
|
// arbitrary precision. Returns an error if the result is negative.
|
|
func SubNonNegative(x, y Dec) (Dec, error) {
|
|
z, err := x.Sub(y)
|
|
if err != nil {
|
|
return Dec{}, err
|
|
}
|
|
|
|
if z.IsNegative() {
|
|
return z, fmt.Errorf("result negative during non-negative subtraction")
|
|
}
|
|
|
|
return z, nil
|
|
}
|