Documentation
¶
Overview ¶
Package gomath defines contracts shared by the numeric subpackages.
Example (Decimal) ¶
package main
import (
"context"
"fmt"
gomath "github.com/faustbrian/go-math"
"github.com/faustbrian/go-math/decimal"
)
func main() {
price := decimal.MustParse("19.995")
result, _ := price.Quantize(
context.Background(), 2, gomath.RoundHalfEven, gomath.DefaultLimits(),
)
fmt.Println(result.Value, result.Conditions)
}
Output: 20.00 rounded,inexact
Example (Integer) ¶
package main
import (
"context"
"fmt"
gomath "github.com/faustbrian/go-math"
"github.com/faustbrian/go-math/integer"
)
func main() {
value, _ := integer.Parse("ff", integer.ParseOptions{
Base: 16, Limits: gomath.DefaultLimits(),
})
result, _ := value.Mul(context.Background(), integer.New(2), gomath.DefaultLimits())
fmt.Println(result)
}
Output: 510
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ( // ErrInvalidArgument reports an invalid option or operation argument. ErrInvalidArgument = errors.New("math: invalid argument") // ErrInvalidSyntax reports input outside a type's strict grammar. ErrInvalidSyntax = errors.New("math: invalid syntax") // ErrLimitExceeded reports work rejected by an explicit resource bound. ErrLimitExceeded = errors.New("math: resource limit exceeded") // ErrDivisionByZero reports division by numeric zero. ErrDivisionByZero = errors.New("math: division by zero") // ErrDomain reports an operation outside its mathematical domain. ErrDomain = errors.New("math: domain error") // ErrConversion reports a conversion that cannot be exact. ErrConversion = errors.New("math: inexact conversion") // ErrOverflow reports a result outside an explicitly bounded range. ErrOverflow = errors.New("math: overflow") // ErrUnderflow reports a nonzero result below an explicitly bounded range. ErrUnderflow = errors.New("math: underflow") // ErrTrappedCondition reports a decimal or floating-point condition selected // by an operation context's trap mask. ErrTrappedCondition = errors.New("math: trapped condition") )
View Source
var ErrRandomSource = errors.New("math: random source failed")
ErrRandomSource reports a failure from a caller-provided random source.
Functions ¶
This section is empty.
Types ¶
type Condition ¶
type Condition uint16
Condition is a bit set of arithmetic conditions.
type Limits ¶
type Limits struct {
MaxInputDigits int
MaxOutputDigits int
MaxExponentMagnitude int32
MaxPrecision uint32
MaxPowerExponent uint64
MaxRootDegree uint32
MaxRandomBits int
MaxRandomAttempts int
MaxIntermediateBits int
MaxDecimalExpansion int
// MaxDiagnosticBytes is retained for v1 source compatibility.
// Deprecated: reserved no-op; stable category strings are fixed and bounded.
MaxDiagnosticBytes int
}
Limits bounds parsing, formatting, and potentially expensive arithmetic. It is copied by value and contains no mutable shared state.
func DefaultLimits ¶
func DefaultLimits() Limits
DefaultLimits returns conservative general-purpose resource bounds.
type RoundingMode ¶
type RoundingMode uint8
RoundingMode selects how a discarded nonzero remainder affects a result.
const ( RoundHalfEven RoundingMode = iota RoundHalfUp RoundHalfDown RoundDown RoundUp RoundCeiling RoundFloor )
func (RoundingMode) String ¶
func (r RoundingMode) String() string
String returns the stable configuration name of r.
func (RoundingMode) Valid ¶
func (r RoundingMode) Valid() bool
Valid reports whether r is a supported rounding mode.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package bigfloat provides immutable arbitrary-precision binary floating-point values.
|
Package bigfloat provides immutable arbitrary-precision binary floating-point values. |
|
Package decimal provides immutable finite base-10 decimals with explicit precision, rounding, exponent, trap, condition, and resource policies.
|
Package decimal provides immutable finite base-10 decimals with explicit precision, rounding, exponent, trap, condition, and resource policies. |
|
Package encoding provides optional deterministic codecs for math values.
|
Package encoding provides optional deterministic codecs for math values. |
|
Package integer provides immutable arbitrary-precision signed integers.
|
Package integer provides immutable arbitrary-precision signed integers. |
|
Package mathtest provides reusable assertions for numeric laws and codecs.
|
Package mathtest provides reusable assertions for numeric laws and codecs. |
|
Package rational provides immutable exact fractions.
|
Package rational provides immutable exact fractions. |
Click to show internal directories.
Click to hide internal directories.