fixed

package module
v0.0.0-...-ef66458 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2021 License: MIT Imports: 7 Imported by: 0

README

Summary

A fixed place numeric library designed for performance.

All numbers have a fixed 7 decimal places, and the maximum permitted value is +- 99999999999, or just under 100 billion.

The library is safe for concurrent use. It has built-in support for binary and json marshalling.

It is ideally suited for high performance trading financial systems. All common math operations are completed with 0 allocs.

Design Goals

Primarily developed to improve performance in go-trader. Using Fixed rather than decimal.Decimal improves the performance by over 20%, and a lot less GC activity as well. You can review these changes under the 'fixed' branch.

If you review the go-trader code, you will quickly see that I use dot imports for the fixed and common packages. Since this is a "business/user" app and not systems code, this provides 2 major benefits: less verbose code, and I can easily change the implementation of Fixed without changing lots of LOC - just the import statement, and some of the wrapper methods in common.

The fixed.Fixed API uses NaN for reporting errors in the common case, since often code is chained like:

   result := someFixed.Mul(NewS("123.50"))

and this would be a huge pain with error handling. Since all operations involving a NaN result in a NaN, any errors quickly surface anyway.

Performance

BenchmarkAddFixed-2         	2000000000	         0.85 ns/op	       0 B/op	       0 allocs/op
BenchmarkAddDecimal-2       	 3000000	       472 ns/op	     400 B/op	      10 allocs/op
BenchmarkAddBigInt-2        	100000000	        18.8 ns/op	       0 B/op	       0 allocs/op
BenchmarkAddBigFloat-2      	20000000	       109 ns/op	      48 B/op	       1 allocs/op
BenchmarkMulFixed-2         	200000000	         6.14 ns/op	       0 B/op	       0 allocs/op
BenchmarkMulDecimal-2       	20000000	        96.0 ns/op	      80 B/op	       2 allocs/op
BenchmarkMulBigInt-2        	100000000	        22.2 ns/op	       0 B/op	       0 allocs/op
BenchmarkMulBigFloat-2      	30000000	        50.9 ns/op	       0 B/op	       0 allocs/op
BenchmarkDivFixed-2         	100000000	        19.8 ns/op	       0 B/op	       0 allocs/op
BenchmarkDivDecimal-2       	 1000000	      1206 ns/op	     928 B/op	      22 allocs/op
BenchmarkDivBigInt-2        	20000000	        67.6 ns/op	      48 B/op	       1 allocs/op
BenchmarkDivBigFloat-2      	10000000	       148 ns/op	      64 B/op	       2 allocs/op
BenchmarkCmpFixed-2         	2000000000	         0.28 ns/op	       0 B/op	       0 allocs/op
BenchmarkCmpDecimal-2       	100000000	        10.8 ns/op	       0 B/op	       0 allocs/op
BenchmarkCmpBigInt-2        	200000000	         8.10 ns/op	       0 B/op	       0 allocs/op
BenchmarkCmpBigFloat-2      	200000000	         8.39 ns/op	       0 B/op	       0 allocs/op
BenchmarkStringFixed-2      	20000000	        76.1 ns/op	      32 B/op	       1 allocs/op
BenchmarkStringNFixed-2     	20000000	        72.9 ns/op	      32 B/op	       1 allocs/op
BenchmarkStringDecimal-2    	 5000000	       328 ns/op	     144 B/op	       5 allocs/op
BenchmarkStringBigInt-2     	10000000	       212 ns/op	      80 B/op	       3 allocs/op
BenchmarkStringBigFloat-2   	 3000000	       568 ns/op	     272 B/op	       8 allocs/op
BenchmarkWriteTo-2          	20000000	        69.9 ns/op	      27 B/op	       0 allocs/op

The "decimal" above is the common shopspring decimal library

Compatibility with SQL drivers

By default Fixed implements decomposer.Decimal interface for database drivers that support it. To use sql.Scanner and driver.Valuer implementation flag sql_scanner must be specified on build.

Documentation

Index

Constants

View Source
const MAX = float64(99999999999.9999999)

Variables

View Source
var NaN = Fixed{/* contains filtered or unexported fields */}
View Source
var ZERO = Fixed{/* contains filtered or unexported fields */}

Functions

This section is empty.

Types

type Fixed

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

Fixed is a fixed precision 38.24 number (supports 11.7 digits). It supports NaN.

func MustParse

func MustParse(s string) Fixed

MustParse creates a new Fixed from a string, and panics if the string could not be parsed

func NewF

func NewF(f float64) Fixed

NewF creates a Fixed from an float64, rounding at the 8th decimal place

func NewI

func NewI(i int64, n uint) Fixed

NewI creates a Fixed for an integer, moving the decimal point n places to the left For example, NewI(123,1) becomes 12.3. If n > 7, the value is truncated

func NewS

