dnum

package
v0.0.0-...-2047864 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2024 License: MIT Imports: 5 Imported by: 5

Documentation

Overview

Package dnum implements decimal floating point numbers.

Uses uint64 to hold the coefficient and int8 for exponent. Only uses 16 decimal digits.

Value is sign * .coef * 10^exp, i.e. assumed decimal to left Coefficient is kept "maximized" in 16 decimal digits. Zeroed value is 0.

Index

Constants

This section is empty.

Variables

View Source
var (
	Zero   = Dnum{}
	One    = Dnum{1000_0000_0000_0000, signPos, 1}
	NegOne = Dnum{1000_0000_0000_0000, signNeg, 1}
	PosInf = Dnum{1, signPosInf, 0}
	NegInf = Dnum{1, signNegInf, 0}
)

common values

Functions

func Compare

func Compare(x, y Dnum) int

Compare compares two Dnum's returning -1 for <, 0 for ==, +1 for >

func Equal

func Equal(x, y Dnum) bool

Equal returns true if two Dnum's are equal

Types

type Dnum

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

Dnum is a decimal floating point number

func Add

func Add(x, y Dnum) Dnum

Add returns the sum of two Dnum's

func Div

func Div(x, y Dnum) Dnum

Div returns the quotient of two Dnum's

func FromFloat

func FromFloat(f float64) Dnum

FromFloat converts a float64 to a Dnum

func FromInt

func FromInt(n int64) Dnum

FromInt returns a Dnum for an int

func FromStr

func FromStr(s string) Dnum

FromStr parses a numeric string and returns a Dnum representation. It panics for invalid input.

func Inf

func Inf(sign int8) Dnum

func Mul

func Mul(x, y Dnum) Dnum

Mul returns the product of two Dnum's

func New

func New(sign int8, coef uint64, exp int) Dnum

New constructs a Dnum, maximizing coef and handling exp out of range Used to normalize results of operations

func Raw

func Raw(sign int8, coef uint64, exp int) Dnum

Raw constructs a Dnum without normalizing - arguments must be valid. Used by SuDnum Unpack

func Sub

func Sub(x, y Dnum) Dnum

Sub returns the difference of two Dnum's

func (Dnum) Abs

func (dn Dnum) Abs() Dnum

Abs returns the Dnum with a positive sign

func (Dnum) Coef

func (dn Dnum) Coef() uint64

Coef returns the coefficient

func (Dnum) Exp

func (dn Dnum) Exp() int

Exp returns the exponent

func (Dnum) Format

func (dn Dnum) Format(mask string) string

Format converts a number to a string with a specified format

func (Dnum) Frac

func (dn Dnum) Frac() Dnum

Frac returns the fractional portion, i.e. x - x.Int()

func (Dnum) Hash

func (dn Dnum) Hash() uint64

Hash returns a hash value for a Dnum

func (Dnum) IsInf

func (dn Dnum) IsInf() bool

IsInf returns true if a Dnum is positive or negative infinite

func (Dnum) IsZero

func (dn Dnum) IsZero() bool

IsZero returns true if a Dnum is zero

func (Dnum) Neg

func (dn Dnum) Neg() Dnum

Neg returns the Dnum negated i.e. sign reversed

func (Dnum) Round

func (dn Dnum) Round(r int, mode RoundingMode) Dnum

func (Dnum) Sign

func (dn Dnum) Sign() int

Sign returns -1 for negative, 0 for zero, and +1 for positive

func (Dnum) String

func (dn Dnum) String() string

String returns a string representation of the Dnum

func (Dnum) ToFloat

func (dn Dnum) ToFloat() float64

ToFloat converts a Dnum to float64

func (Dnum) ToInt

func (dn Dnum) ToInt() (int, bool)

func (Dnum) ToInt64

func (dn Dnum) ToInt64() (int64, bool)

ToInt64 converts a Dnum to an int64, returning whether it was convertible

func (Dnum) Trunc

func (dn Dnum) Trunc() Dnum

Trunc returns the integer portion (truncating any fractional part)

type RoundingMode

type RoundingMode int
const (
	Up RoundingMode = iota
	Down
	HalfUp
)

Jump to

Keyboard shortcuts

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