This should be simple but I can't figure out how to do it. I have a form with a text box in which the user will type a function like x^2*cos(x). I want to be able to take the value the user typed into the box and have vb understand it as a mathematical expression rather than as a string. I have all the code I need working but right now I have to edit the code every time I want to change the function.
It would appear that it is simple but it isn't.
You have to write the code to parse the expression to make it calculate the expression. Check for postfix evaluators and convertors. I've written on, but the code is quite complex. Just post me a message, if you want to have a look at it.
There may be a slower but easier way with the microsoft scripting control...but im not sure.
Thank you, I would like to have a look at the code if you don't mind.
A follow up: In the program I'm writing I'm having the computer do a numerical integration of the type of function listed above. My method right now is to do a simple Riemann sum using a For Next loop. I find I can get better accuracy by using more segments so that for instance I could have the loop run 2.1 billion times and sum up the tiny areas. This takes 33 minutes to do an integration that I can do in my head in 30 sec. Is there a faster mechanism for running the same kind of Riemann sum the same number of times but in much less time?
Ahh i see what you are doing, I can't guarantee it will be fast, because parsing a expression real time is slow. Usually you would need an engine to convert the function into machine code (i.e. compile real time once) and then just use the function over and over again.
I'll post the evaluator, but it may be above your level of coding to understand:
I'll just outline what you need to do:
You need the following modules added to your project:
MPostfixEvaluator.bas
MAssemblyThunks.bas
When you want to Calculate Something:
Dim tRPN(0 To 49) As RPN_EXPRESSION_ITEM
Dim sInput As String
Dim lCount as Long
Dim rAnswer as Double
Before you get carried away, you can't use sInput = "x + 2", because I haven't programmed in support for variables.
Note: This will certainly be slower than your current method, and I believe, Reimann's numerical integration is slow anyway .....so maybe look at the Simpon's Rule
Cheers.
Last edited by Raedwulf; Jul 4th, 2006 at 02:40 AM.
Thanks so much, your code was very helpful. Wow! I can't believe how complex it is just to convert a string to a mathematical expression. Somebody needs to suggest a VB function for converting string to expression to Uncle Bill before he steps down.
My Code was more complex because I used some assembly code & some VB Speed Hacks to make it much faster. If I did it in Pure VB....it would be much slower at least 3-4x slower.
Thanks so much, the scipt control did exactly what I needed, once I played with it to get it to understand variables in the equation. I can see where it is much slower than Raedwulf's parser which I will eventually incorporate, but for right now the script control did the trick beautifully!
v. 4.1 by Leonardo Volpi, Michael Ruder, Thomas Zeutschler, Lieven Dossche.
A nice class for parsing and evaluating math expressions in Visual Basic and VBA. This version accepts also exponential (1.23E-3) and physical numbers with unit of measure like m, MHz, ms, kg, etc and mathematical and physical constants. Now with more than 70 functions and operators recognized. Open Source. Freeware. Documentation in PDF enclosed.
Here's the instructions to test:
-----
x=5+5
Answer=10
------
x+5
Answer=15
Then you can reset the variables i.e. delete them all
If the variables are not found then they default to value 0 - there's no erroring
I will add support for constants and Binary search tree(to speed it up more) a bit later. If I feel particularly enthusiastic i might even write a compiler . But Probably i don't have time atm.