Download

"string to math expression"

Hi!

Is there any way to turn strings into math expressions in blueprint?

For example I have as string: “5*7+1-99/3” and would like to let it calculate the result.

Thought about sth like a math expression node with a string input on the name.

regards

**Edit: I get that I have to convert this: 57+1-99/3 into this: (57)+1-(99/3), that is not the problem.

The problem is that I can not find anything in blueprints that takes this at runtime. Not even with correct brackets.**

Have you tried the “String to int” node? Not sure if that is what you need.

t0b4cc0, why do you have a string containing math equations, perhaps there is another way to accomplish what you are trying to do.

indeed that is a good question, and usually you dont come around math equations in strings. I am working on a genetic algorithm. In short, i have a bitcode. like 00010101011010101, and the first 4, 0001 mean 1, the next few (0101) could mean + for example. the bytecode is stored in strings (I seperate them per comma like “0001,1010,…” i could not find a way to use custom structs in bp)
It was just to easy for me to translate the bytecode to strings of math operators and numbers.

Anyways, I probably should figure out how to make a decent calculator. (only needs to be for basics, + - * /)

For a basic calculator you just need a few booleans, two variables to hold the previous number and the number you are adding/subtracting/etc… and a bunch of branches, if IsAdding is true, then add #1 + #2 (set #1 to the result of the math), if IsMultiplying is true, then multiply them, and so on…

how would you go about following the rules of math with sth like this:

5*7-3/8+2

(so that it correctly calculates 3/8 before 8+2)

Separate it into sections and use BP nodes like this:

create a multiply node (float * float) and enter (5 * 7)
create a division node (float / float) and enter (3 / 8)
create a minus node (float - float) and plug the output from the multiply in the top pin, division output into the second pin
create a plus node (float + float) and plug the result from the minus node in the top pin, and enter 2 for the second pin’s value

-or-

You can use a custom Math node to do this if you’d like, in that case you need to follow the following convention and enter this as the text:

(5 * 7) - (3 / 8) + 2

That should work properly. You always need to evaluate any multiplication or division separately from adding/subtracting, adding in the brackets forces the compiler to do that equation first.

I totally get that, but I do not know how to make a BP solve it after adding brackets.
In code I could write that out and let it calculate. (with a library) **I can not find anything in blueprints that takes this at runtime. Not even with correct brackets. ** thats why I asked in my initial post if there is something like the math expression node that can be fed information at runtime.

What you would need is a parser that evaluates your string expressions.
While Im quite good at writing all types of parsers (kind of hobby), I dont think its feasible in BP.

The closest thing that would be doable in Bp with realative ease would be a stack based parser and using reverse polish notation in the expression.
Then it is just a question of how to tokenize your string.
The idea is that you end up with an array that contains your sequence of operators and operands, like:
“3”
“4”
“+”
… which represents “4+3” in inline notation.
The reverse polish notation completely avoids the problems of resolving (nested) brackets…

Dang, now I wanna make something like this just for fun :slight_smile:

I have made a Blueprint parser that can do this (it’s part of the dialogue system in my signature). I had started a thread about putting it up on the marketplace as a standalone thing here, but it didn’t receive much attention.

At any rate, making a parser to convert a string to a mathematical expression is what you’ll need to do I’m afraid. I used the Shunting Yard algorithm for this (it’s rather simple if you don’t aim to have short-circuiting operators like and/or/?: as I did in my interpreter).

Let me know if you go the route of making a parser and need a hand with it :slight_smile:

when i posted this i started to make a parser already.

i just thought while doing it, that the math expression node allowing a string input as expression during runtime would be nice to have

wow since you seem to be a fan, ill upload mine to dropbox, have a look at it, i dont think its efficient but it does the job. would be nice to hear back from you. (for my genetic algorithm i only have single digit numbers and the only operators are ±*/ so it was quite easy, i had more problems doing the math since it took me a while to realize that find item * in string array does what i expect from a windows search instead of finding the multiply operator index)

only allows single digit numbers, the 4 basic math operators (+,-,/,x), the variable “rechnung” holds the calculation to test.

Ah ok, it certainly would have saved me a bunch of time as well :slight_smile: