Complicated IF statement returning false - if-statement

I am using this long IF statement to calculate the commission rate for sales people. The value if true portion works but the value if false returns FALSE in the cell instead of executing the false instruction which in this case is (IF(R11<1,(H11*($C$7/10000)),H11*R11). Maybe I cannot have another IF in the False section of the formula?
If the main IF statement is FALSE (meaning Q11 is blank) I want the false portion of the formula to then
test if R11 is blank and if it is blank then (H11*($C$7/10000))
if R11 is not blank then H11*R11
Can someone help me get this right?
=IF(Q11>1,(IF(ISNUMBER(SEARCH("COOP",Q11)),(H11*1%),IF(ISNUMBER(SEARCH("CERBASI",Q11)),(H11*0.5%),IF(ISNUMBER(SEARCH("Direct",Q11)),(H11*0.3%),IF(ISNUMBER(SEARCH("Specialist",Q11)),(H11*0.5%),IF(ISNUMBER(SEARCH("UNITED",Q11)),(H11*1%),IF(ISNUMBER(SEARCH("Subordinate",F11)),(H11*0%),(IF(R11<1,(H11*($C$7/10000)),H11*R11))))))))))

For any other people who visit this page as well. Here is an unwrapped version of the authors if statement.
=IF(
Q11>1,
(IF(
ISNUMBER(SEARCH("COOP",Q11)),
(H11*1%),
IF(ISNUMBER(SEARCH("CERBASI",Q11)),
(H11*0.5%),
IF(
ISNUMBER(SEARCH("Direct",Q11)),
(H11*0.3%),
IF(
ISNUMBER(SEARCH("Specialist",Q11)),
(H11*0.5%),
IF(
ISNUMBER(SEARCH("UNITED",Q11)),
(H11*1%),
IF(
ISNUMBER(SEARCH("Subordinate",F11)),
(H11*0%),
(IF(
R11<1,
(H11*($C$7/10000)),
H11*R11
))
)
)
)
)
)
))
)
Sorry I know you never asked to recreate it, but it was really hard to read, maintain and change. What about this?
=IFS(OR(ISNUMBER(SEARCH("COOP",Q11)), ISNUMBER(SEARCH("UNITED",Q11))), (H11*1%), OR(ISNUMBER(SEARCH("CERBASI",Q11)), ISNUMBER(SEARCH("Specialist",Q11))), (H11*0.5%), ISNUMBER(SEARCH("Direct",Q11)), (H11*0.3%), ISNUMBER(SEARCH("Subordinate",F11)), (H11*0%)), R11<1,(H11*($C$7/10000)), H11*R11, TRUE, $C$1)
Easier to read:
=IFS(
OR(ISNUMBER(SEARCH("COOP",Q11)), ISNUMBER(SEARCH("UNITED",Q11))),
(H11*1%),
OR(ISNUMBER(SEARCH("CERBASI",Q11)), ISNUMBER(SEARCH("Specialist",Q11))),
(H11*0.5%),
ISNUMBER(SEARCH("Direct",Q11)),
(H11*0.3%),
ISNUMBER(SEARCH("Subordinate",F11)),
(H11*0%)),
R11<1,
(H11*($C$7/10000)),
H11*R11,
TRUE,
$C$1)
The TRUE is just my way of forcing a else statement on IFS, $C$1 will by the else statement. Or you could just have two ifs statements embedded in a single if statement
=IF(
Q11 > 1,
(IFS(
OR(ISNUMBER(SEARCH("COOP",Q11)), ISNUMBER(SEARCH("UNITED",Q11))),
(H11*1%),
OR(ISNUMBER(SEARCH("CERBASI",Q11)), ISNUMBER(SEARCH("Specialist",Q11))),
(H11*0.5%),
ISNUMBER(SEARCH("Direct",Q11)),
(H11*0.3%),
ISNUMBER(SEARCH("Subordinate",F11)),
(H11*0%)),
R11<1,
(H11*($C$7/10000)),
H11*R11)),
(IFS(
OR(ISNUMBER(SEARCH("COOP",Q11)), ISNUMBER(SEARCH("UNITED",Q11))),
(H11*1%),
OR(ISNUMBER(SEARCH("CERBASI",Q11)), ISNUMBER(SEARCH("Specialist",Q11))),
(H11*0.5%),
ISNUMBER(SEARCH("Direct",Q11)),
(H11*0.3%),
ISNUMBER(SEARCH("Subordinate",F11)),
(H11*0%),
R11<1,
(H11*($C$7/10000)),
H11*R11)))

