num

package
v1.0.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 17, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package num implements arbitrary-precision decimal numbers for bc.

A Num stores value = unscaled × 10^(-scale), where unscaled is a signed big.Int and scale ≥ 0. All arithmetic matches GNU bc semantics.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Cmp

func Cmp(a, b *Num) int

Cmp compares a and b; returns -1, 0, or 1.

func IsInteger

func IsInteger(n *Num) bool

IsInteger reports whether n has no fractional part.

func Length

func Length(n *Num) int

Length returns the number of significant decimal digits (bc `length` function). Examples: .000001 → 6, 1935.000 → 7.

func ToInt64

func ToInt64(n *Num) int64

ToInt64 returns n truncated toward zero to an integer.

Types

type Num

type Num struct {
	// contains filtered or unexported fields
}

Num is an arbitrary-precision decimal number.

func Abs

func Abs(n *Num) *Num

Abs returns |n|.

func Add

func Add(a, b *Num) *Num

Add returns a + b; result scale = max(a.s, b.s).

func Div

func Div(a, b *Num, prec int) *Num

Div returns a / b truncated toward zero to prec decimal places.

func Epsilon

func Epsilon(n int) *Num

Epsilon returns 10^(-n), used as a convergence threshold in series.

func FromInt64

func FromInt64(n int64) *Num

FromInt64 returns the integer n with scale 0.

func Mod

func Mod(a, b *Num, prec int) *Num

Mod returns a % b with bc semantics:

a%b = a - (a/b)*b  where a/b uses prec scale
result scale = max(prec+scale(b), scale(a))

func Mul

func Mul(a, b *Num) *Num

Mul returns a * b with scale = a.s + b.s (full precision).

func Neg

func Neg(n *Num) *Num

Neg returns -n.

func New

func New() *Num

New returns zero with scale 0.

func Parse

func Parse(s string, ibase int) (*Num, error)

Parse parses s as a decimal number in base ibase (2-16). Digits ≥ ibase are clamped to ibase-1 per bc spec.

func Pow

func Pow(base, exp *Num, scaleVar int) *Num

Pow returns base^exp (exp must be integral).

Scale rules from the spec:

  • if exp < 0: result scale = scaleVar
  • if exp >= 0: result scale = min(scale(base)*exp, max(scaleVar, scale(base)))

func SetScale

func SetScale(n *Num, s int) *Num

SetScale truncates (or zero-pads) n to s decimal places.

func Sqrt

func Sqrt(n *Num, prec int) *Num

Sqrt returns sqrt(n) to prec decimal places using Newton's method.

func Sub

func Sub(a, b *Num) *Num

Sub returns a - b; result scale = max(a.s, b.s).

func (*Num) Clone

func (n *Num) Clone() *Num

Clone returns a deep copy.

func (*Num) Format

func (n *Num) Format(obase int) string

Format formats n in the given output base (2-999).

func (*Num) IsZero

func (n *Num) IsZero() bool

IsZero reports whether n == 0.

func (*Num) Scale

func (n *Num) Scale() int

Scale returns the number of decimal places.

func (*Num) Sign

func (n *Num) Sign() int

Sign returns -1, 0, or 1.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL