GithubHelp home page GithubHelp logo

yardanico / nim-mathexpr Goto Github PK

View Code? Open in Web Editor NEW
61.0 5.0 6.0 113 KB

Tiny math expression evaluator library implemented in pure Nim

Home Page: https://yardanico.github.io/nim-mathexpr/mathexpr.html

License: MIT License

Nim 100.00%
nim nim-lang math mathematical-expressions mathematical-expressions-evaluator recursive-descent-parser

nim-mathexpr's Introduction

Mathexpr, a math expression evaluator library in Nim nimble

Mathexpr is a relatively small mathematical expression evaluator library written in Nim without any third-party dependencies. It has a lot of predefined math functions and some constants, and you can also define your own.

Installation

To install mathexpr, simply run:

$ nimble install mathexpr

Documentation

https://yardanico.github.io/nim-mathexpr/mathexpr.html

nim-mathexpr's People

Contributors

cmd410 avatar daef avatar yardanico avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

nim-mathexpr's Issues

Incorrect precedence of power operator

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.

Simple sum gives wrong result

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.

support comparison ops and returning bools

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.

all functions with expected len -1 are broken

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.