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 ¶
Directories
¶
| Path | Synopsis |
|---|---|
|
internal
|
|
|
ast
Package ast declares the types used to represent syntax trees for Go packages.
|
Package ast declares the types used to represent syntax trees for Go packages. |
|
parser
Package parser implements a parser for Go source files.
|
Package parser implements a parser for Go source files. |
|
scanner
Package scanner implements a scanner for Go source text.
|
Package scanner implements a scanner for Go source text. |
|
token
Package token defines constants representing the lexical tokens of the Go programming language and basic operations on tokens (printing, predicates).
|
Package token defines constants representing the lexical tokens of the Go programming language and basic operations on tokens (printing, predicates). |