evaluating expressions

Posted by Tsunami on Thu 28 Oct 2004 05:22 PM — 7 posts, 28,920 views.

USA #0
I have bee trying to get the following expression to evaluate properly, but it seems not to like me.

if (h <= m*f) then
World.note "It worked!"
end if

while testing I made sure h was indeed less than m*f, yet it never evaluated to true. Finally I found that when true, the expression evaluated to 0, and when false, -1.
Therefore I had to change the expression to:

if ((h <= m*f) + 1) then
World.note "It worked!"
end if

It works now, but I was wondering what was going on with that?
USA #1
True is -1
False is 0
Thats how VBScript is set up.
So, you must have had something wrong initially, since when true it should have evaluated to -1, and false to 0.
Try adding parenthesis around m*f, or make sure theyre all numbers (Cint, or whatever), since sometimes the variants cause problems.

Although it will evaluate to true if its any non zero. But True has a value of -1, and False 0 (as is VbTrue and VbFalse).
Amended on Thu 28 Oct 2004 06:19 PM by Flannel
USA #2
I didn't know that true was -1, but the expression should have worked in any case. I tried it with parens around m*f and without; the result was the same. *shrug*
USA #3
Did you cast them to numeric values? It could be trying to do a text comparison.

Since obviously the expression is wrong in one way or another.
USA #4
Post the entire script and we can probly be of more help on this, just seeing the calculation doesn't really give us much to work with.
USA #5
This is the corrected version, the uncorrected version simply didn't have the + 1 inside the ifs.

'h = 100
'm = 100
'mH = World.GetVariable("mH") (= 2900)
'mM = World.getVariable("mM") (= 1900)

sub DoPrompt (name, output, wildcs)
h = wildcs(1)
m = wildcs(2)

if elixirBalance then
if ((h < mH*hF) + 1) then
healElixir "health"
elseif (m <= m*mF) then
healElixir "mana"
end if
end if
if mossBalance then
if ((h <= mH*mF) + 1) then
healMoss
end if
end if
end sub
Australia Forum Administrator #6
Quote:

h = wildcs(1)
m = wildcs(2)


All this sort of stuff is treating the numbers as strings. Thus the comparison will probably not work.

You have also commented out some of the other assignments:

Quote:

'mH = World.GetVariable("mH") (= 2900)
'mM = World.getVariable("mM") (= 1900)


I would convert them to integers, eg.


mH = CInt (World.GetVariable("mH")) 
mM = CInt (World.getVariable("mM"))

...

h = CInt (wildcs(1))
m = CInt (wildcs(2))