Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

Negation in Expression

Hi

I just want to ask if it is possible to do negation using an expression. I want to negate a boolean variable by clicking a button. I am using an If expression now, so I am really good without negation, but I just want to know if I am missing something. I should say I would rather not write a script...

I have tried !, ~, Not()...

Also, has anyone seen an option of the button object that makes the button a toggle button?

Thanks!

Simon

1 Solution

Accepted Solutions
johnw
Champion III
Champion III

NOT is negation.  if(not true(),'A','B') would return 'B'.

I'm not sure that's what you're asking for, though.  It sounds like you have a boolean expression somewhere already.  Based on a toggle button, you either want that expression, or the NOT of that expression.

For boolean, false()=0, true()=-1, but any non-zero is also considered true.  One way to negate a boolean, then, is to add 1 and mod 2.  Mod(true()+1,2) = 0 = false.  Mod(false()+1,2) = 1 = true.  We can also do this with our "new" value of true, mod(1+1,2) = false.  So to negate, we want to add 1.  To leave alone, we want to add 0.  If we had a toggle button that toggled between 0 and 1, it would toggle between negating and not.  And we've already seen how to swap between zero and 1, just add 1 and mod 2.  So then our toggle button can be a variable, and every time you press the button, add one and mod 2 to the variable.  Then for your condition, just add the variable.

See attached.  I really don't know if that's what you wanted, though.

View solution in original post

9 Replies
johnw
Champion III
Champion III

NOT is negation.  if(not true(),'A','B') would return 'B'.

I'm not sure that's what you're asking for, though.  It sounds like you have a boolean expression somewhere already.  Based on a toggle button, you either want that expression, or the NOT of that expression.

For boolean, false()=0, true()=-1, but any non-zero is also considered true.  One way to negate a boolean, then, is to add 1 and mod 2.  Mod(true()+1,2) = 0 = false.  Mod(false()+1,2) = 1 = true.  We can also do this with our "new" value of true, mod(1+1,2) = false.  So to negate, we want to add 1.  To leave alone, we want to add 0.  If we had a toggle button that toggled between 0 and 1, it would toggle between negating and not.  And we've already seen how to swap between zero and 1, just add 1 and mod 2.  So then our toggle button can be a variable, and every time you press the button, add one and mod 2 to the variable.  Then for your condition, just add the variable.

See attached.  I really don't know if that's what you wanted, though.

Not applicable
Author

Hi John

That is one thorough solution...

Now, I'm doing this on the button (setting vEnableLineBreak)

=If(vEnableLineBreak=False(), True(), False())

Your Mod solution is pretty close to what I wanted... Although I was hoping for a single function, so I could just write something like

=Not(vEnableLineBreak)

PS: I love how you switch the color. A brilliant solution. hehe

// Simon

johnw
Champion III
Champion III

Well, on the button itself, you can just do this:

=not vEnableLineBreak

It'll then switch between 0 and -1, and have the same effect on the if() expressions.  I don't see how to simplify the if() expressions, though, and was just being consistent between the button and the expressions on my negation approach.  Perhaps simplicity is better than consistency here, and definitely if ALL you wanted was that button, I'd got with the NOT approach.

rwunderlich
Partner Ambassador/MVP
Partner Ambassador/MVP

You can toggle a boolean variable with

=NOT varname

-Rob

johnw
Champion III
Champion III

Thinking further, I think the following approach might be more clear, as it doesn't depend on mathematical games or on the internal storage format of boolean values.

When you click the button, set value to:

=if(Negate='NOT','','NOT')

And in your if() expressions:

if($(Negate) (Your boolean expression here), true result, false result)

Not applicable
Author

Ok. How could I miss that?  Thanks for pointing that out...

I think I was also making another error in the beginning, I was setting the variable, vEnableLineBreak, to True, which was not interpreted as a boolean variable in the expression. Instead, I should have stored the value as an integer...

Thanks to you both...

Not applicable
Author

I just saw you have a toggle button example in your cookbook available on your website: robwunderlich.com/downloads... I think there should be a mention of that in this discussion...

rwunderlich
Partner Ambassador/MVP
Partner Ambassador/MVP

Thanks for pointing out the "User selected on/off true/false switches" button example in the Cookbook. That example uses

=vShow * -1 -1

to do the toggle.

=NOT vShow

is a little simpler, which I why I didn't point out the example. The next version of the cookbook wlll have the simpler form.

-Rob

http://robwunderlich.com

johnw
Champion III
Champion III

Doh!  Much better.  I'd probably write it like this, but same idea.

-(vShow+1)