Calc problem... already :-(

RandomCoder

Well-Known Member
Licensed User
Longtime User
I'm attempting a go at the calculator competition (see this thread http://www.b4x.com/forum/showthread.php?t=914).

In doing so I would like to take what I believe is a rather unique approach, however I am already experiencing problems and would like your help.

The problem is this....

B4X:
x = 2 + 3
MsgBox(x)
Works like a charm, giving 5 as the result (can't really make it more simple than that)

But now when I do this instead...
B4X:
x = 2 & chr(43) & 3
MsgBox(x)
I get 2+3 instead of the desired mathematical result.

So is there a way of forcing the compiler to treat the expression as an equation rather than a string?

Regards,
RandomCoder
 

agraham

Expert
Licensed User
Longtime User
So is there a way of forcing the compiler to treat the expression as an equation rather than a string?
I personally don't like the weak typing of B4PPC but you can get it to work for you.

B4X:
x = 2 & Chr(43) & 3 & "*" & 4 ' build a valid expression 
y = 0 + x ' coercion forces evaluation of the string
Msgbox(y) ' success
If you use this then you will need an error handler unless you can parse the string for mathematical correctness first.

EDIT: Or use Isnumber() on the string and not try the coercion if it returns false. This approach supports */+- and ^ but not functions such as abs() etc.

EDIT EDIT: Actually Isnumber() isn't reliable in this instance. I don't know why I thought it would be. Stick to the error handler approach!
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
This trick is dangerous.
I recommend you to use the code sample in the link I've posted instead.
It will only work if evaluated once.
This code for example will fail on the second time:
B4X:
Sub App_Start
    For i = 1 To 2
        x = "2*5+4" 
        y = 0 + x 
        Msgbox(y)
    Next
End Sub
The reason it fails is that there is an optimization process that optimizes calculations after the first calculation.
 

agraham

Expert
Licensed User
Longtime User
The reason it fails is that there is an optimization process that optimizes calculations after the first calculation.
Pity - I thought that it might be quite useful:sign0148:


EDIT: Actually this probably explains some inconsistent results and inexplicable errors I have encountered trying some fairly complicated string maniipulation where I was (rather less blatently than above) using B4PPCs apparent use of weak typing to reduce the amount of code.
 
Last edited:

RandomCoder

Well-Known Member
Licensed User
Longtime User
Thanks for the input guys, I had already tried using a variety of amplisand, brackets and single quotes but all to no avail.

With some help from a friend of mine he has got it working using the following method...

B4X:
x=(2 Chr(43) 3)
MsgBox(x)

I must admit that I had already tried this without the brackets and all I got was a value of 2, but with the brackets it's evaluated correctly and produces a value of 5.

Thanks for the help.

Regards,
RandomCoder
 
Top