Passing negatives of variables to a widget

I’m having an issue where passing a negative of a variable to a widget seems to get it returned as a string.

a widget:

<<widget “add”>>

<<= $args[0] + $args[1]>>




instead of 0 when the following is quoted:

<<set _i = 1, _j = 1>>
<<add _i -_j>>

Is there any way of resolving this without assigning a temporary variable to the negative of the desired variable? E.g

<<set _i = 1, _j = 1>>
<<set _k = -j>>
<<add _i _k>>

You may use a backquote expression—see Macro Arguments—to force the unary negation/minus operator. E.g.,

<<add _i `-_j`>>

That said, and assuming this isn’t a toy scenario, if you know the status of the second operand to know that you need to use the unary negation/minus operator in the first place, it seems like it would make more sense to have a <<sub>> widget—e.g., <<sub _i _j>>.

1 Like

If you don’t want to use backquotes, then there’s this method:

<<widget "add">>
	<<run $.wiki("<<set _arg0 = " + $args[0] + ">>")>>
	<<run $.wiki("<<set _arg1 = " + $args[1] + ">>")>>
	<<= _arg0 + _arg1>>

That basically stores the parsed value of each argument in a temporary variable, and then adds those temporary variables together.

Hope that helps! :slight_smile:

1 Like

That’s perfect thank you, and yeah it was just a toy scenario, the add widget was just an example :).