Results 1 to 14 of 14

Thread: Coding math with VB6

  1. #1

    Thread Starter
    Lively Member oswaler's Avatar
    Join Date
    Jul 2006
    Location
    U.S. of A.
    Posts
    87

    Coding math with VB6

    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.

    Thanks for your help - Eric

  2. #2
    Addicted Member
    Join Date
    Aug 2005
    Location
    York
    Posts
    197

    Re: Coding math with VB6

    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.

    Cheers.

  3. #3

    Thread Starter
    Lively Member oswaler's Avatar
    Join Date
    Jul 2006
    Location
    U.S. of A.
    Posts
    87

    Re: Coding math with VB6

    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?

    Thanks - Eric

  4. #4
    Addicted Member
    Join Date
    Aug 2005
    Location
    York
    Posts
    197

    Re: Coding math with VB6

    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

    sInput = "2 + 2"

    lCount = ConvertToPostfixRPN(sInput, tRPN)
    rAnswer = EvaluateRPNExpressionDouble(tRPN, lCount)

    txtAnswer.Text = rAnswer

    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.

  5. #5

    Thread Starter
    Lively Member oswaler's Avatar
    Join Date
    Jul 2006
    Location
    U.S. of A.
    Posts
    87

    Re: Coding math with VB6

    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.

  6. #6
    Addicted Member
    Join Date
    Aug 2005
    Location
    York
    Posts
    197

    Re: Coding math with VB6

    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.

  7. #7
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Coding math with VB6

    Don't know how much use it would be to you, but have a look at the Microsoft Script Control.

    Add one to your form and it can evaluate string equations, something like:
    VB Code:
    1. Private Sub Command1_Click()
    2.     MsgBox ScriptControl1.Eval("5 + 10")
    3. End Sub
    I've choosen a basic example, but i believe it can do very complex things + it saves you having to write your own parser.

  8. #8
    Addicted Member
    Join Date
    Aug 2005
    Location
    York
    Posts
    197

    Re: Coding math with VB6

    I did mention that - but its tonnes slower than my parser - that he's using now.

  9. #9

    Thread Starter
    Lively Member oswaler's Avatar
    Join Date
    Jul 2006
    Location
    U.S. of A.
    Posts
    87

    Re: Coding math with VB6

    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!

    Thanks again - Eric

  10. #10
    Fanatic Member VBAhack's Avatar
    Join Date
    Dec 2004
    Location
    Sector 000
    Posts
    617

    Re: Coding math with VB6

    You might want to check this out:

    http://digilander.libero.it/foxes/SoftwareDownload.htm

    Class Math Expression Parser

    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.

  11. #11
    Addicted Member
    Join Date
    Aug 2005
    Location
    York
    Posts
    197

    Re: Coding math with VB6

    Oooops....posted the wrong version of the expression parser earlier :|
    I'll send my newer version later today :P

    The one I posted is an old version which is more confusing and slower to use.
    Probably when I post later I'll try and add support for variables

    Cheers.

  12. #12
    Addicted Member
    Join Date
    Aug 2005
    Location
    York
    Posts
    197

    Re: Coding math with VB6

    Ok....didn't have time to do the symbols....but here's the newer one anyway.
    Attached Files Attached Files

  13. #13
    Addicted Member
    Join Date
    Aug 2005
    Location
    York
    Posts
    197

    Re: Coding math with VB6

    Ok I've done the symbols

    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.

    Cheers
    Attached Files Attached Files

  14. #14

    Thread Starter
    Lively Member oswaler's Avatar
    Join Date
    Jul 2006
    Location
    U.S. of A.
    Posts
    87

    Re: Coding math with VB6

    Thanks so much again for your help!

    -Eric

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width