yardanico / nim-mathexpr Goto Github PK
View Code? Open in Web Editor NEWTiny math expression evaluator library implemented in pure Nim
Home Page: https://yardanico.github.io/nim-mathexpr/mathexpr.html
License: MIT License
Tiny math expression evaluator library implemented in pure Nim
Home Page: https://yardanico.github.io/nim-mathexpr/mathexpr.html
License: MIT License
in REPL:
mathexpr>>> ident
error: Unexpected character ``
tested in mathexpr 1.3.2
https://github.com/Yardanico/nim-mathexpr/blob/master/src/mathexpr.nim#L316
Here, i can see that power operator (^) is parsed at the same level of precedence as multiplication operators, which is not correct from mathematical POV.
This means that: x * y ^ z
will be evaluated as (x * y) ^ z
when it should clearly be x * (y ^ z)
, different interpretations can lead to dramatically differing results, which may not be what user, who are familiar with math, may expect. Coincidentally, I could not find a test case that covers it -_-
The proposed solution is to either parse ^ in parseFactor
level or add another function between parseTerm
and parseFactor
specifically for this operator.
import mathexpr
let e = newEvaluator()
echo e.eval("0.0767999999999999+0")
echo e.eval("0.076799999999999+0")
gives me as output
767999999999999.0
0.076799999999999
The first line is obviously incorrect.
functions like 'min' that checkArgs(-1) are broken because they evaluate true here.
I help myself by patching the existing functions like
e.addFunc("min", proc (a:seq[float]): float = a.min)
but I guess it would be nice (and easy) to fix CheckArgLen.
great library!
any chance of supporting e.g. : eval("a > 22", {"a": 5.0})
?
I can use, e.g.:
proc gt(args: seq[float]): float =
if args[0] <= args[1]: return 0.0
return 1
mathexpr.functions["gt"] = gt
echo eval("gt(a, 4)", {"a": 5.0})
but it would be nice to have support for this. I see that everything is float
as it is now, but thought you might have some ideas on this.
thanks.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.