fixed

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

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

Go to latest
Published: May 14, 2019 License: MIT Imports: 8 Imported by: 0

README

Summary

A fixed place numeric library with overflow check in Go designed for performance.

All numbers have a fixed 8 decimal places, and the maximum permitted value is + 9999999999, or just under 10 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.

Performance

goos: darwin
goarch: amd64
pkg: github.com/cryptowrold/fixed
BenchmarkAddFixed-4         	2000000000	         0.93 ns/op	       0 B/op	       0 allocs/op
BenchmarkAddDecimal-4       	 5000000	       338 ns/op	     176 B/op	       8 allocs/op
BenchmarkAddBigInt-4        	100000000	        20.8 ns/op	       0 B/op	       0 allocs/op
BenchmarkAddBigFloat-4      	10000000	       114 ns/op	      48 B/op	       1 allocs/op
BenchmarkMulFixed-4         	200000000	         6.66 ns/op	       0 B/op	       0 allocs/op
BenchmarkMulDecimal-4       	10000000	       105 ns/op	      80 B/op	       2 allocs/op
BenchmarkMulBigInt-4        	100000000	        24.0 ns/op	       0 B/op	       0 allocs/op
BenchmarkMulBigFloat-4      	30000000	        52.7 ns/op	       0 B/op	       0 allocs/op
BenchmarkDivFixed-4         	200000000	         6.76 ns/op	       0 B/op	       0 allocs/op
BenchmarkDivDecimal-4       	 1000000	      1105 ns/op	     568 B/op	      21 allocs/op
BenchmarkDivBigInt-4        	20000000	        63.8 ns/op	       8 B/op	       1 allocs/op
BenchmarkDivBigFloat-4      	10000000	       153 ns/op	      24 B/op	       2 allocs/op
BenchmarkCmpFixed-4         	2000000000	         0.51 ns/op	       0 B/op	       0 allocs/op
BenchmarkCmpDecimal-4       	100000000	        11.1 ns/op	       0 B/op	       0 allocs/op
BenchmarkCmpBigInt-4        	200000000	         7.68 ns/op	       0 B/op	       0 allocs/op
BenchmarkCmpBigFloat-4      	200000000	         7.24 ns/op	       0 B/op	       0 allocs/op
BenchmarkStringFixed-4      	20000000	        71.9 ns/op	      32 B/op	       1 allocs/op
BenchmarkStringNFixed-4     	20000000	        72.6 ns/op	      32 B/op	       1 allocs/op
BenchmarkStringDecimal-4    	 5000000	       308 ns/op	      64 B/op	       5 allocs/op
BenchmarkStringBigInt-4     	10000000	       171 ns/op	      24 B/op	       2 allocs/op
BenchmarkStringBigFloat-4   	 3000000	       571 ns/op	     192 B/op	       8 allocs/op
BenchmarkWriteTo-4          	30000000	        52.4 ns/op	      18 B/op	       0 allocs/op

The "decimal" above is the common shopspring decimal library

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	NaN   = Fixed{/* contains filtered or unexported fields */}
	ZERO  = Fixed{/* contains filtered or unexported fields */}
	ONE   = Fixed{/* contains filtered or unexported fields */}
	TWO   = Fixed{/* contains filtered or unexported fields */}
	THREE = Fixed{/* contains filtered or unexported fields */}
	FOUR  = Fixed{/* contains filtered or unexported fields */}
	FIVE  = Fixed{/* contains filtered or unexported fields */}
	SIX   = Fixed{/* contains filtered or unexported fields */}
	SEVEN = Fixed{/* contains filtered or unexported fields */}
	EIGHT = Fixed{/* contains filtered or unexported fields */}
	NINE  = Fixed{/* contains filtered or unexported fields */}
	TEN   = Fixed{/* contains filtered or unexported fields */}
	MAX   = 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 10.8 digits). It supports NaN.

func NewFromFloat

func NewFromFloat(f float64) Fixed

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

func NewFromOriginal

func NewFromOriginal(i uint64) Fixed

NewFromOriginal creates a Fixed for an fixed original integer, moving the decimal point n places to the left For example, NewFromOriginal(123) becomes 0.00000123.

func NewFromString

func NewFromString(s string) Fixed

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

func NewFromStringErr

func NewFromStringErr(s string) (Fixed, error)

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

func NewFromUint

func NewFromUint(i uint64) Fixed

NewFromUint creates a Fixed from an uint64

func NewFromUintWithExponent

func NewFromUintWithExponent(i uint64, n uint) Fixed

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

func ReadFrom

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

ReadFrom reads a Fixed from an io.Reader

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) 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) 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) Original

func (f Fixed) Original() uint64

Original return the original digital of the Fixed,

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) UInt

func (f Fixed) UInt() uint64

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

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

Jump to

Keyboard shortcuts

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