In R, TRUE && factor(FALSE) gives an error but TRUE && factor(FALSE) == FALSE returns TRUE. When TRUE && factor(FALSE) cannot be computed then how does R compares it with FALSE?
Also FALSE && factor(FALSE) returns FALSE but FALSE && factor(FALSE) == FALSE returns FALSE, it should return TRUE because the left hand side expression evaluates to FALSE. I tried FALSE && factor(FALSE) == TRUE but that also returns FALSE. Can someone explain the above results?
Kindly look at the operator precedence. As per the list == as highest precedence then && so the FALSE && factor(FALSE) == FALSEreturns FALSE as it first evaluates == and &&. If you want to execute && first and then == then use the proper bracket:
(FALSE && factor(FALSE)) == FALSE
And it returns TRUE. If you execute:
FALSE & factor(FALSE) == FALSE
Then first it executes factor(FALSE) == FALSE which executes to TRUE and then FALSE && TRUE so finally you will get FALSE.
Related
Title says most of it. Here is a related thread, but I do not have enough reputation to comment.
Here is the prompt:
A number, a, is a power of b if it is divisible by b and a/b is a power of b. Write a function called is_power that takes parameters a and b and returns True if a is a power of b.
In the archived post, the most upvoted answer was to address the trivial base cases of (x,x) and (1,x). Then, determine if (a % b == 0) and then recursively call to find (a/b is a power of b).
Here is the code I've written in Julia:
function ispower(a,b)
if a == 0 && b != 0
return false
elseif a == b
return true
elseif a % b == 0 && ispower(a/b, b)
return true
else
return false
end
end
As I wrote in the comment if you multiply 1 by 1 you can only get 1, so the only number that is a power of 1 is 1 and no other number. Therefore, your function should return false for an input (x,1), whenever x is not 1.
Then you should also consider the case where b==0. Then the only number that is a power of b is 0.
So, I would write you function like this:
function ispower(a::Integer,b::Integer)
if (a==b)
return true
elseif (a==0 || b==0 || b==1)
return false
elseif (a % b) == 0
return ispower(div(a,b), b)
else
return false
end
end
When I run this function it's out put is continuous.
def func(l):
print(f' at begining not isnull{not isnull(l)}')
while not isnull(l):
print(f'in while loop isnull return is {isnull(l)}')
islat(l[1:])
return "At return."
Output that just runs on until I ctl-c.
>>> func(["this"])
At function begining: Not isnull(l) returns True
in while loop: Not isnull(l) returns True
At function begining: Not isnull(l) returns False
in while loop: Not isnull(l) returns True
At function begining: Not isnull(l) returns False
in while loop: Not isnull(l) returns True
At function begining: Not isnull(l) returns False
in while loop: Not isnull(l) returns True
At function begining: Not isnull(l) returns False
I do not understand why the while loop is entered when not isnull is True or False?
while loop will execute while the given expression is True. In your case, the given expression is not isnull . Since isnull False, not isnull is True, hence the loop executes untill isnull becomes True
This might be a dumb question, but I'm interested in why R doesn't allow multiple relational operators in a statement, say,
2 < 5 > 3
R returns
Error: unexpected '>' in "2 < 5 >"
instead of TRUE.
I'm interested in why R doesn't allow multiple relational operators in a statement.
Could you name an example programming / scientific language that allows 0 < 5 > 3?
Suppose this syntax is legitimate, then what is the default rule for it? Which of the following is correct?
(0 < 5) > 3 => TRUE > 3 => 1 > 3 => FALSE
0 < (5 > 3) => 0 < TRUE => 0 < 1 => TRUE
I think you know that the legitimate syntax is (0 < 5) && (5 > 3).
Note that the original question title "multiple logical operators" is imprecise. ">" is a relational operator not a logical one. Using multiple logical operators in a statement is not a problem, say
FALSE && FALSE || TRUE
!FALSE || TRUE
However, be aware that (mixed) logical operations are not associative:
(FALSE && FALSE) || TRUE => FALSE || TRUE => TRUE
FALSE && (FALSE || TRUE) => FALSE && TRUE => FALSE
(!FALSE) || TRUE => TRUE || TRUE => TRUE
!(FALSE || TRUE) ==> !TRUE => FALSE
I have two conditions for if statement. I found that those are different, but I don't know the reason.
1: if ((local != -1) || (fall_back == 1))
2. if ((local != -1) || ((local != -1) && (fall_back == 1)))
Those two are different. But in Math, we have A V (B ^ C) = (A V B) ^ (A V C). If I use this equation and reorganize statement 2, it seems to be the same as 1. Is there anything fundamental I am missing?
How can I simplify statement 2? It doesn't look good.
Short circuit evaluation is done in order to optimize.
So in an expression like (A!=B) || (A!=C), if A!=B turns out to be true the second part A!=C is not evaluated at all, because independent of the second part, the expression will turn out to be true.
Consider
A -> local != -1
B -> fall_back == 1
Your first condition reduces to
A V B
And second statement reduces to
A V (A ^ B) => A ^ (1 V B) => A
So your first statement requires either condition A or B to be true
And second statement tests only if A is true
Consider test case where local = 0 and fall_back = 1
Statement 1 => True as fall_back == 1
Statement 2 => False as local != 0
Hence conditions are not the same
This is Simple, If you Take local != -1 = true and fall_back == 1 = false
Then in Both case result will be true because the value of local != -1 = true
but if take local != -1 = false and fall_back == 1 = true
then in first case result will be true and in second case result will be false.
In Logical OR if the first condition is true means it wont evaluate the second one! But in Logical AND first condition false means it wont evaluate the second one!
if ((local != -1) || (fall_back == 1))
In this if any one of the condition is true, local != -1 or fall_back == 1 it will evaluate the body of the if.
if ((local != -1) || ((local != -1) && (fall_back == 1)))
But here both should be true, Then only it will evaluate the expression. else local != -1 fails means, Both conditions will fails. Because Logical AND(&&) both should be true!
Consider- local != -1 is false and fall_back == 1 is true
if ( false || true ) -> if(0 || 1) -->true
if ( false || (false && true)) -> if(0 || (0 && 1)) -> if(0 || 0) --> false
This question already has answers here:
Logical operators (AND, OR) with NA, TRUE and FALSE
(2 answers)
Closed 9 years ago.
Perhaps I'm missing something obvious.
In R, TRUE && NA evalues to NA. This doesn't make sense to me, because && should evaluate left to right, and stop as soon as one of its conditions is true.
This doesn't make sense to me, because && should evaluate left to
right, and stop as soon as one of its conditions is true.
This is wrong. You are mixing up && with ||:
TRUE && FALSE gives FALSE
&& requires both conditions to be TRUE
&& will short-circuit on FALSE
TRUE || FALSE gives TRUE
|| requires a single condition to be TRUE
|| will short-circuit on TRUE
Also,
TRUE || NA
gives
TRUE