Categorygithub.com/cockroachdb/apd/v3
modulepackage
3.2.1
Repository: https://github.com/cockroachdb/apd.git
Documentation: pkg.go.dev

# README

apd

apd is an arbitrary-precision decimal package for Go.

apd implements much of the decimal specification from the General Decimal Arithmetic description. This is the same specification implemented by python’s decimal module and GCC’s decimal extension.

Features

  • Panic-free operation. The math/big types don’t return errors, and instead panic under some conditions that are documented. This requires users to validate the inputs before using them. Meanwhile, we’d like our decimal operations to have more failure modes and more input requirements than the math/big types, so using that API would be difficult. apd instead returns errors when needed.
  • Support for standard functions. sqrt, ln, pow, etc.
  • Accurate and configurable precision. Operations will use enough internal precision to produce a correct result at the requested precision. Precision is set by a "context" structure that accompanies the function arguments, as discussed in the next section.
  • Good performance. Operations will either be fast enough or will produce an error if they will be slow. This prevents edge-case operations from consuming lots of CPU or memory.
  • Condition flags and traps. All operations will report whether their result is exact, is rounded, is over- or under-flowed, is subnormal, or is some other condition. apd supports traps which will trigger an error on any of these conditions. This makes it possible to guarantee exactness in computations, if needed.

apd has three main types.

The first is BigInt which is a wrapper around big.Int that exposes an identical API while reducing memory allocations. BigInt does so by using an inline array to back the big.Int's variable-length value when the integer's absolute value is sufficiently small. BigInt also contains fast-paths that allow it to perform basic arithmetic directly on this inline array, only falling back to big.Int when the arithmetic gets complex or takes place on large values.

The second is Decimal which holds the values of decimals. It is simple and uses a BigInt with an exponent to describe values. Most operations on Decimals can’t produce errors as they work directly on the underlying big.Int. Notably, however, there are no arithmetic operations on Decimals.

The third main type is Context, which is where all arithmetic operations are defined. A Context describes the precision, range, and some other restrictions during operations. These operations can all produce failures, and so return errors.

Context operations, in addition to errors, return a Condition, which is a bitfield of flags that occurred during an operation. These include overflow, underflow, inexact, rounded, and others. The Traps field of a Context can be set which will produce an error if the corresponding flag occurs. An example of this is given below.

See the examples for some operations that were previously difficult to perform in Go.

Documentation

https://pkg.go.dev/github.com/cockroachdb/apd/v3?tab=doc

# Functions

MakeErrDecimal creates a ErrDecimal with given context.
New creates a new decimal with the given coefficient and exponent.
NewBigInt allocates and returns a new BigInt set to x.
NewFromString creates a new decimal from s.
NewWithBigInt creates a new decimal with the given coefficient and exponent.
NumDigits returns the number of decimal digits of b.

# Constants

Clamped is raised when the exponent of a result has been altered or constrained in order to fit the constraints of the Decimal representation.
DefaultTraps is the default trap set used by BaseContext.
DivisionByZero is raised when a non-zero dividend is divided by zero.
DivisionImpossible is raised when integer division cannot be exactly represented with the given precision.
DivisionUndefined is raised when both division operands are 0.
Finite is the finite form.
Inexact is raised when a result is not exact (one or more non-zero coefficient digits were discarded during rounding).
Infinite is the infinite form.
InvalidOperation is raised when a result would be undefined or impossible.
MaxExponent is the highest exponent supported.
MinExponent is the lowest exponent supported with the same limitations as MaxExponent.
NaN is the NaN form.
NaNSignaling is the signaling NaN form.
Overflow is raised when the exponent of a result is too large to be represented.
Round05Up rounds zero or five away from 0; same as round-up, except that rounding up only occurs if the digit to be rounded up is 0 or 5.
RoundCeiling towards +Inf: rounds up if digits are > 0 and the number is positive.
RoundDown rounds toward 0; truncate.
Rounded is raised when a result has been rounded (that is, some zero or non-zero coefficient digits were discarded).
RoundFloor towards -Inf: rounds up if digits are > 0 and the number is negative.
RoundHalfDown rounds up if the digits are > 0.5.
RoundHalfEven rounds up if the digits are > 0.5.
RoundHalfUp rounds up if the digits are >= 0.5.
RoundUp rounds away from 0.
Subnormal is raised when a result is subnormal (its adjusted exponent is less than Emin), before any rounding.
SystemOverflow is raised when an exponent is greater than MaxExponent.
SystemUnderflow is raised when an exponent is less than MinExponent.
Underflow is raised when a result is both subnormal and inexact.

# Variables

BaseContext is a useful default Context.

# Structs

BigInt is a wrapper around big.Int.
Context maintains options for Decimal operations.
Decimal is an arbitrary-precision decimal.
ErrDecimal performs operations on decimals and collects errors during operations.
NullDecimal represents a string that may be null.

# Type aliases

Condition holds condition flags.
Form specifies the form of a Decimal.
Rounder specifies the behavior of rounding.