Arithmetic operators in an expression... Why does 18 * -2 = -2?

fixed
verified

#1

Cost me a little time to figure out what was going on.

18 * -2 = -2

18 * (-2) = -36

18 * 2 = 36

I can work around this obviously, but is that a bug?


#2

What’s really weird…
-2 *18 = -36!!!
That is really odd.


#3

Sounds like a bug. My guess is it’s putting a zero between the * and -, so “18 * 0 - 2 = -2”. But that’s just a guess.


#4

If a variable is used

18 * @negativeVariable

it works fine.


#5

@ipaterson looks like evaluate expression is not handling the special case of operand followed by a negative number and treats the - as an operand instead.


#6

Yikes, I moved this to Bug Reports and tagged for later investigation.


#7

To at least explain the result, this is getting evaluated as

+240ms ║2.0 * 0.0
+247ms ║0.0 - 1.0

I can’t promise anything here since the code that evaluates expressions is very complex but if it turns out to be an obvious fix I’ll give you folks something to test :slight_smile:


#8

The expression 2 * -1 resulted in these three expression components:

+200ms ║║item [t:integer, v:2, d:0, o:*]
+200ms ║║item [t:null, v:0, o:-]
+201ms ║║item [t:integer, v:1, d:0, o:+]

As far as I could tell, this unary negation was always represented by t:null regardless of whether it was followed by a number, a variable, or other. The correct fix seems to be changing line 5384 of the webCoRE Piston Smart App from

if (((item.o) == '!') || ((item.o) == '!!') || ((item.o) == '~')) break;

to

if (((item.o) == '!') || ((item.o) == '!!') || ((item.o) == '~') || (item.t == null && item.o == '-')) break;

I am out of time today, would anyone like to try out this tweak with a variety of expressions?


#9

This should be fixed in today’s v0.3.104.20180323 release which will require updating your smartapps at ide.smartthings.com and possibly hard refreshing the dashboard.