Answer» I was helping a friend with a Python assignment and in their code I noticed something along the lines of this:
Code: [Select]if x < (y and z):
They were a bit of a novice at PROGRAMMING and I take it that their intention was that they wanted to simply check if x was less than y and z, but when the were getting incorrect SELECTIONS from this conditional, I suggested that they simply change it to
Code: [Select]if x < y and x < z: which then worked out.
Now here's what I'm curious about. What does a language do when the operands of a boolean operator are not true/false or 1/0?
What does (y and z) equal when y and z are say 1.5 and 0.2? Does it vary from language to language?
I personally have little experience with Python and in other languages like C++ or Java this would simply error. But the very fact that the code ran in Python has me a little confused.1. The word "Boolean" comes from the name of George Boole, so is spelled with a capital B, although many people don't SEEM to bother these days.
2. A Boolean or logical data type can have one of two values (usually denoted true and false). These can be represented with 1 bit which can either be 1 or 0. In most languages when you try to do a Boolean operation on data types of more than 1 bit, a "bitwise" operation is performed, that is, each bit of one number, with each bit of another number, performing a logic operation for every bit.
The boolean math expression below describes this:
e.g.
00001111 AND 00011110 -------- equals 00001110
I don't think it makes sense to try and do Boolean stuff with non integers. Ah ok. I knew of SEPARATE bitwise operators like & and | in addition to comparison operators like && and || in Java/C++, but I hadn't thought that 'and' and 'or' would automatically do that for inputs with binary representations that require more than 1 bit. This is the first time I've seen it happen.
So for the example I indicated, should I assume that Python took the binary representations of 1.5 and 0.2 and performed bitwise 'and' on them?
Also I'll keep capitalization of Boolean in mind from now on I'm not sure how Python represents non integer values.Or how it does logical operations on non-Boolean data types, if it does them at all. My example was from FreeBasic. Like I said, I don't think it makes sense to try bitwise operations with non integer values. Did you understand why I wrote that? (Do you know what "floating point" means?)
Well I have some sense of what floating point means. I did recently learn the IEEE representation for floating point numbers, which are essentially decimal values.Some time ago, Bill Gates, or somebody like him, made the run that false would be 0 and true would be -1 using 16 bit integer on the Intel 8080 CPU. the minus one is expressed in binary as: 1111 1111-1111 1111 The Intel CPU can test for zero or non-zero values. Engineers like to use this to read the status of external devices. Thus any value that is not zero is a true value.
In other words, the practice is more practical than pure.
Interesting
|