Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Algorithms
  4. how to convert a string to a mathematical function

how to convert a string to a mathematical function

Scheduled Pinned Locked Moved Algorithms
tutorial
8 Posts 8 Posters 43 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nta_3886
    wrote on last edited by
    #1

    i have a mathematical function,example x^2+sin(x),but it is String.i want it is mathematical function,it can return value float

    leo

    D D R U L 6 Replies Last reply
    0
    • N nta_3886

      i have a mathematical function,example x^2+sin(x),but it is String.i want it is mathematical function,it can return value float

      leo

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      nta_3886 wrote:

      i want it is mathematical function,it can return value float

      Ok.


      "A good athlete is the result of a good and worthy opponent." - David Crow

      "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

      1 Reply Last reply
      0
      • N nta_3886

        i have a mathematical function,example x^2+sin(x),but it is String.i want it is mathematical function,it can return value float

        leo

        D Offline
        D Offline
        Dan Neely
        wrote on last edited by
        #3

        use a stack to convert the formula into Reverse Polish Notation (RPN). Then evaluate the RPN version. You can find many examples of how to do both steps of this via google.

        -- If you view money as inherently evil, I view it as my duty to assist in making you more virtuous.

        E 1 Reply Last reply
        0
        • D Dan Neely

          use a stack to convert the formula into Reverse Polish Notation (RPN). Then evaluate the RPN version. You can find many examples of how to do both steps of this via google.

          -- If you view money as inherently evil, I view it as my duty to assist in making you more virtuous.

          E Offline
          E Offline
          El Corazon
          wrote on last edited by
          #4

          dan neely wrote:

          use a stack to convert the formula into Reverse Polish Notation (RPN). Then evaluate the RPN version. You can find many examples of how to do both steps of this via google.

          Semi-on-topic... I just found my code for this from Pascal class back in the 80's. I used it on my first job to build an RPN based spreadsheet program in RPG II. :)

          _________________________ Asu no koto o ieba, tenjo de nezumi ga warau. Talk about things of tomorrow and the mice in the ceiling laugh. (Japanese Proverb)

          1 Reply Last reply
          0
          • N nta_3886

            i have a mathematical function,example x^2+sin(x),but it is String.i want it is mathematical function,it can return value float

            leo

            R Offline
            R Offline
            Russell
            wrote on last edited by
            #5

            a lot of work:doh:


            Russell

            1 Reply Last reply
            0
            • N nta_3886

              i have a mathematical function,example x^2+sin(x),but it is String.i want it is mathematical function,it can return value float

              leo

              U Offline
              U Offline
              Urs Enzler
              wrote on last edited by
              #6

              If you are using the .net Framework then you can compile that into a method in a temporary assembly and execute it as normal code. I'm sure you can find a sample about that in the WWW.

              -^-^-^-^-^- no risk no funk ................... please vote ------>

              1 Reply Last reply
              0
              • N nta_3886

                i have a mathematical function,example x^2+sin(x),but it is String.i want it is mathematical function,it can return value float

                leo

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #7

                Hi, if your expression uses a .NET language such as C# you can have .NET compile and run it for you, as in http://www.codeproject.com/csharp/evalcscode.asp[^] if you want to define your own langauge you will need to parse and evaluate everything yourself, as in http://www.codeproject.com/csharp/sota_expression_evaluator.asp[^] or http://www.codeproject.com/cpp/MathieuMathParser.asp[^] Did you notice all links are to existing CodeProject articles, which you can search using the Search box available on nearly every CP page? :)

                Luc Pattyn [Forum Guidelines] [My Articles]


                this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/... - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google


                1 Reply Last reply
                0
                • N nta_3886

                  i have a mathematical function,example x^2+sin(x),but it is String.i want it is mathematical function,it can return value float

                  leo

                  P Offline
                  P Offline
                  PICguy
                  wrote on last edited by
                  #8

                  If you are using Rexx then interpret say "string" Except you first need to write sin(x) and other functions you use in Rexx. You also need to use Rexx rules for syntax. Rexx can compile and execute on the fly. With just a little more detail in dos one might type rexx math 1.2*4.7 5.64 prints The file math.cmd contains /* execute incomming statement */ numeric digits 40 arg cmd interpret say cmd It's been a while but saving x to a environment variable or some such is not that hard. My 40 digit ln() in ln.cmd is /* ln(x) */ numeric digits 40 arg x /* prescale to put x into reasonable range */ xp = 0 if x > 2 then do until x < 2 xp = xp + 1 x = x / 2 end /* now that x <=2 compute ln(x) method from Abramowitz etal section 4.1.39 ln(2) to 40 places takes 26 loops */ z = x-1 val = 0 do t = 26 to 1 by -1 t2z = t*t*z val = t2z / (t+t+val+1) val = t2z / (t+t+val) end val = z / (val+1) /* account for prescale */ if xp = 0 then return val return (const(ln2)*xp) + val

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups