Categorygithub.com/soudy/mathcat
modulepackage
0.0.0-20201027222343-588f3d377cb9
Repository: https://github.com/soudy/mathcat.git
Documentation: pkg.go.dev

# README

mathcat Build Status GoDoc

mathcat is an expression evaluating library and REPL in Go with basic arithmetic, functions, variables and more.

Features

mathcat doesn't just evaluate basic expressions, it has some tricks up its sleeve. Here's a list with some of its features:

  • Hex literals (0xDEADBEEF)
  • Binary literals (0b1101001)
  • Octal literals (0o126632)
  • Scientific notation (24e3)
  • Variables (with UTF-8 support)
  • Functions (list)
  • Bitwise operators
  • Relational operators
  • Some handy predefined variables
  • Its own REPL

Installation

Library

go get github.com/soudy/mathcat

REPL

go get github.com/soudy/mathcat/cmd/mc

REPL usage

The REPL can be used by simply launching mc:

mc> 8**8
16777216
mc> (8**8) - e # Look, a comment!
16777213.281718171541

Or it can read from stdin like so:

$ echo "3**pi * (6 - -7)" | mc

Arguments

NameDescriptionDefault
precisionbits of decimal precision used in decimal float results64
modetype of literal used as result. can be decimal, hex, binary or octaldecimal

Library usage

There are three different ways to evaluate expressions, the first way is by calling Eval, the second way is by creating a new instance and using Run, and the final way is to use Exec in which you can pass a map with variables to use in the expression.

Eval

If you're not planning on declaring variables, you can use Eval. Eval will evaluate an expression and return its result.

res, err := mathcat.Eval("2 * pi * 5") // pi is a predefined variable
if err != nil {
    // handle errors
}
fmt.Printf("Result: %s\n", res.FloatString(6)) // Result: 31.415927

Run

You can use Run for a more featureful approach. With this method you can assign and use variables across the Parser instance.

p := mathcat.New()
p.Run("a = 1")
p.Run("b = 3")
res, err := p.Run("a + b * b") // 10

Exec

To pass external variables to an expression without using Run, you can use Exec to pass a map of variables.

res, err := mathcat.Exec("a + b * b", map[string]*big.Rat{
    "a": big.NewRat(1, 1),
    "b": big.NewRat(3, 1),
}) // 10

Besides evaluating expressions, mathcat also offers some other handy functions.

GetVar

You can get a defined variable at any time with GetVar.

p := mathcat.New()
p.Run("酷 = -33")
if val, err := p.GetVar("酷"); !err {
    fmt.Printf("%f\n", val) // -33
}

IsValidIdent

Check if a string qualifies as a valid identifier

mathcat.IsValidIdent("a2") // true
mathcat.IsValidIdent("6a") // false

RationalToInteger

Convert a big.Rat to a big.Int. Useful for printing in other bases or when you're only working with integers.

integer := mathcat.RationalToInteger(big.NewRat(42, 1))
fmt.Printf("%#x\n", integer) // prints 0x2a

Supported operators

OperatorDescription
=assignment
+addition
-subtraction
/division
*multiply
**power
%remainder
&bitwise and
|bitwise or
^bitwise xor
<<bitwise left shift
>>bitwise right shift
~bitwise not
==equal
!=not equal
>greater than
>=greater than or equal
<less than
<=less than or equal

All of these except ~ and relational operators also have an assignment variant (+=, -=, **= etc.) that can be used to assign values to variables.

Functions

mathcat has a big list of functions you can use. A function call is invoked like in most programming languages, with an identifier followed by a left parentheses like this: max(5, 10).

FunctionArgumentsDescription
abs(n)1returns the absolute value of given number
sin(n)1returns the sine of given number
cos(n)1returns the cosine of given number
tan(n)1returns the tangent of given number
asin(n)1returns the arcsine of given number
acos(n)1returns the acosine of given number
atan(n)1returns the arctangent of given number
ceil(n)1returns the smallest integer greater than or equal to a given number
floor(n)1returns the largest integer less than or equal to a given number
ln(n)1returns the natural logarithm of given number
log(n)1returns the the decimal logarithm of given number
logn(k, n)2returns the the k logarithm of n
max(a, b)2returns the larger of the two given numbers
min(a, b)2returns the smaller of the two given numbers
sqrt(n)1returns the square root of given number
rand()0returns a random float between 0.0 and 1.0
fact(n)1returns the factorial of given number
list()0list all functions

Predefined variables

There are some handy predefined variables you can use (and change) throughout your expressions:

  • pi
  • tau
  • phi
  • e
  • true (set to 1)
  • false (set to 0)

Documentation

For a more technical description of mathcat, see here.

License

This project is licensed under the MIT License. See the LICENSE file for the full license.

# Packages

No description provided by the author

# Functions

Ceil returns the ceil of a rational number.
Eval evaluates an expression and returns its result and any errors found.
Exec executes an expression with a given map of variables.
Factorial calculates the factorial of rational number n.
Floor returns the floor of a rational number.
Gcd calculates the greatest common divisor of the numbers x and y.
IsValidIdent checks if a string qualifies as a valid identifier.
Lex starts lexing an expression, converting an input string into a stream of tokens later passed on to the parser.
Max gives the maximum of two rational numbers.
Min gives the minimum of two rational numbers.
Mod returns x % y.
New initializes a new Parser instance, useful when you want to run multiple expression and/or use variables.
RationalToInteger converts a rational number to an integer.

# Constants

+.
+=.
&.
&=.
No description provided by the author
No description provided by the author
0b10101101100.
,.
3.
/.
/=.
end of line.
=.
==.
>.
>=.
0xDEADBEEF.
x.
(.
<<.
<<=.
<.
<=.
*.
*=.
~.
!=.
0o666.
|.
|=.
**.
**=.
%.
%=.
).
>>.
>>=.
-.
-=.
-.
^.
^=.

# Variables

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
FunctionNames holds all the function names that are available for use.
RatFalse represents false in boolean operations.
RatTrue represents true in boolean operations.

# Structs

Parser holds the lexed tokens, token position, declared variables and stacks used throughout the parsing of an expression.
Token is an entity in an expression.

# Type aliases

Tokens is a slice of pointers to a token.
TokenType represents the type of token.