Tuesday, March 17

Geek

Truth Or Consequences

Two-valued (Boolean) logic is easy.  We recognise the basics of it from our everyday lives, and even if we can't immediately grasp some complex logical expression, we can work up to it from basic principles.

But when you start introducing more values, things get weird.

Take the simple if - then - else statement:
if a=b then:
  print "A = B".
else:
  print "A <> B".
end.
With two-valued logic, it couldn't be simpler.  If the expression is true, then you do the first bit, else you do the second bit.  But introduce null values, and hence logical nulls as a result of logical comparisons, and it starts to go pear-shaped.  If A is 1 and B is null, we find that this:
if a=b then:
  print "A = B".
else:
  print "A <> B".
end.
Prints A <> B, which is true from a two-valued perspective.  But this:
if a<>b then:
  print "A <> B".
else:
  print "A = B".
end.
Prints A = B, which is not true from a two-valued perspective.  What's going on?

What's going on is that the answer to the question, does A equal B, is neither true nor false.  It's null.  And so is the answer to the question, does A not equal B.  Since the then clause is taken if the if is true, either way, we take the else clause instead.

The problem is that if - then - else is implicitly two-valued.  What we need is a three-valued branch:
if A = B then:
  print "A = B".
else:
  print "A <> B".
otherwise:
  print "You have a null value somewhere".
end.
Yes, an if - then - else - otherwise structure will make any moderately experienced programmer choke on his cornflakes, but it's the logical consequence of the database null.

Well, that's ternary logic dealt with.  But what about yesterday's topic, quaternary logic?  Now we have four states: true, false, any, and none.  How should our if - then - else behave now?

Well, we can clarify that by looking at yet another variation on the names for the quaternary values: true, false, both and neither.  So the clear way to handle neither is not to take either of the branches - neither then nor else.

And the way to handle both is, well, to take both.

I can hear the anguished screams from here.

Posted by: Pixy Misa at 07:35 PM | Comments (8) | Add Comment | Trackbacks (Suck)
Post contains 391 words, total size 3 kb.

1 The great thing about not being programmer is that I can read this post and understand it (which is not always the case here!) and not scream.

Posted by: Stephen Macklin at Tuesday, March 17 2009 09:05 PM (R7LgM)

2 Hmmm...
Given the content of the last three posts, along with this one, may one assume you're preparing to roll your own index function?

Posted by: Old Grouch at Wednesday, March 18 2009 12:17 AM (KyCzn)

3

A problem a lot of programmers ran into really early was that translations of decimal operations into binary didn't necessarily yield exact answers, when dealing with floating point division and many more complex operations. sqrt(2)*sqrt(2) != 2, though it really should be. Likewise (1/3)*3 != 1. In both cases the difference is tiny, and due to binary round-off.

The solution on one programming language I was involved in was a fudge factor they called "fuzz". If two numbers were within "fuzz" of each other, they were treated as equal even though the binary representations were not precisely the same.

Recently I noticed that ActionScript 2.0 includes both a "==" comparison operator and a "===" comparison operator. == means "fuzzy equality" and === means "must be precisely the same".

Posted by: Steven Den Beste at Wednesday, March 18 2009 03:22 AM (+rSRq)

4 By the way, Pixy, Ace needs your help.

Posted by: Steven Den Beste at Wednesday, March 18 2009 08:49 AM (+rSRq)

5 An outbreak of lesbian hentai?  I'll be right there.  I mean, to delete the spam.

Posted by: Pixy Misa at Wednesday, March 18 2009 01:15 PM (PiXy!)

6 Old Grouch - I'm preparing a scripting language for the next-but-one release of Minx.  I've already implemented the core parser and runtime in the upcoming release.

Back in 2002 I spent a bit of time designing my own programming language, so I thought it would be worth my time to dig out my notes from that project and apply it now that I'm implementing a programming language.  Some of the concepts I was working with were a trifle oblique to the needs of the average web page script twiddler, though, like quaternary logic and multivalued variables.

Posted by: Pixy Misa at Wednesday, March 18 2009 01:20 PM (PiXy!)

7 BTW (and OT), looks like some BBCode is leaking into the stylesheets:
Warning: Expected declaration but found '(opening bracket)'. Skipped to next declaration.
Source file: http://oldgrouch.mee.nu/css/site.css
Line: 254

Here's the source (open/close brackets changed to left/right parens to be sure they display):
.sidebar {
(if style.block.sidebar.color.background)background-color: (style.block.sidebar.color.background);(/if)
padding: 15px 5px 15px 25px;
font-size: small;
font-family: Verdana, Arial, sans-serif;
overflow: hidden;
}

Posted by: Old Grouch at Thursday, March 19 2009 12:50 AM (7qwzl)

8 Thanks, I'll get that sorted out.  Changing the if to an iff should do it.

Posted by: Pixy Misa at Thursday, March 19 2009 02:16 AM (PiXy!)

Hide Comments | Add Comment

Post is locked.
43kb generated in CPU 0.03, elapsed 0.0438 seconds.
51 queries taking 0.0223 seconds, 222 records returned.
Powered by Minx 1.1.6c-pink.