NTDLS / CMathParser

A fairly robust mathematics parsing engine for C++ projects.
MIT License
21 stars 6 forks source link

Variable support #1

Closed ClutchplateDude closed 2 years ago

ClutchplateDude commented 4 years ago

This looks super interesting for a OSS project I'm working on (and it looks like nice, clean code). However I need to support variables. Basically I'll have a number of these expressions and some will use expressions or literals to define variables and yet other expressions will use these variables, etc. etc. Have you thought about supporting that? If not, where would be the best place to inject variable detection and handling in this code?

NTDLS commented 3 years ago

@ClutchplateDude, first of all: thanks for the issue! I love getting these, second of all: I have no idea why I'm not getting notified! I'll fix that. On to the issue I use this math parser in another project (the project it was designed for) which uses variables. Unless I am misunderstanding your question, then I think you should just be able to build an expression with string variables and do string replacements before calculating.

Lets assume that $HourlySalary$ is suppose to be 25.50.

So the weekly could be calculated as ($HourlySalary$ * 40)

Then before passing to the math parser, you could just replace $HourlySalary$ with 25.50.

ClutchplateDude commented 3 years ago

Hey @NTDLS, wasn't expecting to hear back :-) .... so thanks. I know I could do that, but it would be way easier if the library just called me with the variable name. That way I don't have to do a find/replace at every location where I use the parser. My use case is that I have many input fields where a user can enter numbers or formulas and I have a table of variables. It's been a while since I looked at the code, but if you think that this is not suited for your library, I can see if I can make a more compelling case.

NTDLS commented 3 years ago

@ClutchplateDude I think its a great idea. I'll see if I can bake it in today. I really wish I would have seen this... EIGHT MONTHS AGO. 😅

ClutchplateDude commented 3 years ago

Oh, cool! It would be great if there was no need for a variable delimiter. You should just be able to detect letters and assume they are variables if they don't match any built-in functions. Stretch goal: detect that the token is a function call and have a second callback for functions that passes name and argument(s)....

NTDLS commented 3 years ago

@ClutchplateDude its been checked in and samples of usage and the callback are in Entry.Cpp. image

That stretch goal is starting to sound like a whole programming language, which incidentally can be found here: https://github.com/NTDLS/Simple-Scripting-Engine -and here- http://www.networkdls.com/Software/View/Simple_Scripting_Engine/

ClutchplateDude commented 3 years ago

Wow, that was quick, now I do wish I'd followed up 8 months ago :-) Any plans to add functions like sin, cos,. tan, abs, log, etc? I'm using this library in a 3D modelling tool and as you can imagine when dealing with geometry, those trig functions would be useful.

NTDLS commented 3 years ago

Totally can do, but on that note I /think/ those are already built in the the simple scripting engine. It’s just like the math parser. You pass it a sting and it passes back the result. No external dependencies at all. I’ll whip up an example in a few - but I’ll probably add the basic mathematical functions to the math parser too.

NTDLS commented 3 years ago

@ClutchplateDude You should be able to use most of the base mathematical functions available in the C library such as: ABS, ACOS, ASIN, ATAN, ATAN, ATAN2, AVG, CEIL, COS, COSH, EXP, FLOOR, LDEXP, LOG, LOG10, NOT, POW, SIN, SINH, SQRT, SUM, TAN, TANH and I also added ModPOW because its cool. :D

Note that you can nest these function calls and intermix them with variables.

image

ClutchplateDude commented 3 years ago

This is very cool! Thank you. So now that you've come this far..... :-) please add one more callback for non-Native functions, something like: result = fnEvaluate(name, args[])

NTDLS commented 3 years ago

And THAT is the reason I called those “native“ functions! 😆

I’ll add non-native in a bit. 💯

ClutchplateDude commented 3 years ago

Hehe, and here I thought I was being clever in getting those implemented first.... :-) Seriosuly, though, thanks for the work you put into this. BTW, this library is not mentioned on your NTDLS website....

ClutchplateDude commented 3 years ago

BTW, what is modPOW?

NTDLS commented 3 years ago

Basically a way to raise a large number by a large factor but reduce it by a modulus. I use it in diffie-hellman key exchange implementations.

NTDLS commented 3 years ago

Hehe, and here I thought I was being clever in getting those implemented first.... :-) Seriosuly, though, thanks for the work you put into this. BTW, this library is not mentioned on your NTDLS website....

The parser isn’t mentioned on the site, but it is used in the Expression Calculator and the Simple Scripting Engine.

As far as thanks is concerned, I just want to make useful stuff. I hope you can expand on it in a larger project.

NTDLS commented 3 years ago

@ClutchplateDude pushed!

image

ClutchplateDude commented 3 years ago

Awesome!