TCL 0 not equal to FALSE - boolean

I tried
expr 0==false
but it returns 0 instead of 1.
According to http://wiki.tcl.tk/16295, False values are the case-insensitive words no, off, false, their unique abbrevations, and 0.
It is weird, or my understanding are wrong?

While specifying operands for expr command, to validate against a boolean value, we should use only string is command.
% expr {0==false}
0
% expr {[string is false 0]}
1
Simply validating against boolean equal == will treat them as if like literal string/list.
Reference : expr

Related

How to convert numbers into boolean in Julia?

I want to convert numbers into equivalent boolean. based on the official doc:
"false is numerically equal to 0 and true is numerically equal to 1."
So I would like to convert the numeric values to the equivalent boolean. Example of expected behavior:
[1] Bool(5)
> true
But, Julia gives me:
ERROR: InexactError: Bool(5)
Stacktrace:
[1] Bool(x::Int64)
# Base .\float.jl:158
[2] top-level scope
# REPL[26]:1
But if I say:
[1] Bool(0.0)
# result
> false
It looks good on 0 and 0.0! But it doesn't work on numbers like 5 or 2.1, etc.
The quick answer is that it seems like you want to test whether a value is non-zero or not and you can test if a number x is non-zero like this: x != 0. Whether a number is considered true or false depends on what language you're in, however; more on that below.
Having gotten the quick answer out of the way, we can talk about why. The docs say that false and true are numerically equal to zero and one. We can test this out ourselves:
julia> false == 0
true
julia> true == 1.0
true
You can see that the equality holds regardless of numerical type—integers or floats or even more esoteric number types like complexes or rationals. We can also convert booleans to other types of numbers:
julia> Int(false)
0
julia> Float64(true)
1.0
We can convert those values back to booleans:
julia> Bool(0)
false
julia> Bool(1.0)
true
What you can't do is convert a number that isn't equal to zero or one to boolean:
julia> Bool(5)
ERROR: InexactError: Bool(5)
That is because the Bool type can only represent the values zero and one exactly and trying to convert any other numeric value to Bool will give an InexactError. This is the same error you get if you try to convert a float that isn't integer-valued to an integer type:
julia> Int(5.0)
5
julia> Int(5.5)
ERROR: InexactError: Int64(5.5)
Or an integer to a smaller type that isn't big enough to represent that value:
julia> Int8(123)
123
julia> Int8(1234)
ERROR: InexactError: trunc(Int8, 1234)
The exact same thing is happening here: Bool is not big enough to represent the value 5, so you get an error if you try to convert the value 5 to Bool.
The convention that many languages use for truthiness of numbers is that values that represent zero are falsey and values that represent no-zeros are truthy. Note that there is no sound mathematical reason for this: zero is not false and non-zero numbers are not true; it's just a convention that comes from the C programming language, which doesn't have a boolean type and uses this convention for treating integers as true/false in conditionals. This convention is far from universal, however, as there are popular languages that don't follow it: in Lisps and Ruby, for example, all numbers are truthy. I wrote a post recently on the Julia discourse forum about differing notions of truthiness in different languages and why Julia rejects truthiness and instead requires you to write out an explicit condition like comparison with zero for numbers, or non-emptiness for collections.
Since the condition you actually want to check is whether a number is equal to zero or not, that's just what you should do: explicitly compare the number to zero by doing x != 0. This will evaluate to a boolean value: true if x is non-zero and false if x is equal to zero. There's a predicate function that does this as well: iszero(x) checks if x is zero, which can be handy if you want to e.g. count how many zero or non-zero values there are in a collection:
julia> v = rand(-2:2, 100);
julia> count(iszero, v)
18
julia> count(!iszero, v)
82

How to use length and rlike using logical operator inside when clause

Want to check if the column has values that have certain length and contains only digits.
The problem is that the .rlike or .contains returns a Column type. Something like
.when(length(col("abc")) == 20 & col("abc").rlike(...), myValue)
won't work as col("abc").rlike(...) will return Column and unlike length(col("abc")) == 20 which returns Boolean (length() however also returns Column). How do I combine the two?
After doing a bit of searching in compiled code, found this
def when(condition : org.apache.spark.sql.Column, value : scala.Any) : org.apache.spark.sql.Column
Therefore the conditions in when must return Column type. length(col("abc")) == 20 was evaluating to Boolean.
Also, found this function with the following signature
def equalTo(other : scala.Any) : org.apache.spark.sql.Column
So, converted the whole expression to this
.when(length(col("abc")).equalTo(20) && col("abc").rlike(...), myValue)
Note that the logical operator is && and not &.
Edit/Update : #Histro's comment is correct.

IntValue ?? 0 == -1? "-": "+" What does this mean?

I am importing price information and adding + or -.
I put the title code in print () and it works but I do not know what it means.
print("\(IntValue ?? 0 == -1 ? "-" : "+")")
Please explain it briefly to me.
Kevin's answer is very good.
Some background that helps explain further:
The code you posted uses two rather cryptic operators together.
?? is the nil-coalescing operator.
It takes an optional value, which can contain nil, and provides a new value to use when it does contain nil.
Edit:
(Note that you can skip the nil-coalescing operator and use IntValue == -1 instead. That works because only a non-nil value of -1 is equal to -1. An optional that contains nil is not equal to -1.
You could rewrite the line as
print("\(IntValue == -1 ? "-" : "+")")
And get the same result.)
The next tricky bit is the "ternary operator". This comes from C. It's quite cryptic, but also quite useful.
It takes the form boolean ? value_for_true : value_for_false
Where boolean is a boolean expression that evaluates to true or false.
If boolean is true, then the result of the whole ternary expression is the value_for_true sub-expression.
If boolean is false the result of the whole ternary expression is the value_for_false sub-expression.
IntValue ?? 0 == -1 is the boolean part of your ternary expression. It evaluates as true if IntValue is -1. It evaluates as false if IntValue contains any other value, or if it contains nil.
(Note that variables and let constants should start with lower-case letters, so IntValue should be intValue.)
The variable IntValue is an optional, which means its either an Integer or nil. IntValue ?? 0 means that if IntValue exists, then use the value of IntValue. If IntValue is nil, then use the value 0. Next, compare that value with -1. If that value is equal to -1, then print -. If that value does not equal -1, then print +.
Here's equivalent code with only if statements:
var defaultInt = 0
if IntValue != nil {
defaultInt = IntValue! // force unwrap the optional value
}
if defaultInt == -1 {
print("-")
}
else {
print("+")
}