Related

Why is my elif statement always executing in Godot?

I'm new to Godot programming, just to preface this. I'm trying to make it so that when you fall off of a platform without jumping you start accelerating downwards rather than just going down at a fixed speed. I almost have that, but my elif statement is happening every frame rather than the frame you leave the ground.
I've tried changing things, but I'm stuck at a logical dead end where any option I think of or find has resulted in the same or worse behavior.
extends KinematicBody2D
const UP_DIRECTION = Vector2.UP #up is up and down is down
# Declare member variables here.
var speed = 200 #speed of walk
var velocity = Vector2() #movement Vectors
var gravity = 1000 #gravity, it's real
var jump = 0 #jumping speed
var canjump = 0 #jumps left
var jumpcount = 1 #times you can jump
var nojump = false #did you fall or did you jump?
# Called every physics update.
func _physics_process(delta):
#jumping
if (canjump > 0 && Input.is_action_just_pressed("Jump") || is_on_floor() && canjump > 0 && Input.is_action_pressed("Jump")):
jump = -2000
canjump -= 1
nojump = false
#falling without jumping first
elif not (is_on_floor() && Input.is_action_just_pressed("Jump") && nojump == false):
jump = -gravity
canjump -= 1
nojump = false
#decelerating jumps
if (jump < 0):
jump += 45
#grounding jump variables
if(is_on_floor()):
canjump = jumpcount
nojump = true
#setting x and y
velocity.x = (Input.get_action_strength("Right") - Input.get_action_strength("Left")) * speed
velocity.y = gravity + jump
#using found inputs
velocity = move_and_slide(velocity, UP_DIRECTION)
pass
Here's my code, it tried commenting in the tdlr of what things are supposed to do. Again all I'm looking for is the reason why my elif statement is being applied every frame and how to fix that.
If you have a conditional that looks like this:
if condition:
prints("Something")
The execution flow would enter it when the condition is true.
Let us look at this as example:
if is_on_floor() && Input.is_action_just_pressed("Jump") && nojump == false:
prints("Something")
The above conditional requires three things:
is_on_floor() must be true.
Input.is_action_just_pressed("Jump") must be true.
nojump == false must be true. I mean, nojump must be false.
I think we can agree that does not happen all the time.
Now, if I negate the conditional:
if not condition:
prints("Something")
The execution flow would enter when the condition is false.
So, in a case like the one on your code:
if not (is_on_floor() && Input.is_action_just_pressed("Jump") && nojump == false):
prints("Something")
The conditional has one requirement:
not (is_on_floor() && Input.is_action_just_pressed("Jump") && nojump == false)`
Must be true. I mean:
is_on_floor() && Input.is_action_just_pressed("Jump") && nojump == false
Must be false. Double check D'Morgan's Law if you need to.
It is false when either:
is_on_floor() is false.
Or Input.is_action_just_pressed("Jump") is false.
Or nojump == false is false.
In other words, only one of these being false is sufficient.
To restate that. This condition:
not (is_on_floor() && Input.is_action_just_pressed("Jump") && nojump == false)`
Is equivalent to the following condition (by D'Morgan's law):
not is_on_floor() or not Input.is_action_just_pressed("Jump") or not nojump == false
By the way, using and and or is idiomatic in GDScript.
Wait, I can simplify that a bit:
not is_on_floor() or not Input.is_action_just_pressed("Jump") or nojump`
Thus, the execution flow will enter if either:
not is_on_floor() is true. I mean, if is_on_floor() is false.
not Input.is_action_just_pressed("Jump") is true. I mean, if Input.is_action_just_pressed("Jump") is false.
nojump is true.
And I reiterate that is either of them. Only one is sufficient. And I believe most of the time you haven't just pressed "Jump", also being on the air is enough. And I suspect nojump is false most of the time.
So, we can conclude that the execution flow will enter most of the time.
Write what you mean. You say in comments:
#falling without jumping first
Let us do that. What does falling mean? Does it mean on the air?
var falling := not is_on_floor()
Or do you also mean going down?
var falling := not is_on_floor() and velocity.dot(Vector2.DOWN) > 0.0
Alright, your pick. What does jumping first mean? Does it mean the player got in the air as a result of a jump? Ok... So you would do this when the player jumped:
jumped = true
Which I believe is this:
if canjump > 0 && Input.is_action_just_pressed("Jump"):
jumped = true
And reset it on the ground:
if is_on_floor():
jumped = false
Now we can say "falling without jumping first":
if falling and not jumped:
prints("something")
Almost as if it were the comment, but unlike the comment it can be executed.

Power Query column based on conditionals

Hey guys new to Power BI here and I am having some trouble creating a column based on conditionals in Power Query.
I am trying to do something like if the previous letter was A,B,or C and the current letter is E,F,or G mark True, else False.
My code looks like this so far:
Custom Column =
IF List.Contains({“A”, “B”,”C”}), [PrevLetter]
AND list.Contains({“E”, “F”, ”G”}) , [Letters]
THEN “TRUE”
ELSE “FALSE”
Output would be so something like:
PrevLetter
Letter
CustomColumn
A
F
True
X
M
False
B
E
True
C
F
True
Any help is truly appreciated!
The answer depends on if you are looking for a letter within a word, or if that letter represents the entire text, but for your example, one answer could be
= if Text.PositionOfAny([PrevLetter], {"A", "B","C"})+Text.PositionOfAny([Letter], {"E", "F","G"})=0 then true else false
or if you want to plan around nulls
= try if Text.PositionOfAny([PrevLetter], {"A", "B","C"})+Text.PositionOfAny([Letter], {"E", "F","G"})=0 then true else false otherwise false
or
= Table.AddColumn(Source, "Custom", each if List.ContainsAny({[PrevLetter]},{"A","B","C"}) and List.ContainsAny({[Letter]},{"D","E","F"}) then true else false )

Pine script condition 1 and condition 2 fulfilled at up to n steps back

Say I have condition 1 and condition 2. If condition 1 and condition 2 was met within up to, say, 5 bars, then I want to perform some action. As an example let's say condition 1 was met at current close, and condition 2 was fulfilled 5 bars ago, then I want to perform some action. How do I formulate that in Pine?
condition1 = ...
condition2 = ...
if (condition1(close)==true or condition1(close-2)==true or
condition1(close-3)==true or condition1(close-4)==true or
condition1(close-5)==true)
and (condition2(close)==true or condition2(close-2)==true or
condition2(close-3)==true or condition2(close-4)==true or
condition2(close-5)==true)
then...
Could it perhaps be formulated something like:
if condition1(close:close-5)== true and condition2(close:close-5)== true then ...
I have read e.g. this thread:
Change background for only the last 5 bars: A Very Simple Problem I can't crack
It sounds like a similar problem, but I am unsure about how to implement it.
a) You would need to use ta.barssince() function.
https://www.tradingview.com/pine-script-reference/v5/#fun_ta%7Bdot%7Dbarssince
//#version=5
indicator("My Script")
/// let's say condition 1 was met at current close, and condition 2 was fulfilled 5 bars ago, then I want to perform some action
ema10 = ta.ema(close, 10)
condition1 = close > open
condition2 = ta.crossover(ema10, close)
x = false
if condition1 and ta.barssince(condition2) == 5
x := true
plot(ema10)
bgcolor(condition2? color.orange:na)
bgcolor(x?color.green:na)
b) Another approach is to use history-referencing operator [].
https://www.tradingview.com/pine-script-docs/en/v5/language/Operators.html#history-referencing-operator
//#version=5
indicator("My Script")
// lets say you want to check if condition2 if true for 5 bars, and condition1 is true in the current bar
ema10 = ta.ema(close, 10)
condition1 = ta.crossover(ema10, close)
condition2 = close > open
condition3 = condition2[1] and condition2[2] and condition2[3] and condition2[4] and condition2[5]
x = false
if condition1 and condition3
x := true
plot(ema10)
bgcolor(condition2? color.orange:na)
bgcolor(x?color.green:na)

Why i cant check if a variable have a value using == true?

if(10) it is true, but if(10 == true) is false. Can someone tell me why the first case convert the number to bool but the second case didnt?
if (10) is equivalent to if (10 != 0), whereas if (10 == true) is if (10 == 1) (since true is promoted to the value 1 of type int).
In layman's terms: Two things that both happen to satisfy some property aren't automatically the same thing.
(E.g. doughnuts and frisbees are both round, but that doesn't mean a doughnut is equal to a frisbee. Integers and booleans can both be evaluated in a boolean context, but that doesn't mean that every integer that evaluates as true is equal to every true boolean.)
if( ... )
{
// if statement
}
To execute if statement in C++, ... should have a true value.
When you wrote if( 10 ){//something useful}
I think 10 treats as int but not bool variable. The following logic should be applied then
if( 10 ) -> if( bool( 10 ) ) -> if( true )
When you write if( 10 == true ){//something useful}, according to C++ standard, there should be the following logic behind the scene
if( 10 == true ) -> if( 10 == int( true ) ) -> if( 10 == 1 ) -> if( false )
You may write something like
if( 10 != false )
or
if( !!10 == true )
also
if( ( bool ) 10 == true ) // alternatively, if( bool ( 10 ) == true )
In old C (before C99), there is no false or true, but there are 0 or non-0 values.
In modern C (from C99), there is false or true (<stdbool.h>), but they are syntactic sugar for 0 and 1, respectively.
if( 10 ) // evaluates directly since 10 is non-zero value
if( 10 == true ) -> if( 10 == 1 ) -> if( 0 )
Because they are entirely different things.
In C, anything that is NOT false is automatically true, and C has a very strict definition of false.
You can think of if(10) as if(10 != false)
and likewise if (10 == true) as if((10 == true) != false)
10 is clearly not true, in the sense that 10 is a number and true is a boolean.
When you're in an if statement, the compiler has to evaluate the condition until it reaches either a true or a false. If it does not reach a true or a false, it has to convert it to a true or a false. As a general rule, 0 evaluates to false, and everything else evaluates to true.
So if(-1) is true, as is if(234) and so on.
The expression 10 == true already evaluates to false, so no further conversion is needed. if(10) is neither true or false, so the compiler has to convert it, using our rule above, and it becomes true.

if and else if do the same thing

I'm trying to refactor an if-else chain that doesn't look particularly good. My common sense is telling me that I should be able to call my method only once but I can't figure out an elegant way to do it. Currently what I have is:
if(condition1)
do method1;
else if(condition2)
do method1;
Which looks hideous. There's repeat code! The best I can come up with is:
if(condition1 || (!condition1 && condition2))
do method1;
But this also looks bad, since I'm negating condition1 after the or, which seems unnecessary...
I made a truth table for this:
c1| c2| r
0 | 0 | 0
0 | 1 | 1
1 | 0 | 1
1 | 1 | 1
And in case anyone is interested, the real-life problem I'm having is that I got 2 intances of Fancytree in javascript and I want to set some rules to transferring nodes between them. Tree A can only transfer lone nodes to Tree B, while Tree B can reorder itself freely, so I put this on Tree B's dragDrop event:
if(data.otherNode.tree === node.tree){
data.otherNode.moveTo(node, data.hitMode);
}
else if(!data.otherNode.hasChildren()){
data.otherNode.moveTo(node, data.hitMode);
}
You can simplify even more - if the first condition is true, the method should be invoked regardless of the second condition. So the !condition1 in your refactored code is redundant. Instead, you could just have:
if(condition1 || condition2)
do method1;
In modern programming languages the if condition will even short circuit. This means that when the first condition is evaluated as true, the second condition won't even get evaluated.
What you suggested,
if(condition1 || (!condition1 && condition2))
do method1;
Is logically the same as
if(condition1 || condition2)
do method1;
So I think that is your best answer.
It's not logically the same as your truth table though, so either your truth table or your current code is wrong, if truth table r is meant to do
do method1;
In writing
if (condition1 || condition2) {
//code1
}
if condition1 is correct code1 is executed and if condition1 is incorrect then only condition2 is checked and code proceeds accordingly. Hence it would be same as
if ( condition1 ) {
//method1
} else if ( condition2 ) {
//method1
}