func NewS(s string) Fixed

NewS creates a new Fixed from a string, returning NaN if the string could not be parsed

func NewSErr

func NewSErr(s string) (Fixed, error)

NewSErr creates a new Fixed from a string, returning NaN, and error if the string could not be parsed

func Parse

func Parse(s string) (Fixed, error)

Parse creates a new Fixed from a string, returning NaN, and error if the string could not be parsed. Same as NewSErr but more standard naming

func ReadFrom

func ReadFrom(r io.ByteReader) (Fixed, error)

ReadFrom reads a Fixed from an io.Reader

func (Fixed) Abs

func (f Fixed) Abs() Fixed

Abs returns the absolute value of f. If f is NaN, NaN is returned

func (Fixed) Add

func (f Fixed) Add(f0 Fixed) Fixed

Add adds f0 to f producing a Fixed. If either operand is NaN, NaN is returned

func (Fixed) Cmp

func (f Fixed) Cmp(f0 Fixed) int

Cmp compares two Fixed. If f == f0, return 0. If f > f0, return 1. If f < f0, return -1. If both are NaN, return 0. If f is NaN, return 1. If f0 is NaN, return -1

func (*Fixed) Compose

func (f *Fixed) Compose(form byte, negative bool, coefficient []byte, exponent int32) (err error)

Compose sets the internal decimal value from parts. If the value cannot be represented then an error should be returned.

func (Fixed) Decompose

func (f Fixed) Decompose(buf []byte) (form byte, negative bool, coefficient []byte, exponent int32)

Decompose returns the internal decimal state into parts. If the provided buf has sufficient capacity, buf may be returned as the coefficient with the value set and length set as appropriate.

func (Fixed) Div

func (f Fixed) Div(f0 Fixed) Fixed

Div divides f by f0 returning a Fixed. If either operand is NaN, NaN is returned

func (Fixed) Equal

func (f Fixed) Equal(f0 Fixed) bool

Equal returns true if the f == f0. If either operand is NaN, false is returned. Use IsNaN() to test for NaN

func (Fixed) Float

func (f Fixed) Float() float64

Float converts the Fixed to a float64

func (Fixed) Frac

func (f Fixed) Frac() float64

Frac return the fractional portion of the Fixed, or NaN if NaN

func (Fixed) GreaterThan

func (f Fixed) GreaterThan(f0 Fixed) bool

GreaterThan tests Cmp() for 1

func (Fixed) GreaterThanOrEqual

func (f Fixed) GreaterThanOrEqual(f0 Fixed) bool

GreaterThaOrEqual tests Cmp() for 1 or 0

func (Fixed) Int

func (f Fixed) Int() int64

Int return the integer portion of the Fixed, or 0 if NaN

func (Fixed) IsNaN

func (f Fixed) IsNaN() bool

func (Fixed) IsZero

func (f Fixed) IsZero() bool

func (Fixed) LessThan

func (f Fixed) LessThan(f0 Fixed) bool

LessThan tests Cmp() for -1

func (Fixed) LessThanOrEqual

func (f Fixed) LessThanOrEqual(f0 Fixed) bool

LessThan tests Cmp() for -1 or 0

func (Fixed) MarshalBinary

func (f Fixed) MarshalBinary() (data []byte, err error)

MarshalBinary implements the encoding.BinaryMarshaler interface.

func (Fixed) MarshalJSON

func (f Fixed) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

func (Fixed) Mul

func (f Fixed) Mul(f0 Fixed) Fixed

Mul multiplies f by f0 returning a Fixed. If either operand is NaN, NaN is returned

func (Fixed) Round

func (f Fixed) Round(n int) Fixed

Round returns a rounded (half-up, away from zero) to n decimal places

func (Fixed) Sign

func (f Fixed) Sign() int

Sign returns:

-1 if f <  0
 0 if f == 0 or NaN
+1 if f >  0

func (Fixed) String

func (f Fixed) String() string

String converts a Fixed to a string, dropping trailing zeros

func (Fixed) StringN

func (f Fixed) StringN(decimals int) string

StringN converts a Fixed to a String with a specified number of decimal places, truncating as required

func (Fixed) Sub

func (f Fixed) Sub(f0 Fixed) Fixed

Sub subtracts f0 from f producing a Fixed. If either operand is NaN, NaN is returned

func (*Fixed) UnmarshalBinary

func (f *Fixed) UnmarshalBinary(data []byte) error

UnmarshalBinary implements the encoding.BinaryUnmarshaler interface

func (*Fixed) UnmarshalJSON

func (f *Fixed) UnmarshalJSON(bytes []byte) error

UnmarshalJSON implements the json.Unmarshaler interface.

func (Fixed) WriteTo

func (f Fixed) WriteTo(w io.ByteWriter) error

WriteTo write the Fixed to an io.Writer, returning the number of bytes written

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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