Documentation
¶
Overview ¶
Package num provides error-carrying decimal arithmetic with fixed policies. Num keeps small coefficients inline and uses private apd storage for larger values and rounded division. It preserves input precision and supports JSON, XML, SQL, text, and Go's fmt package. Values are immutable: assignment, arithmetic, and formatting never mutate another copy. Num is deliberately not comparable; use Num.Equal or Num.Cmp.
Calculations and rounding ¶
Constructors preserve decimal values and fractional trailing zeros. Addition and subtraction are exact and retain the greater operand scale; multiplication is exact and retains the sum of the scales. Capacity errors are reported instead of rounding an exact result to fit.
Division is a rounding boundary. Num.Div rounds half-even (banker's rounding) to max(34, meaningfulDigits(left), meaningfulDigits(right)) significant digits, then removes unnecessary trailing zeros. Meaningful digits are the coefficient digits after removing trailing zeros from a temporary copy; zero counts as one digit. Equivalent values such as 1.23 and 1.2300 select the same precision. The 34-digit floor follows the decimal128 precision convention; it is a package policy rather than a claim that every later calculation will be correctly rounded. Repeating quotients succeed. Terminating quotients exceeding the selected precision also round.
Num.Quantize is an explicit rounding boundary: Quantize(2) rounds half-even or pads zeros to exactly two fractional places. Half-even is the only rounding rule: exact ties choose the even final digit, for either sign. There are no public contexts, rounding-mode parameters, or process-global scale settings. Negative zero becomes positive zero, retaining any intentional fractional scale. Comparisons ignore scale; callers must check Num.Ok before comparing.
Arithmetic carries errors through chains so callers can check the final value:
pnl := exitPrice.Sub(entryPrice).Mul(quantity)
settlement := pnl.Quantize(2)
if !settlement.Ok() {
return fmt.Errorf("settlement: %w", settlement.Err)
}
Formatting ¶
Formatting selects presentation without changing the value. Num.String, %v, and %s produce the complete stored representation in plain decimal notation. %q quotes that representation. %f and %F select fixed-point display with half-even rounding; their default precision is six fractional places. Width and precision may be supplied dynamically:
n := num.FromString("123.456789")
fmt.Sprintf("%.2f", n) // "123.46"
fmt.Sprintf("%10.2f", n) // " 123.46"
fmt.Sprintf("%.*f", 4, n) // "123.4568"
n.String() // "123.456789", unchanged
Numeric formatting supports +, space, -, and 0 flags. Plus takes precedence over space; left alignment takes precedence over zero-padding; zeros follow the sign. Textual forms support width and alignment, but reject precision rather than truncate numeric text. Unsupported verbs (including %e and %g), unsupported flags, and excessive rendering requests produce bounded fmt-style diagnostics. Numeric formatting of invalid values includes an error or uninitialized marker; textual formatting preserves that marker. Formatting never converts through float64.
Capacity and input ¶
Stored values support at most 4,096 coefficient digits (including retained trailing zeros), 4,096 integer digits, and a fractional scale from 0 through 4,096. Positive exponents are normalized to scale zero before validation. Construction and exact arithmetic never silently round to satisfy capacity. Division results must also fit after unnecessary trailing zeros are removed.
Numeric input is limited to 16,384 decoded bytes. Accepted forms are finite decimal literals with an optional sign, fractional part, and decimal exponent; forms such as .5 and 1. are accepted. Embedded or surrounding numeric-text whitespace, underscores, nondecimal syntax, NaN, and infinities are rejected. JSON permits its own surrounding whitespace and standard string escapes. Float64 construction uses the shortest round-trip decimal representation of the supplied float, not its exact binary expansion.
Rendering separately permits up to 4,096 fractional places, field width 16,384, and 16,384 output bytes. Formatting padding may exceed stored-value coefficient capacity because it is presentation only.
Errors and initialization ¶
Num{} is uninitialized, not numeric zero: its Err field is nil but Ok is false. Use Zero for an initialized scale-zero zero. Arithmetic and serialization reject uninitialized operands rather than treating them as zero. Errors from invalid input, capacity limits, invalid quantization scale, and division by zero propagate through arithmetic. Ordinary division or quantization rounding succeeds. Existing carried errors take precedence over later calculation. Use errors.Is to identify ErrInvalidNumericString, ErrUninitialized, ErrInvalidScale, ErrLimitExceeded, or ErrDivisionByZero. Unsupported SQL scan sources report ErrUnsupportedType. Serialization failures wrap ErrMarshalErrorState together with the underlying numeric error.
Serialization and nullable values ¶
JSON output is a bare exact decimal number, including stored trailing zeros. Num.UnmarshalJSON accepts quoted and bare input; parse failures set Num.Err and return nil so unrelated struct fields can still decode. JSON null is invalid for Num. Text/XML and SQL encode exact decimal text without rounding; SQL Num.Value returns a string. Text and SQL decoding return their errors. Failed decoding clears stale numeric state.
Direct JSON decoding into Num preserves precision. Generic Go decoding into any uses float64 by default, and JavaScript numbers use binary floating point; these consumers can lose decimal precision. Go consumers handling generic JSON can call encoding/json.Decoder.UseNumber and pass json.Number.String() to FromString. Output does not switch between strings and numbers by size.
NullNum represents presence separately from numeric validity. JSON null, SQL NULL, and empty text set Valid=false; absent JSON output is null. A present malformed JSON number has Valid=true and a carried Num error. Failed text or SQL decoding returns an error and leaves Valid=false. A missing nullable struct field remains absent. SQL scanning accepts string, []byte, int64, uint64, float64, and float32; float32 is first converted to float64.
Index ¶
- type Error
- type NullNum
- func (nn NullNum) MarshalJSON() ([]byte, error)
- func (nn NullNum) MarshalText() ([]byte, error)
- func (nn *NullNum) Scan(src any) error
- func (nn NullNum) String() string
- func (nn *NullNum) UnmarshalJSON(data []byte) error
- func (nn *NullNum) UnmarshalText(data []byte) error
- func (nn NullNum) Value() (driver.Value, error)
- type Num
- func (n Num) Abs() Num
- func (n Num) Add(other Num) Num
- func (n Num) Cmp(other Num) int
- func (n Num) Div(other Num) Num
- func (n Num) Equal(other Num) bool
- func (n Num) Format(state fmt.State, verb rune)
- func (n Num) GreaterThan(other Num) bool
- func (n Num) IsZero() bool
- func (n Num) LessThan(other Num) bool
- func (n Num) MarshalJSON() ([]byte, error)
- func (n Num) MarshalText() ([]byte, error)
- func (n Num) Mul(other Num) Num
- func (n Num) Neg() Num
- func (n Num) Ok() bool
- func (n Num) Quantize(scale int) Num
- func (n *Num) Scan(src any) error
- func (n Num) String() string
- func (n Num) Sub(other Num) Num
- func (n *Num) UnmarshalJSON(data []byte) error
- func (n *Num) UnmarshalText(data []byte) error
- func (n Num) Value() (driver.Value, error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Error ¶
type Error string
Error is a sentinel error type for the num package.
const ( ErrInvalidNumericString Error = "num: invalid numeric string" ErrUnsupportedType Error = "num: unsupported type" ErrMarshalErrorState Error = "num: cannot marshal error-state value" ErrUninitialized Error = "num: uninitialized value" ErrInvalidScale Error = "num: invalid scale" ErrLimitExceeded Error = "num: numeric limit exceeded" ErrDivisionByZero Error = "num: division by zero" )
type NullNum ¶
NullNum represents a Num that may be null.
Example ¶
package main
import (
"encoding/json"
"fmt"
"go.trippwill.dev/num"
)
func main() {
type Position struct {
Price num.Num `json:"price"`
Strike num.NullNum `json:"strike"`
}
data := []byte(`{"price": 42.50, "strike": null}`)
var pos Position
_ = json.Unmarshal(data, &pos)
fmt.Printf("price=%s strike.valid=%v\n", pos.Price, pos.Strike.Valid)
}
Output: price=42.50 strike.valid=false
func (NullNum) MarshalJSON ¶
MarshalJSON returns JSON null when !Valid, otherwise delegates to Num.MarshalJSON.
func (NullNum) MarshalText ¶
MarshalText returns an empty byte slice when !Valid, otherwise delegates to Num.MarshalText.
func (*NullNum) Scan ¶
Scan implements database/sql.Scanner. Handles nil → Valid=false.
func (*NullNum) UnmarshalJSON ¶
UnmarshalJSON handles JSON null → Valid=false, otherwise delegates to Num.UnmarshalJSON.
func (*NullNum) UnmarshalText ¶
UnmarshalText handles empty input → Valid=false, otherwise delegates to Num.UnmarshalText.
type Num ¶
type Num struct {
Err error
// contains filtered or unexported fields
}
Num is an immutable, error-carrying finite decimal. Its zero value is uninitialized; use Zero or a constructor for a valid numeric zero.
Example (ErrorChain) ¶
package main
import (
"fmt"
"go.trippwill.dev/num"
)
func main() {
exit := num.FromString("150.00")
entry := num.FromString("100.00")
qty := num.FromInt64(10)
pnl := exit.Sub(entry).Mul(qty)
if !pnl.Ok() {
fmt.Println("error:", pnl.Err)
return
}
fmt.Println(pnl)
}
Output: 500.00
func FromFloat64 ¶
FromFloat64 interprets v using its shortest round-trip decimal spelling, rather than the exact binary fraction. NaN and infinities are invalid.
func FromString ¶
FromString constructs an exact decimal, retaining fractional trailing zeros. Malformed, nonfinite, or out-of-capacity inputs produce an error-state Num.
Example ¶
package main
import (
"fmt"
"go.trippwill.dev/num"
)
func main() {
n := num.FromString("123.45")
fmt.Println(n)
}
Output: 123.45
func (Num) Add ¶
Add returns the exact sum, retaining the greater operand scale.
Example ¶
package main
import (
"fmt"
"go.trippwill.dev/num"
)
func main() {
price := num.FromString("0.1")
tax := num.FromString("0.2")
total := price.Add(tax)
fmt.Println(total)
}
Output: 0.3
func (Num) Div ¶
Div divides n by other using half-even rounding. Its precision is the greater of 34 and each operand's coefficient digits after removing trailing zeros. Repeating expansions succeed; a zero divisor or exceeded capacity does not.
func (Num) Format ¶ added in v0.3.0
Format implements fmt.Formatter. The f and F verbs round half-even to the requested number of fractional digits, defaulting to six, without changing n. They support width and the +, space, -, and 0 flags. The v and s verbs print the complete Num.String representation; q quotes it as a Go string. These text verbs support width and the - flag, and reject precision.
Precision is limited to 4096, width to 16384, and each formatted value to 16384 bytes. Unsupported formatting and invalid numeric states produce fmt diagnostics. Text verbs retain the error or uninitialized String marker.
func (Num) GreaterThan ¶
GreaterThan reports numeric ordering. Check both Ok first.
func (Num) MarshalJSON ¶
MarshalJSON encodes n as an exact bare JSON number, retaining stored scale. No presentation formatting or binary floating-point conversion is involved.
Example ¶
package main
import (
"encoding/json"
"fmt"
"go.trippwill.dev/num"
)
func main() {
n := num.FromString("42.50")
data, _ := json.Marshal(n)
fmt.Println(string(data))
}
Output: 42.50
func (Num) MarshalText ¶
MarshalText returns exact decimal text for text and XML encoders.
func (Num) Quantize ¶ added in v0.3.0
Quantize rounds half-even or pads zeros to exactly scale fractional places. Scale must be between 0 and 4096 and the result must fit Num's capacity.
Example ¶
package main
import (
"fmt"
"go.trippwill.dev/num"
)
func main() {
price := num.FromString("1.235")
fmt.Println(price.Quantize(2))
fmt.Printf("display=%.2f exact=%s\n", price, price)
}
Output: 1.24 display=1.24 exact=1.235
func (*Num) Scan ¶
Scan implements database/sql.Scanner. Accepted types are string, []byte, int64, uint64, float64, and float32. Floats use shortest decimal conversion; float32 first converts to float64, preserving the earlier driver contract.
func (*Num) UnmarshalJSON ¶
UnmarshalJSON accepts numeric tokens and quoted decimal strings. Invalid values clear the previous number and carry Err, allowing partial struct decode.
func (*Num) UnmarshalText ¶
UnmarshalText parses exact decimal text, clearing stale state on failure.