What do Perl functions that return Boolean actually return

The Perl defined function (and many others) returns "a Boolean value".
Given Perl doesn't actually have a Boolean type (and uses values like 1 for true, and 0 or undef for false) does the Perl language specify exactly what is returned for a Boolean values? For example, would defined(undef) return 0 or undef, and is it subject to change?
In almost all cases (i.e. unless there's a reason to do otherwise), Perl returns one of two statically allocated scalars: &PL_sv_yes (for true) and &PL_sv_no (for false). This is them in detail:
>perl -MDevel::Peek -e"Dump 1==1"
SV = PVNV(0x749be4) at 0x3180b8
REFCNT = 2147483644
FLAGS = (PADTMP,IOK,NOK,POK,READONLY,pIOK,pNOK,pPOK)
IV = 1
NV = 1
PV = 0x742dfc "1"\0
CUR = 1
LEN = 12
>perl -MDevel::Peek -e"Dump 1==0"
SV = PVNV(0x7e9bcc) at 0x4980a8
REFCNT = 2147483647
FLAGS = (PADTMP,IOK,NOK,POK,READONLY,pIOK,pNOK,pPOK)
IV = 0
NV = 0
PV = 0x7e3f0c ""\0
CUR = 0
LEN = 12
yes is a triple var (IOK, NOK and POK). It contains a signed integer (IV) equal to 1, a floating point number (NV) equal to 1, and a string (PV) equal to 1.
no is also a triple var (IOK, NOK and POK). It contains a signed integer (IV) equal to 0, a floating point number (NV) equal to 0, and an empty string (PV). This means it stringifies to the empty string, and it numifies to 0. It is neither equivalent to an empty string
>perl -wE"say 0+(1==0);"
0
>perl -wE"say 0+'';"
Argument "" isn't numeric in addition (+) at -e line 1.
0
nor to 0
>perl -wE"say ''.(1==0);"
>perl -wE"say ''.0;"
0
There's no guarantee that this will always remain the case. And there's no reason to rely on this. If you need specific values, you can use something like
my $formatted = $result ? '1' : '0';
They return a special false value that is "" in string context but 0 in numeric context (without a non-numeric warning). The true value isn't so special, since it's 1 in either context. defined() does not return undef.
(You can create similar values yourself with e.g. Scalar::Util::dualvar(0,"").)
Since that's the official man page I'd say that its exact return value is not specified. If the Perl documentation talks about a Boolean value then then it almost always talks about evaluating said value in a Boolean context: if (defined ...) or print while <> etc. In such contexts several values evaluate to a false: 0, undef, "" (empty strings), even strings equalling "0".
All other values evaluate to true in a Boolean context, including the infamous example "0 but true".
As the documentation is that vague I would not ever rely on defined() returning any specific value for the undefined case. However, you'll always be OK if you simply use defined() in a Boolean context without comparing it to a specific value.
OK: print "yes\n" if defined($var)
Not portable/future proof: print "yes\n" if defined($var) eq '' or something similar
It probably won't ever change, but perl does not specify the exact boolean value that defined(...) returns.
When using Boolean values good code should not depend on the actual value used for true and false.
Example:
# not so great code:
my $bool = 0; #
...
if (some condition) {
$bool = 1;
}
if ($bool == 1) { ... }
# better code:
my $bool; # default value is undef which is false
$bool = some condition;
if ($bool) { ... }
99.9% of the time there is no reason to care about the value used for the boolean.
That said, there are some cases when it is better to use an explicit 0 or 1 instead of the boolean-ness of a value. Example:
sub foo {
my $object = shift;
...
my $bool = $object;
...
return $bool;
}
the intent being that foo() is called with either a reference or undef and should return false if $object is not defined. The problem is that if $object is defined foo() will return the object itself and thus create another reference to the object, and this may interfere with its garbage collection. So here it would be better to use an explicit boolean value here, i.e.:
my $bool = $object ? 1 : 0;
So be careful about using a reference itself to represent its truthiness (i.e. its defined-ness) because of the potential for creating unwanted references to the reference.

Boolean for whether a string contains a substring

Suppose I have a text variable $$string.
How can I write a boolean to check whether $$string contains the text $$substring?
e.g. if $$string is "foobar" and $$substring is "oo", then the result should be True, and if the $$string is "foo" and $$substring is "bar" the result should be False.
For a problem like this I'm partial to the PatternCount function:
PatternCount($$string ; $$substring)
You should then get back false = 0 or true >= 1. You can force true to 1 as follows:
PatternCount($$string ; $$substring) > 0
Function Definition here: http://fmhelp.filemaker.com/fmphelp_10/en/html/func_ref3.33.73.html
Use the Position function:
Position($$string;$$substring;1;1)>0
Note: Position($$string;$$substring;a;b) checks whether $$substring is contained at least b-times in $$string from starting position a, and returns where the b th occurrence is located in $$string, or -1 if there is no b th occurrence. Counting starts from 1.