formula

package module
v2.0.0-alpha.7 Latest Latest
Warning

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

Go to latest
Published: May 26, 2020 License: MIT Imports: 10 Imported by: 9

Documentation

Overview

Package formula implements very simple but fast formula parsing and evaluating. It uses the AST package to parse expressions in the formula which it transforms to executable functions.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ErrInsufficientArgs

type ErrInsufficientArgs struct {
	// Func is the name of the function.
	Func string
	// Pos is the character position of Func.
	Pos int
	// Actual is the number of arguments provided to Func.
	Actual int
	// Expected is the minimum number of arguments expected by Func.
	Expected int
}

ErrInsufficientArgs is returned when a function in a formula requires more arguments than that provided.

func (*ErrInsufficientArgs) Error

func (e *ErrInsufficientArgs) Error() string

Error implements error.

type ErrPanic

type ErrPanic struct {
	// Func is the name of the function.
	Func string
	// Pos is the character position of Func.
	Pos int
	// Reason for panic.
	Reason string
	// Filename of where panic occurred.
	File string
	// Line number of where panic occurred.
	Line int
}

ErrPanic is returned when a registered function panics.

func (*ErrPanic) Error

func (e *ErrPanic) Error() string

Error implements error.

type ErrUnknownFunc

type ErrUnknownFunc struct {
	// Func is the name of the unknown function encountered.
	Func string
	// Pos is the character position of the unknown Func.
	Pos int
}

ErrUnknownFunc is returned when a formula contains an unrecognized function.

func (*ErrUnknownFunc) Error

func (e *ErrUnknownFunc) Error() string

Error implements error.

type ErrUnknownVariable

type ErrUnknownVariable struct {
	// Var is the name of the unknown variable or constant.
	Var string
	// Pos is the character position of the unknown variable or constant.
	Pos int
}

ErrUnknownVariable is returned when a formula contains an unrecognized variable or constant.

func (*ErrUnknownVariable) Error

func (e *ErrUnknownVariable) Error() string

Error implements error.

type Formula

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

Formula is a parsed formula that is ready to be evaluated. It is safe to use concurrently from multiple goroutines.

func New

func New(formula string) (*Formula, error)

New returns a new formula for a given string. The formula is parsed and may be evaluated if parsed successfully. If not successful, an error is returned and the formula is nil.

Most functions from the math package that return a single float64 are supported. The equivalent function name is all lower-cased. Therefore RoundToEven becomes roundtoeven. See https://golang.org/pkg/math/.

func (*Formula) Eval

func (formula *Formula) Eval(variables ...Variable) (float64, error)

Eval evaluates a formula using the variables passed. If an unknown variable/constant or function is encountered, ErrUnknownVariable or ErrUnknownFunc is returned respectively. If a known function is passed with too few arguments, ErrInsufficientArgs is returned.

Some special math constants are already included. They are automatically defined unless over-ridden by variables. These are: π, 𝜋, pi, Φ, phi, e, E.

func (*Formula) MustEval

func (formula *Formula) MustEval(variables ...Variable) float64

MustEval calls Eval but panics if Eval returns an error.

func (*Formula) RegisterFunc

func (formula *Formula) RegisterFunc(name string, paramCount int, f func(args ...float64) float64)

RegisterFunc registers a custom function to be usable by the formula. This function allows an arbitrary number of input floats and one output float. The paramCount passed indicates the number of input floats expected. If less than the required paramCount arguments are passed to the function, Eval will return an ErrInsufficientArgs error. The function does not need to internally check the correct arg length. Functions must be registered with the formula before evaluating.

Example:

// Add sinc function: https://en.wikipedia.org/wiki/Sinc_function
RegisterFunc("sinc", 1, func(args ...float64) float64 {
   if args[0] == 0 {
      return 1
   }
   return math.Sin(args[0]) / args[0]
})

type Variable

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

Variable represents a variable with a specific name and value, that may be passed to a formula.

func Var

func Var(name string, value interface{}) Variable

Var returns a new variable that may be passed to a formula when evaluating it. All variables in the formula with that name will then adapt the value of the variable. The value passed must be a numeric value. If the value is not numeric, the function panics.

Jump to

Keyboard shortcuts

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