What does !! (double exclamation point) mean? - perl

In the code below, from a blog post by Alias, I noticed the use of the double exclamation mark !!. I was wondering what it meant and where I could go in the future to find explanations for Perl syntax like this. (Yes, I already searched for !! at perlsyn).
package Foo;
use vars qw{$DEBUG};
BEGIN {
$DEBUG = 0 unless defined $DEBUG;
}
use constant DEBUG => !! $DEBUG;
sub foo {
debug('In sub foo') if DEBUG;
...
}
UPDATE
Thanks for all of your answers.
Here is something else I just found that is related The List Squash Operator x!!

It is just two ! boolean not operators sitting next to each other.
The reason to use this idiom is to make sure that you receive a 1 or a 0. Actually it returns an empty string which numifys to 0. It's usually only used in numeric, or boolean context though.
You will often see this in Code Golf competitions, because it is shorter than using the ternary ? : operator with 1 and 0 ($test ? 1 : 0).
!! undef == 0
!! 0 == 0
!! 1 == 1
!! $obj   == 1
!! 100 == 1
undef ? 1 : 0 == 0
0 ? 1 : 0 == 0
1 ? 1 : 0 == 1
$obj ? 1 : 0 == 1
100 ? 1 : 0 == 1

not-not.
It converts the value to a boolean (or as close as Perl gets to such a thing).

Because three other answers claim that the range is "0" or "1", I just thought I'd mention that booleans in Perl (as returned by operators like ==, not, and so on) are undef and 1, not 0 and 1.

Related

OBV Calculation

obv = cum(change(src) > 0 ? volume : change(src) < 0 ? -volume : 0*volume)
obv2 = cum(sign(change(src)) * volume )
obv and obv2 return different results
ta.obv(df['close'], df['volume']) returns the same result as obv2
I will also need the return value of obv calculation with talib
OBV calculation
Thanks in advance for your help.
I've added an OBV indicator to TradingView, opened it's sources and found
obv = ta.cum(math.sign(ta.change(src)) * volume)
Then I cloned the indicator and replaced this line with
obv = ta.cum(ta.change(src) > 0 ? volume : ta.change(src) < 0 ? -volume : 0*volume)
And added it as a second indicator. I've got a different results. But also I've got a warning:
line 9: The function 'ta.change' should be called on each calculation
for consistency. It is recommended to extract the call from the
ternary operator or from the scope
So, I replaced the line with
c = ta.change(src)
obv = ta.cum(c > 0 ? volume : c < 0 ? -volume : 0*volume)
And got exactly the same results as original OBV!
So your code for obv is incorrect and you shall google for explanation of this warning. For example: The function should be called on each calculation for consistency, console output?

what is mean by swift condition is if ((status & 0x3F) == 1 ){ }

if ((status & 0x3F) == 1 ){ }..
the status is variable in swift language.
what is mean about this condition, & mean and (status & 0x3F) value return
& is the bitwise AND operator. It compares the bits of the two operands and sets the corresponding bit to 1 if it is 1 in both operands, or to 0 if either or both are 0.
So this statement:
((status & 0x3F) == 1)
is combining status with 0b111111 (the binary equivalent of 0x3F and checking if the result is exactly 1. This will only be true if the last 6 bits of status are 0b000001.
In this if:
if( (dtc24_state[2] & 0x8) == 0x8 ) {
self.haldexABCDTC24State.text = status_str + " - UNKNOWN"
self.haldexABCDTC24State.textColor = text_color
active_or_stored_dtc = true
}
dct24_state is an array of values. The value of dct24_state[2] is combined with 0x8 or 0b1000 and checked against 0x8. This is checking if the 4th bit from the right is set. Nothing else matters. If the 4th bit from the right is set, the if is true and the code block is executed.
0x3F is 111111. So, it means this:
for each bit of yourNumber in binary system presentation use and method.
This way truncates the left part of the number. and the result compares with 1.
e.g.
7777 is 1111001100001 after executing and this number converts into
100001. So the result is false.
But for 7745 (1111001000001) the result is 1. The result is true.
The rule for 'and' function: 0 & 0 = 0 ; 0 & 1 = 0; 1 & 0 = 1; 1 & 1 = 1.

How to normalize Integer variable to positive negative or zero with bit operations

7 -> 1
0 -> 0
-7 -> -1
I've have code:
(x == 0 ? 0 : x / abs(x)) + 1
but is it possible to avoid division and make it faster?
How about
(x == 0 ? 0 : (x < 0 ? -1 : 1))
The idea was to use bit operations to avoid branching code or value conversion.
Haven't found how to do it with bit operations but apple already add this function
https://developer.apple.com/documentation/swift/int/2886673-signum
signum()
Returns -1 if this value is negative and 1 if it’s positive; otherwise, 0.
so simple) raw test shows ~x100 faster implementation

Using the 'not' operator in Boolean statements for Matlab

If a statement like ~(5>4) || (1>10) is given what does it evaluate to in MATLAB?
What I think is the answer is :
~1 || 0 = 0 || 0
= 0
so it evaluates to false.
Am I correct in that the not operator only attaches to the (5 > 4)?
In this case, Matlab would evaluate the logical operators from left to right. See Operator Precedence for instances when this would not be the case.
That would mean that ~(5>4) | (1>10) would first evaluate the left half of the OR:
~(5>4) --> ~(1) --> 0
Then continuing...
0 | (1>10) --> 0 | 0 --> 0

coffeescript refactoring

Is there any coffeescript specific trickery that would make this look neater:
index = (->
if segment == 'index'
return 0
else if segment == 'inbox'
return 2
1
)()
Yes, a switch expression:
index = switch segment
when 'index' then 0
when 'inbox' then 2
else 1
You could use an inline if ... then ... else statement broken up into multiple lines (for readability) by using the \ character.
index = if segment == 'index' then 0 \
else if segment == 'inbox' then 2 \
else 1
This is useful if your conditional logic is too complex for a simple switch block.
Yes, the CoffeeScript-specific existential operator:
index = {'index': 0, 'inbox': 2}[segment] ? 1
You could also use an inline if statement to get rid of the function call:
index = if segment == 'index' then 0 else if segment == 'inbox' then 2 else 1
But an inline if wouldn't be any harder in straight Javascript:
index = segment == 'index' ? 0 : segment == 'inbox' ? 2 : 1