[fpc-pascal] Resolving math expression as input from user
james at productionautomation.net
Tue Apr 23 12:04:57 CEST 2019
I'm trying to figure out if there is some function available that would take a string as input and return a double value that is the complete evaluation of the string?
So when asked for a numeric input, the user could just put in a number like 6.28 but also put in a formula like SQRT(2) or (3*(8+Q))/9.3 or Hypot(I,J).... etc... using any random math operations in combination with math functions and variables, without editing the code to create an expression to evaluate the formula.
I don't know what this method of input is even called, but I have some programs that do it on a small scale, for example I have a CAD program where I can type in 5/32 as in put and it will calculate it to be 0.15625 I can put in anything I want.. like SQRT(2) or Pi or big long formula and it will resolve it for me, so basically all the input fields are all formula calculators. The CAD program does some basic functions, but none of the more complex ones like Hypot and it has no way to use any variables... still it's very useful to just put a formula in the input field.
I have a unit that I found decades ago for turbo pascal that partially does this, but it's outdated, it only works with reals, doesn't work with functions from the math unit like Hypot or atan2 but it does work with things like sqrt, and it doesn't work with values available in the math unit like NAN +inf or -inf, and I would really like to know if the result was NAN as opposed to 0. Anyway I'm wondering if there is already something available that does this in a more modern way, using all the features of the math unit, or am I going to just need to muddle through and re-write the unit I have already.
I tried to do a search on this but don't really know what it's called. My old unit was call infix, but when I search for that I find a lot of methods for converting infix to postfix, which is not what this does at all, the unit I have just evaluates the expression properly and it never converted it to postfix notation to do it. I think it was called infix because it could calculate user input in infix notation and return a result... but that process of accepting a formula as a string, evaluating it as a formula and returning a value.. I do not know what that might be called.. or if it already exists as a function in a unit somewhere. I started to add some missing functions to the unit I have and to convert it to use doubles.. etc, but it's going to be a long tedious process.. and I'll be wasting my time if I could have just used something that is already out there.
More information about the fpc-pascal