engine

package
v0.0.0-...-eaeccb9 Latest Latest
Warning

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

Go to latest
Published: May 1, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var InternalVars = map[string]float64{
	"Pi": math.Pi,
	"e":  math.E,
}

InternalVars holds mathematical constants as float64. The engine converts these to *big.Rat at initialisation time. Expressions using these constants (e.g. sin(Pi)) will produce inexact results.

View Source
var OperatorPropsMap = map[string]OperatorProps{
	"+":   {Precedence: 2, RightAssoc: false},
	"-":   {Precedence: 2, RightAssoc: false},
	"*":   {Precedence: 3, RightAssoc: false},
	"/":   {Precedence: 3, RightAssoc: false},
	"^":   {Precedence: 4, RightAssoc: true},
	"-u":  {Precedence: 5, RightAssoc: true},
	"sin": {Precedence: 5, RightAssoc: true},
	"cos": {Precedence: 5, RightAssoc: true},
	"tan": {Precedence: 5, RightAssoc: true},
}

Functions

func CanonicalString

func CanonicalString(tokens []Token) string

func FormatDecimal

func FormatDecimal(r *big.Rat) string

FormatDecimal formats a *big.Rat as a decimal string using %g formatting, matching the previous float64 output behaviour.

func FormatMixed

func FormatMixed(r *big.Rat) string

Types

type AngleMode

type AngleMode int
const (
	Degrees AngleMode = iota
	Radians
)

func (AngleMode) String

func (a AngleMode) String() string

type Engine

type Engine struct {
	AngleMode AngleMode
	// contains filtered or unexported fields
}

func NewEngine

func NewEngine() *Engine

func (*Engine) Calculate

func (e *Engine) Calculate(expression string) (*big.Rat, bool, error)

Calculate evaluates a mathematical expression string. Returns the result as a *big.Rat, a bool indicating whether the result is exact (true = pure rational arithmetic, false = float64 approximation was used e.g. for trig functions or non-integer powers), and any error.

func (*Engine) Derive

func (e *Engine) Derive(postfixTokens []Token, xVal float64, hVal float64) (float64, error)

func (*Engine) EvaluateAt

func (e *Engine) EvaluateAt(postfixTokens []Token, x float64) (float64, error)

func (*Engine) EvaluateWithDerivative

func (e *Engine) EvaluateWithDerivative(postfixTokens []Token, x, h float64) (y, dy float64, err error)

func (*Engine) Parse

func (e *Engine) Parse(expression string) ([]Token, error)

type Evaluator

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

func NewEvaluator

func NewEvaluator(angleMode AngleMode) *Evaluator

func (*Evaluator) Evaluate

func (e *Evaluator) Evaluate(postfixTokens []Token, vars map[string]*big.Rat) (*big.Rat, bool, error)

Evaluate evaluates a postfix token stream. Returns the result as a *big.Rat, a bool indicating whether the result is exact (true) or required a float64 approximation (false), and any error.

type Lexer

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

func NewLexer

func NewLexer() *Lexer

func (*Lexer) Categorise

func (l *Lexer) Categorise(tokenValue string) TokenType

func (*Lexer) ExtractToken

func (l *Lexer) ExtractToken() string

func (*Lexer) Tokenise

func (l *Lexer) Tokenise(input string) ([]Token, error)

type OperatorProps

type OperatorProps struct {
	Precedence int
	RightAssoc bool
}

type Parser

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

func NewParser

func NewParser() *Parser

func (*Parser) Convert

func (p *Parser) Convert(tokens []Token) ([]Token, error)

type Token

type Token struct {
	Type  TokenType
	Value string
}

type TokenType

type TokenType int
const (
	Number TokenType = iota
	Variable
	Operator
	Function
	LeftBrace
	RightBrace
	Negation
	Error
)

func (TokenType) String

func (t TokenType) String() string

Jump to

Keyboard shortcuts

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