Hi I was wondering if there is any known way to get rid of unnecessary parentheses in mathematical formula. The reason I am asking this question is that I have to minimize such formula length
if((-if(([V].[6432])=0;0;(([V].[6432])-([V].[6445]))*(((([V].[6443]))/1000*([V].[6448])
+(([V].[6443]))*([V].[6449])+([V].[6450]))*(1-([V].[6446])))))=0;([V].[6428])*
((((([V].[6443]))/1000*([V].[6445])*([V].[6448])+(([V].[6443]))*([V].[6445])*
([V].[6449])+([V].[6445])*([V].[6450])))*(1-([V].[6446])));
it is basically part of sql select statement. It cannot surpass 255 characters and I cannot modify the code that produces this formula (basically a black box ;) ) As you see many parentheses are useless. Not mentioning the fact that:
((a) * (b)) + (c) = a * b + c
So I want to keep the order of operations Parenthesis, Multiply/Divide, Add/Subtract.
Im working in VB, but solution in any language will be fine.
The numbers 6432, 6445 are field names. I've already found a solution to change these numbers to different base (using ASCII characters) so that.
6432 = "37*"
6428 = "37$"
and so on....
It's always one char less ;)
Public Function ToBaseN(n As Variant, base As Integer) As String
Dim numerals As String
numerals = "0123456789abcdefghijklmnopqrstuvwxyz@#$%^&*()"
If n = 0 Then
ToBaseN = "0"
Else
ToBaseN = lstrip(ToBaseN(Int(n / base), base), "0") & Mid(numerals, (n Mod base) + 1, 1)
End If
End Function
Public Function lstrip(s As String, strip As String)
If Left(s, Len(strip)) = strip Then
lstrip = Mid(s, Len(strip) + 1)
Else
lstrip = s
End If
End Function
Edit
I found an opposite problem (add parentheses to a expression) Question.
I really thought that this could be accomplished without heavy parsing. But it seems that some parser that will go through the expression and save it in a expression tree is unevitable.