meval

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2019 License: MIT Imports: 6 Imported by: 1 module, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Math Evaluator

GoDoc

Math Eval is Yet Another Math Evaluator. for using eval is necessary to create an Evaluator with e := meval.New(). An evaluator contains the variables and the functions e.SetVar("x", 42). For evaluating expressions use the Eval method e.Eval("x+2").

Functions

eval comes with some builtins functions, others may be added in future: Ln, Log10, Log2, Sin, Cos, Tan, Sqrt. e.Eval("ln(2)")

Math Constants

eval comes with some predefine math constants, others may be added in future: Pi, E. e.Eval("pi")

Parser

Meval is currently built around the Golang parser, this was chosen for the semplicity of implementing, in future a custom one could be implemented.

Documentation

Overview

Package Math Eval implements a math evaluator. for using eval is necessary to create an Evaluator with

e := meval.New()

An evaluator contains the variables and the functions

e.SetVar("x", 42)

For evaluating expressions use the Eval method

e.Eval("x+2")

Valid operators are: +, -, *, /, %(remainder), ^(power)

Functions

eval comes with some builtins functions, others may be added in future: ln, log10, log2, log, sin, cos, tan, sqrt

e.Eval("ln(2)")

Math Constants

eval comes with some predefine math constants, others may be added in future: pi, e

e.Eval("pi")
Example
package main

import (
	"fmt"

	"github.com/gSpera/meval"
)

func main() {
	e := meval.New()
	value, _ := e.Eval("2+2")
	fmt.Println(value)
}
Output:
4
Example (Constant)
package main

import (
	"fmt"

	"github.com/gSpera/meval"
)

func main() {
	e := meval.New()
	v, _ := e.Eval("pi")
	fmt.Printf("%.2f\n", v)
}
Output:
3.14
Example (CustomFunction)
package main

import (
	"fmt"

	"github.com/gSpera/meval"
)

func main() {
	e := meval.New()
	sum := func(args ...float64) (float64, error) {
		var sum float64
		for _, arg := range args {
			sum += arg
		}
		return sum, nil
	}
	e.SetFn("sum", sum)

	value, _ := e.Eval("sum(1,2,42)")
	fmt.Println(value)
}
Output:
45
Example (FunctionCall)
package main

import (
	"fmt"

	"github.com/gSpera/meval"
)

func main() {
	e := meval.New()
	value, _ := e.Eval("sqrt(49.0)")
	fmt.Println(value)
}
Output:
7
Example (Log)
package main

import (
	"fmt"

	"github.com/gSpera/meval"
)

func main() {
	e := meval.New()
	v, _ := e.Eval("log(11, 15)")
	fmt.Printf("%.2f\n", v)
}
Output:
1.13
Example (X)
package main

import (
	"fmt"

	"github.com/gSpera/meval"
)

func main() {
	e := meval.New()
	e.SetVar("x", 7)
	value, _ := e.Eval("x * x")
	fmt.Println(value)
}
Output:
49

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Evaluator

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

Evaluator is the enviroment for parsing and evaluating expressions

func New

func New() *Evaluator

func (*Evaluator) Eval

func (e *Evaluator) Eval(input string) (float64, error)

func (*Evaluator) SetFn

func (e *Evaluator) SetFn(name string, fn Function)

func (*Evaluator) SetVar

func (e *Evaluator) SetVar(name string, value float64)

type Function

type Function func(args ...float64) (v float64, err error)

Function if a math function

Jump to

Keyboard shortcuts

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