fixed

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2023 License: Apache-2.0 Imports: 5 Imported by: 7

README

Fixed-point math library

The library implements basic math operations for 72/56 fixed-point values, and Log, Exp, Lgamma, BinCDF functions.

Motivation

There is no efficient golang implementation for fixed-point math with a high-precision bincdf function.

The library uses a 56-bit fractional part to achieve maximum performance on math functions like Log, Exp, and Lgamma.

Implementation

BinCDF is implemented with incomplete Beta function in standart way:

Iₓ(a,b) = (xᵃ*(1-x)ᵇ)/(a*B(a,b)) * (1/(1+(d₁/(1+(d₂/(1+...))))))   
(xᵃ*(1-x)ᵇ)/B(a,b) = exp(lgamma(a+b) - lgamma(a) - lgamma(b) + a*log(x) + b*log(1-x))   
d_{2m+1} = -(a+m)(a+b+m)x/((a+2m)(a+2m+1))   
d_{2m}   = m(b-m)x/((a+2m-1)(a+2m))   

Fixed-point arithmetics and Log, Exp, Lgamma functions are implemented with the bits go module and raw operations over 56-bit fractional part values in range [-7..7].

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	One  = fixedOne
	Zero = Fixed{}
)
View Source
var ErrOverflow = errors.New("overflow")

Functions

This section is empty.

Types

type Fixed

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

72/56 fixed-point value

func BinCDF

func BinCDF(n int, p Fixed, x int) Fixed

func BinCDF64

func BinCDF64(n int64, p Fixed, x int64) Fixed

func Div64

func Div64(p, q int64) Fixed

Div64 creates new Fixed equal to p/q signed result

func DivUint64

func DivUint64(p, q uint64) Fixed

func FracFromBytes

func FracFromBytes(x []byte) Fixed

FracFromBytes takes only fractional part from bytes array and return fixed value

func From

func From(val float64) Fixed

func FromBytes

func FromBytes(x []byte) Fixed

FromBytes creates fixed value from bytes array

func New

func New(val int) Fixed

func New64

func New64(val int64) Fixed

func (Fixed) Abs

func (x Fixed) Abs() Fixed

func (Fixed) Add

func (x Fixed) Add(y Fixed) Fixed

func (Fixed) Bytes

func (x Fixed) Bytes() []byte

Bytes converts fixed value into bytes array

func (Fixed) Ceil

func (x Fixed) Ceil() int64

func (Fixed) Div

func (x Fixed) Div(y Fixed) Fixed

func (Fixed) EqualTo

func (x Fixed) EqualTo(y Fixed) bool

EqualTo compares fixed values and returns true if x == y

func (Fixed) Float

func (x Fixed) Float() float64

func (Fixed) Floor

func (x Fixed) Floor() int64

func (Fixed) GreaterThan

func (x Fixed) GreaterThan(y Fixed) bool

GreaterThan compares fixed values and returns true if x > y

func (Fixed) LessThan

func (x Fixed) LessThan(y Fixed) bool

func (Fixed) Mul

func (x Fixed) Mul(y Fixed) Fixed

func (Fixed) Neg

func (x Fixed) Neg() Fixed

func (Fixed) Round

func (x Fixed) Round() int64

func (Fixed) String

func (x Fixed) String() string

func (Fixed) Sub

func (x Fixed) Sub(y Fixed) Fixed

Jump to

Keyboard shortcuts

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