logical opertation !A && !B vs !(A||B) - logical-operators

I often see the following statement in embedded software
!A && !B
rather than
! (A || B)
Although both give the same truth table value.
I often use the http://web.stanford.edu/class/cs103/tools/truth-table-tool/ to
check complex logical operations.
Is there logical reason to it? Is there any general rule/best practice/conventions in using logical statements in regards to time critical applications?

Related

In Julia when to use `!` (boolean not) vs `~` (bitwise not)?

The docs say that ! is "Boolean not. Implements three-valued logic" and ~ is "Bitwise not."
When should one be used versus the other?
There is a similar question for Python about comparison operators, but I am not sure where the languages may differ.
As you say, ! is boolean not and ~ is bitwise not. This ends up being a bit redundant since bitwise not for booleans is just boolean not so anywhere you can use ! you should also be able to use ~. (This is always true with standard Julia should remain true as long as package don't addg methods that don't conform to the intended meaning of these operators.) So if you're a Matlab programmer, for example, and you're used to using ~ for boolean not, then you can keep doing that. My suggestion, however, would be to be as precise in your meaning as you can be: if you expect the argument to be boolean, use ! so that you get an error if it isn't. Use ~ if you expect the argument to be an integer and want to flip its bits. But frankly it's no big deal to use ~ for booleans. If it really matters that the argument is boolean, like if it's being used in a conditional, then the error will show up soon anyway.
There was a discussion in the lead up to the 1.0 release about removing ! from the language and using ~ for boolean negation instead, but we ended up keeping ! because it's familiar from languages like C and C++ and giving it any other meaning would be actively confusing. But it's telling that removing it was an option in the first place.

When should i use `==` vs `===` vs `isequal`

I see that julia has 3 different ways
to do equality.
==, ===, and isequal
Which should I use, and when?
=== is the built-in equality.
On primitives, it is value equality: if they have the same bit level representation then they are equal.
On mutable structs, it is referential equality: things are equal if they are the same memory locations.
On immutable structs, it is structural equality: two structures are equal if they are of the same type and all their fields are equal.
In all 3 cases this is all more or less just bit-level equality, references to memory are pointers.
(But of fancyness to make the immutable struct version recursive)
Ideally, one wouldn't use this much, because it is not customizable.
Sometimes though it is good to use because the optimizer can reason about it really well, so it can lead to better performance for hot loops.
== is general purpose equality
It is overloadable.
For Floats it follows the IEEE rules, i.e. -0.0 == 0.0, NaN != NaN.
And it follows 3 values logic rules for missing == missing resulting in missing.
if == is not defined then it falls back to ===
If you have defined ==, you need to define hash as isequal falls back to using ==, see below.
isequal is equality for purpose of dictionaries.
I don't know a much better way to put it.
Things that are isequal are considered the same for purposes of Dict and Set.
So you can't have two items that are isequal as distinct keys in a Dict.
Use this if you want to be sure that NaNs are equal to each other,
and similarly that missings are equal to each other.
When defining isequal you also must define hash.
isequal(a, b) implies hash(a) == hash(b)
If isequal is not defined, then it falls back to ==
Basically:
== when you are interested in the values of two objects: 1 == 1->true and 1 == 1.0->true
=== when you want to make sure that two objects cannot be distinguished (including arrays pointing to different memory): 1 === 1->true but 1 === 1.0->false and A = [1, 2, 3]; B = [1, 2, 3] leads to A == B->true but A === B->false (A === A->true).
isequal() same as == but handles floating point numbers differently: NaN == NaN->false but isequal(NaN, NaN)->true.
A deeper discussion here.

Dafny verifies insertion sort using swap

I'm working on how to use dafny to verify an insertion sort using "swap" adjacent elements but I can't find a reasonable invariant for the while loop, can anyone help me fix it?
Here is the link: http://rise4fun.com/Dafny/wmYME
There are a few problems here.
First, your inner loop is not correct, because the temp variable is never updated. I recommend removing temp and using the loop condition down >= 0 && a[down+1] < a[down] instead.
Second, you have several issues with the inner loop invariant being ill formed (index out of range, violating precondition of sorted). However, instead of fixing these, I recommend throwing out both inner loop invariants and trying again.
My preferred invariant for the inner loop of insertion sort is "a[0..up+1] is sorted except possibly at down + 1". You can state this as
invariant forall j,k | 0 <= j < k < up+1 && k != down+1 :: a[j]<=a[k]
The resulting file verifies.

Why can't I use || and && operators in data.table row selection? [duplicate]

This question already has answers here:
Boolean operators && and ||
(4 answers)
Closed 9 years ago.
Why there are four logical operators:
&, &&
|, ||
What's the differences in usage?
Yes, I've checked the docs, yet I'm a little bit confused. The docs says:
‘&’ and ‘&&’ indicate logical AND and ‘|’ and ‘||’ indicate
logical OR. The shorter form performs elementwise comparisons in
much the same way as arithmetic operators. The longer form
evaluates left to right examining only the first element of each
vector. Evaluation proceeds only until the result is determined.
The longer form is appropriate for programming control-flow and
typically preferred in ‘if’ clauses.
I think a piece of example will clearly demonstrate them. Thanks.
Key differences are as below...
Long form(&& or ||) short circuits, which means if it can identify the result by just validating just the first element. While doing &&, if the comparision of first two elements resulted in false, comparing next set of elements will also result in False. So, it returns false. While doing || if comparision resulted in true in first few elements, we can confidently say that any further validations will not change the result so it returns True.
Short forms continues to do for the entire vectors and creates a vector of results and returns it.
Hope this helps.
& and && indicate logical AND and | and || indicate logical OR. The
shorter form performs elementwise comparisons in much the same way as
arithmetic operators. The longer form evaluates left to right
examining only the first element of each vector. Evaluation proceeds
only until the result is determined. The longer form is appropriate
for programming control-flow and typically preferred in if clauses.
Source: http://stat.ethz.ch/R-manual/R-patched/library/base/html/Logic.html

What's the differences between & and &&, | and || in R? [duplicate]

This question already has answers here:
Boolean operators && and ||
(4 answers)
Closed 9 years ago.
Why there are four logical operators:
&, &&
|, ||
What's the differences in usage?
Yes, I've checked the docs, yet I'm a little bit confused. The docs says:
‘&’ and ‘&&’ indicate logical AND and ‘|’ and ‘||’ indicate
logical OR. The shorter form performs elementwise comparisons in
much the same way as arithmetic operators. The longer form
evaluates left to right examining only the first element of each
vector. Evaluation proceeds only until the result is determined.
The longer form is appropriate for programming control-flow and
typically preferred in ‘if’ clauses.
I think a piece of example will clearly demonstrate them. Thanks.
Key differences are as below...
Long form(&& or ||) short circuits, which means if it can identify the result by just validating just the first element. While doing &&, if the comparision of first two elements resulted in false, comparing next set of elements will also result in False. So, it returns false. While doing || if comparision resulted in true in first few elements, we can confidently say that any further validations will not change the result so it returns True.
Short forms continues to do for the entire vectors and creates a vector of results and returns it.
Hope this helps.
& and && indicate logical AND and | and || indicate logical OR. The
shorter form performs elementwise comparisons in much the same way as
arithmetic operators. The longer form evaluates left to right
examining only the first element of each vector. Evaluation proceeds
only until the result is determined. The longer form is appropriate
for programming control-flow and typically preferred in if clauses.
Source: http://stat.ethz.ch/R-manual/R-patched/library/base/html/Logic.html

Resources