Shop OBEX P1 Docs P2 Docs Learn Events
What is TRUE? — Parallax Forums

What is TRUE?

K2K2 Posts: 693
edited 2011-07-24 12:00 in Propeller 1
The Propeller Manual indicates that TRUE is -1 and FALSE is 0. Under the explanation of the IF command, the manual says that a "valid" condition is one that evaluates to TRUE...

This would seem to be a radical departure from the convention in many other languages, that anything non-zero is considered TRUE and only 0 is FALSE.

Yet if I write this in Spin...
IF 1
  K2 := king

...I become king. What gives?

Comments

  • blittledblittled Posts: 681
    edited 2011-07-21 13:30
    I haven't looked at the manual but I believe False is 0 and True is any non zero value
  • mindrobotsmindrobots Posts: 6,506
    edited 2011-07-21 13:41
    This was in the "Similar threads" section of your thread: http://forums.parallax.com/showthread.php?105627-Why-is-TRUE-1

    It's been a topic of discussion and merriment before.

    ZERO is FALSE
    Non-ZERO is TRUE

    seems to be the general consensus.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2011-07-21 13:53
    The predefined constant true == -1 ($ffff_ffff). However, for a condition to evaluate to "true" it need only be non-zero, i.e. not false. I guess you could say that any non-zero value posesses truthiness, even though it might not equal true. :)

    -Phil
  • Mark_TMark_T Posts: 1,981
    edited 2011-07-21 13:57
    It appears the description in the manual about IF is wrong - a condition is deemed valid if it doesn't evaluate to FALSE (0).
  • localrogerlocalroger Posts: 3,452
    edited 2011-07-21 13:57
    For purposes of evaluation, e.g. the expression behind an if, any nonzero value is true.

    -1 is used as true so that a bitwise inverse (what NOT does in most languages) will flip it to FALSE (0). If you use another value, not-true isn't false.

    But Spin adds another layer, because in Spin NOT returns false for any nonzero input; it's not a bitwise inverse. The bitwise inverse operator is an exclamation mark.
  • K2K2 Posts: 693
    edited 2011-07-21 14:05
    On page 167 of the current manual I found an interesting sentence fragment...

    "The Boolean AND operator compares two operands and returns TRUE (-1) if both values are TRUE (non-zero)..."

    Here on out, when I encounter "TRUE" in the manual, I will interpret it as I like. :) This just proves how much simpler PASM is.

    Thanks for the explanations and the link to a previous discussion of this.
  • Dave HeinDave Hein Posts: 6,347
    edited 2011-07-21 14:46
    Watch out for expressions like "if value == TRUE". It's not the same as "if value" or "if value <> FALSE". A value could be non-zero, but it's not necessarily equal to TRUE.
  • localrogerlocalroger Posts: 3,452
    edited 2011-07-21 15:04
    Although Spin provides logic operators that work with "any nonzero value" as TRUE, it's really a good idea to get in the habit of using -1 consistently because that's how just about every other language in existence implements them.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2011-07-21 15:24
    localroger,

    In Perl, a conditional evaluates to "true" if it's defined and not 0, "", or "0". Every BASIC I've ever seen considers a non-zero value to be true when used in conditionals. Which languages were you referring to that require a conditional to evaluate to -1 before it's true?

    -Phil
  • prof_brainoprof_braino Posts: 4,313
    edited 2011-07-22 08:59
    Is 0=FALSE and non-zero=TRUE for fuzzy logic? Does it mean we can use X > Y to test is a condition is "More True" ?
  • jazzedjazzed Posts: 11,803
    edited 2011-07-22 09:47
    It is generally TRUE that 0 is false. There may be a parallel universe where -1 is false though :)
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2011-07-22 10:14
    My first computer (a Poly 88) used a variant of HP BASIC in which Booleans evaluated to 1 or 0, instead of -1 or 0. I wasn't until I ran across Microsoft BASIC on the TRS80 that I encountered the true == -1 scheme. But, IIRC, in Microsoft BASIC, all Booleans are bitwise operations; there are no logical Booleans that promote truth to true the way Spin and other modern languages do.

    One thing Spin lacks that I wish it had is lazy Booleans: && and ||. With these, the expression to the right is not executed if the result of the Boolean is already known. They're really handy for creating less verbose code. Another is the ternary conditional expression: result := condition ? evaluate_if_true : evaluate_if_false .

    I realize that ||, ?, and : are otherwise deployed. But their additional use in the situations noted above would be syntactically unambiguous.

    -Phil
  • localrogerlocalroger Posts: 3,452
    edited 2011-07-22 11:21
    Which languages were you referring to that require a conditional to evaluate to -1 before it's true?

    It's not that they interpret $0001 as false, it's that all the logical operations are bitwise and only work right on all-0 and all-1 multibit values. This has been the case for every platform I've ever used, since that's the way it is in assembly language. If you use 1 as TRUE and perform a typical NOT on it you get $FFFE, which is still true.
  • jazzedjazzed Posts: 11,803
    edited 2011-07-22 11:23
    localroger wrote: »
    If you use 1 as TRUE and perform a typical NOT on it you get $FFFE, which is still true.
    Ergo, anything not false should be true :)
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2011-07-22 11:27
    localroger wrote:
    If you use 1 as TRUE and perform a typical NOT on it you get $FFFE, which is still true.
    But what's a "typical NOT?" There's "not" and there's "!"; and they're necessarily different from each other.

    Abstracting logical operators from their bitwise roots is very useful in high level languages. Perl actually has three "and" operators (and similar for "or"): & (bitwise), && (lazy logical), and and (lazy, lower-precedence logical) -- all of them helpful in their given contexts.

    -Phil
  • localrogerlocalroger Posts: 3,452
    edited 2011-07-22 19:39
    Phil, the "typical NOT" is the one in ASM, which is word length bitwise. I have never seen an assembly language that provides for "nonzero multibit value true" on logical operations. That extends to most high level languages, which unless their creators create a special class of instructions will inherit this behavior.
  • Toby SeckshundToby Seckshund Posts: 2,027
    edited 2011-07-22 23:28
    All this "what is true" and "what is false" sounds like a re-run of my divorce case.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2011-07-23 00:13
    localroger wrote:
    Phil, the "typical NOT" is the one in ASM, which is word length bitwise.
    Not to niggle, but it's only "typical" if you're writing assembly code. I'm not quite sure what this has to do with a high-level language. In assembly we have the luxury of testing condition flags, viz:
            test  a,a wz
       if_z test  b,b wz
       if_z jmp   #:false
            add   a,#16
    :false  ...
    

    We do this all the time, but we don't have that luxury in Spin, for example; hence the distinction between logical and bitwise Booleans. The equivalent in Spin, of course, is
    [b]if[/b] (a [b]or[/b] b)
      a += 16
    

    which is way better than having to write if ((a <> 0) | (b <> 0)).

    -Phil
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2011-07-23 06:20
    "What is True?"

    I'm washing my hands of it, entirely. :zombie:
  • localrogerlocalroger Posts: 3,452
    edited 2011-07-23 06:57
    Phil, the reason the way it's done in assembly is important is that higher level languages are written in assembly. Spin's behavior is not at all standard.

    The typical syntax for a HLL if statement is given as: IF {condition} THEN {do stuff}. You'll note that {condition} is not specified as to type; it's a generic mathematical expression evaluation, which can result in any 32-bit value, and the typical HLL will interpret any nonzero result as true and {do stuff}. Logical operations like AND, OR, and NOT are just math operators like *, +, and /, except higher on the operator precedence hierarchy. Comparison operators like =, <>, and so on all return -1 for TRUE because if they didn't, further logical operations on their results (like A = 3 OR NOT (B <> 4)) would not work. The reason the HLL does it this way is twofold; first, as I said above that's the instruction provided by the CPU, and second, if the HLL did like Spin and abstracted any nonzero input to TRUE, you couldn't use AND and OR for bitwise mask manipulation. You'd have to provide a different function for that -- which Spin does. But Spin is the only development system I've ever used that does it that way. In most of the world, TRUE is -1 so that the same CPU instructions used for bitwise logical operation can also be used for logical expression evaluation.
  • Dave HeinDave Hein Posts: 6,347
    edited 2011-07-23 08:08
    Roger,

    I'm a bit confused about the dialog between you and Phil. First of all, it is possible to do the Spin statement "a := b and c", where the values of "b" or "c" don't have to be only 0 or -1. Of course, the result stored in "a" will be either 0 or -1. Also, C uses the value of 1 for TRUE. It is complemented in C with the ! operator, which is a logical operator in C, and not binary. I have also used (1 ^ x) and (1 - x) to complement the logical value in of x in C.

    Using -1 for TRUE is nice because you can do something like "a := (x & a) | (!x & b)" in Spin to select either the value of a or b depending on the value of x. I have also done something like "a += x & incr" to conditionally add incr to a depending on the logical value of x. In C, I would do something like "a += x * incr", which is OK if the processor has a single-cycle multiple. An alternate method in C would be "a += (-x) & incr". Of course C also has the ?: operator that provides a better way to do that.

    Dave
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2011-07-23 08:29
    localroger wrote:
    But Spin is the only development system I've ever used that does it that way.
    I guess what's "typical" all depends on past experience. To me, as a Perl programmer, Spin's approach seems both natural and logical. Perl, like Spin, does not have a Boolean type, so it's not fussy about what kind of values get presented to its logical operators.

    But even in C, which is about as typical as typical gets for some people, the && and || operators treat any non-zero argument as true and return 1 for true (instead of -1) and 0 for false.

    -Phil
  • jazzedjazzed Posts: 11,803
    edited 2011-07-23 10:06
    I guess what's "typical" all depends on past experience.
    Indeed. See .sig :)
  • localrogerlocalroger Posts: 3,452
    edited 2011-07-23 14:58
    Phil, it is kind of ridiculous to compare systems like Spin and Basic which have strongly typed integer variables with systems like Perl and Javascript which do everything in double precision floating point whether you want them to or not and aren't even too picky about whether something is a number or a string. Sure, in a system like that the code that tries to figure out what you're up to can coerce to a special one-bit boolean if the designer thought of it, but that is not how the kind of languages you would use in a limited environment are going to work. In fact, I've done a little work with Javascript which works similarly, and have always thought loosely typed variables are one of the dumbest ideas ever to infest computerland. The fact that you pretty much have to use Javascript to get anything done on the web is highly annoying for mainly this reason.

    In systems which use integer math, of which Spin is sterling example, logical TRUE is minus one or all bits on. Get used to it now, and you won't have to figure out why your code did something strange later.

    Dave, in Spin the statement "a := b and c" will coerce b and c to -1 if they are nonzero before doing the bitwise CPU and operation and returning its result. That's interesting about C because Spin is exactly the opposite -- NOT coerces the input to -1 before inverting, and ! does a bitwise one's complement. So one might say Spin is influenced both by Basic and C, but in this regard it hews strongly to BASIC.

    Incidentally, it was a primary goal in the design of the standard floating point libraries that the integer minus one be represented by all bits on, and zero by zero; again, this was so that bitwise logical operators would work properly for logical operations. This means that you can place -1 in a floating point variable, execute a CPU bitwise inversion, and you will get 0. So obviously a lot of thought has gone in this direction.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2011-07-23 16:05
    localroger,

    Spin is definitely not strongly typed! (See: http://en.wikipedia.org/wiki/Strong_typing) All expression evaluations are done with longs, whether you want them to or not. It's not even picky about whether the operands are numbers or strings. It may not perform the operation correctly, of course, but the compiler doesn't enforce typing. The only difference between Spin and other non-strongly-typed languages is the choice of the default type for expression evaluation. And, BTW, you can choose integer evaluation in Perl if you want it: http://perldoc.perl.org/integer.html
    localroger wrote:
    ...Get used to it now, and you won't have to figure out why your code did something strange later.
    I've never had a problem with it, nor do I expect I ever will. The way Spin handles things is highly useful.

    I'm frankly baffled that this discussion continues. When you find yourself in a hole, it's almost always better to stop digging. :)

    -Phil
  • jazzedjazzed Posts: 11,803
    edited 2011-07-23 16:07
    localroger wrote: »
    So one might say Spin is influenced both by Basic and C, but in this regard it hews strongly to BASIC.
    SPIN is just another variant of BASIC with funny syntax.
  • localrogerlocalroger Posts: 3,452
    edited 2011-07-24 10:48
    Well, that Wikipedia entry really leaves languages like Basic and Spin completely out of the discussion. I regard Spin as strongly typed because you must explicitly declare your storage type and provide for the kind of math that will be used; the language does not figure it out for you, and you must specify everything. That is obviously different from a language that requires you to declare variable types but then automatically provides conversions and checks for errors in usage, but I would consider both types to be "opposite" of something like Javascript that doesn't distinguish between strings and numbers.

    And that blows past my main point, which is that Spin doesn't even support array bounds checking, and you're comparing it to Perl? -1 as TRUE is an elegant way to make a generic math expression evaluator handle logical expressions without special cases or operators. It is the only way I would consider developing a system for my own use, and over the years I've developed several. Anything else is a waste of CPU cycles and special-case code.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2011-07-24 12:00
    Who said anything about bounds-checking? My comparison to Perl focused narrowly on the topic at hand. Anyway, our opinions on the topic are unlikely to converge, so we can agree to disagree and let it go at that.

    -Phil
Sign In or Register to comment.