i and j are both booleans, and are TRUE. k is also a boolean, and I've just initialized it as i+j.
Should I expect k to be TRUE in some programming languages, and FALSE in others?
Short answer is you shouldn't be adding booleans in the first place... but here's a summary of the behavior of various languages.
Languages in which true + true is true:
#define TRUE 1
. Therefore TRUE + TRUE == 2
, which is also nonzero -- and therefore also true. (but not necessarily TRUE)bool
data type. Essentially it's just typedef enum {false, true} bool;
which makes true
1 -- so again, true + true
is 2.t + t
returns t
.True + True
returns 2; Python handles anything non-empty and non-zero as True.true + true
returns 2, which is a true value.true + true == true
.true + true
returns 2, which is also true.Languages in which true + true raises an error
true + true
results in a NoMethodError. Additionally, booleans cannot be coerced into numbers (0 + true
also throws an error)true + true
results in the error "attempt to perform arithmetic on a boolean value".true
and false
are actually quite screwy, and personally I just use ints because it makes more sense to me, but I don't code in Tcl much anyway. However, [expr true + true]
doesn't work, returning can't use non-numeric string as operand of "+"
.Note that many languages in the latter category still interpret any non-zero integer value as true in an if statement, but if you're starting off with a boolean, you won't get to that point -- at least not without some amount of coercing.
So in summary, not one of the languages I have handy will evaluate TRUE + TRUE to FALSE, but several of them will result in an error of some kind. However it's still not generally a good idea to add booleans; after all, they're boolean, so you're better off using a proper boolean operator (e.g., i or j
or equivalent).
Honestly, I can't think of any reason I would. But I have to admit to being curious. Thanks for the comprehensive answer.
No problem. I have too much time on my hands.
Java raises an error as well (operator + cannot be applied to boolean,boolean
), and so does Scheme.
It's also worth noting that PHP does have a real boolean type, it's just that when adding them, the variables automatically get cast to integers (with value 1) and added, and any non-zero integer (2 in this case) evaluates to true as well.
Casting the sum to a boolean and back will also give you 1, not 2.
PHP's type system is hilarious.
>>5
Lots of languages have the same boolean/int semantics. Try it with javascript, python, etc. -- they'll all do the same thing.
I don't see the problem of summing booleans. It makes perfect sense in the context of counting. sum([x%5 == 0 for x in range(100)])
would tell you how many numbers are divisible by five in the range 0 to 99 in Python (namely, 20) for example.
>>7
And that works well as long as the language has loose typing (as Python does). If not, it should be an error.
boolean logic is mod 2.
In boolean logic, + is equal to ^.
>>4
You also don't know Common Lisp, t is not the answer it's a type error, gb2 /prog/ and read SICP
>>10
Is that C99? I haven't really done C in years.
>>11
Oh yeah. Dunno what I was thinking:
[1]> (+ t t)
*** - +: T is not a number
Regardless, shut up. T is not a "type error", it's the result of a value not enclosed in a s-expression. Also, SICP isn't even CLisp, it's Scheme, and /prog/ is full of shitheads like you who think they're awesome because they halfassed their way through a beginner-level book. So how about you go away.
> Is that C99? I haven't really done C in years.
> Also, SICP isn't even CLisp, it's Scheme
> /prog/ is full of shitheads like you who think they're awesome because they
> halfassed their way through a beginner-level book. So how about you go away.
> Regardless, shut up. T is not a "type error", it's the result of a value not
> enclosed in a s-expression.
ITT nerd rage.
> So there you have it. Not type error, nor the one and only output when an expression is evaluated. (not S-expression)
Yeah that's pretty much what I was going for, except better-written. By "a value" I was referring to that specific value -- either 't' or '+' (which, in this case, also printed 't').
I have read SICP, and I believe it is a first-level book, so long as you understand the underlying mathematics. Not so good for students who are still trying to figure out derivatives.
Anyway, welcome to 4-ch, I'm pretty sure you'll like it here. Just do less trolling. ;)
>>8
What I'm trying to say is that I really think it should be like that in any language. I really don't see the problem of having true and false converted into the integers 1 and 0 as it is very helpful and not just done for hardware reasons. Another example would be the way booleans can be used for subscripting the nodes of a BST when the nodes are declared as an array of two elements in C.
Yeah that's pretty much what I was going for, except better-written. By "a
value" I was referring to that specific value -- either 't' or '+' (which, in
this case, also printed 't').
I have read SICP, and I believe it is a first-level book, so long as you
understand the underlying mathematics. Not so good for students who are still
trying to figure out derivatives.
Anyway, welcome to 4-ch, I'm pretty sure you'll like it here. Just do less
trolling. ;)
I've been browsing 4-ch for some time now, but my posts are a-few. It's the
first time someone calls me a troll, and I'd expect that to be quite rare in an
anonymous community. As such, I'd also like you to elaborate on the matter. I
know what I am, and I'm certainly not a troll. What are the odds for a
delusionary fool whose posts are, unintentionally similar to those of a troll?
Just in case, let me repeat this once more, I am not a troll. Please do
not consider me as one, because it certainly makes me feel shitty since my posts
are genuine.
>>16
What I'm trying to say is that I really think it should be like that in any
language. I really don't see the problem of having true and false converted into
the integers 1 and 0 as it is very helpful and not just done for hardware
reasons.
I doubt a boolean data type is of any use for most languages.
Another example would be the way booleans can be used for subscripting
the nodes of a BST when the nodes are declared as an array of two elements in C.
Or, what I would do in the absence of a boolean data type is to use AND 1 if t +
t = f, or NOT NOT if t + t = t. As you see, it also depends on the semantics and
behavior of a boolean to decide which is most appropriate. Alas these semantics
might not be there, and what's for a programmer to do then? A concrete example
of rotten documentation (or absence thereof) would be Xorg. Take a look, and my
post will get a whole differend meaning to you - I am positive about this.
>>17
I'm sorry, my HTML wasn't correct.
>>17
"gb2 /prog/ and read SICP" is fairly standard troll rhetoric if you ask me. Notwithstanding, your other points are valid.
Maybe this wont work, but why not just set it as True when you declare it? That way you dont have to wonder about certain languages.