[SOLVED] How do I replace x^y with Math.pow(x,y)?

0 favourites
  • 8 posts
  • Hello,

    I have an example math string like this:

    4^5 + 3^2 + 6 - 2^3

    Then I would like to convert it to:

    Math.pow(4,5) + Math.pow(3,2) + 6 - Math.pow(2,3)

    I tried to break them with "^" and put in array then combine head and tail for each power but everything leads to dead end.

    Appreciate with any idea ^_^

    Tagged:

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • The only way I would know to do what your asking is by using regular expressions.

    regexr.com

    That site allows you to see how the expressions work in realtime, which is nice for newbs like myself.

    Then

    for loop,

    action, RegexMatchAt(TextInput.Text,"\S+","g",loopindex)

    that would fill your array if there are space (you may want that differently, \S+ is just a not space parse.),

    then you could do a regexreplace for getting it into the math.pow format.

    I was curious why the .pow format? Set text to str((2^2)+(2^2)) displays 8.

    construct.net/en/tutorials/exploring-regex-construct-860/regexmatchat-3

  • You can make a parser to do it. There are many ways to do that depending on what you want to do.

    Fist step is to get a list of tokens (numbers and +- or ^)

    A simple way to do that is with a series of replaces so what you end up with is a list of tokens divided by spaces.

    Then you can loop over them and make that substitution with something like this:

    var text = "2^3+4^6
    
    Set text to replace(text, “ “, “”)
    Set text to replace(text, “+”, “ + “)
    Set text to replace(text, “-“, “ - “)
    Set text to replace(text, “^”, “ ^ “)
    
    var output= ""
    var i=0
    var n=tokencount(text, " ")
    
    while i<n
    -- add 1 to i
    -- if tokenat(text,i," ")="^"
    -- -- add "pow("&tokenat(text,i-1," ") &","&tokenat(text,i+1," ")&")" to output
    -- -- add 2 to i
    -- else
    -- -- add tokenat(text,i-1," ") to output
  • I was curious why the .pow format? Set text to str((2^2)+(2^2)) displays 8.

    Thanks for your answer!

    I have an input for users can input the numbers and operators to create a math string as a calculator.

    Then users press "=" to show the result.

    I use Math.pow(x,y) because the eval(string) in JS script does not give the correct result if there is a "^".

    But I would try your way to convert the math string and put in the str().

  • You can make a parser to do it. There are many ways to do that depending on what you want to do.

    Fist step is to get a list of tokens (numbers and +- or ^)

    A simple way to do that is with a series of replaces so what you end up with is a list of tokens divided by spaces.

    Then you can loop over them and make that substitution with something like this:

    var text = "2^3+4^6
    
    Set text to replace(text, “ “, “”)
    Set text to replace(text, “+”, “ + “)
    Set text to replace(text, “-“, “ - “)
    Set text to replace(text, “^”, “ ^ “)
    
    var output= ""
    var i=0
    var n=tokencount(text, " ")
    
    while i<n
    -- add 1 to i
    -- if tokenat(text,i," ")="^"
    -- -- add "pow("&tokenat(text,i-1," ") &","&tokenat(text,i+1," ")&")" to output
    -- -- add 2 to i
    -- else
    -- -- add tokenat(text,i-1," ") to output

    Thanks R0J0hound!

    As I understand that with your script it would work with numbers have 1 character.

    But it is a good start for me. :)

  • Hello jsutton and R0J0hound

    It's me to bother you guys again.

    I have time to come back this.

    I have a string "4^5+3^2 + 6 - 2^3"

    Do you know how to remove the ""? To turn it to:

    str(4^5+3^2 + 6 - 2^3)

    Thanks again for your help!

  • You mean just get rid of the quotes?

    Set text to replace(text, """", "")

    Set text to "str("&text&")"

    Or do you mean just evaluating the formula in a string?

    Sounds like you already are utilizing execjs() to run it like a JavaScript expression, but want different syntax with pow and such.

    In which case I want to revise my previous reply. I’d recommend writing a parser. Or don’t. Just search for the newest posts by me where I mention parser and use one of those capx.

  • Thank you R0J0hound

    I found that the "^" in JS will be "**".

    Because the eval(x^y) is not correct but I replace "^" with "**", it will be correct.

    eval("2^3") = 1 (incorrect)

    eval("2**3") = 8 (correct)

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)