There are no bugs, at least not on expression evaluation side. 9/2 is indeed 4. It’s integer division. If you want floating point division, at least one of the operands must be of floating point type.
Do a regex replace that finds all numerical literals that don’t have a decimal point and appends it at the end, so 9/2 would become 9./2. and evaluate as you expect it to.
Alternatively, a somewhat simpler regex to construct would the one that adds float(*) around every numerical literal, regardless of it having a decimal point or not.
I don’t really understand your code but one way to transform the 9/2=4.5 expression could be to split the left side at the / character. Lets call the two numbers (9 and 2) a and b.
Now recombine them: var s := "float(%s)/%s" % [a, b]
This turns into float(9)/2, which executed is 4.5 as a float.
Exactly as I described. Are you familiar with regular expressions? If not, you should learn the basics. It’s an immensely powerful tool for any kind of string processing. Every programmer should know how to use them.
var text = "9/2=4.5"
var re: RegEx = RegEx.create_from_string("[\\d\\.]+")
var text_floatized = re.sub(text, "float($0)", true)
print(text, " -> ", text_floatized)
var ex: Expression = Expression.new()
for token in text_floatized.split("="):
ex.parse(token)
print(ex.execute())
I gave you an example to demonstrate the principle. It’s not meant to be directly pasted into your code. You need to understand what has been demonstrated and apply/adapt it to your specific use case.
token is a loop iterator, so of course it’s not defined outside of loop body scope.
func CheckEquationNewPerfect():
ConvertTilesToString()
var text = ValueToCheck
var re: RegEx = RegEx.create_from_string("[\\d\\.]+")
var text_floatized = re.sub(text, "float($0)", true)
var ex: Expression = Expression.new()
var exTwo: Expression = Expression.new()
var resultLeft = null
var resultRight = null
var token = text_floatized.split("=")
ex.parse(token[0])
exTwo.parse(token[1])
resultLeft = ex.execute()
resultRight = exTwo.execute()
if (resultLeft == null or resultRight == null): return(false)
if (is_equal_approx(resultLeft, resultRight) == true):
var numberOfOperators = 0
var scoreAdd = 0
I just want to say that in general it’s a bit impolite to mark your own post as the solution when other people replied to you and helped you get there.
This happens a lot. But I think it’s because it’s not clear what and how mark as solution is used, not that people trying to get credit from other people’s help. Maybe the forum needs to make it more clear.