Tuesday, March 17
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: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:
print "A = B".
else:
print "A <> B".
end.
if a=b then:Prints A <> B, which is true from a two-valued perspective. But this:
print "A = B".
else:
print "A <> B".
end.
if a<>b then:Prints A = B, which is not true from a two-valued perspective. What's going on?
print "A <> B".
else:
print "A = B".
end.
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: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.
print "A = B".
else:
print "A <> B".
otherwise:
print "You have a null value somewhere".
end.
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.
Posted by: Stephen Macklin at Tuesday, March 17 2009 09:05 PM (R7LgM)
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)
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)
Posted by: Steven Den Beste at Wednesday, March 18 2009 08:49 AM (+rSRq)
Posted by: Pixy Misa at Wednesday, March 18 2009 01:15 PM (PiXy!)
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!)
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)
Posted by: Pixy Misa at Thursday, March 19 2009 02:16 AM (PiXy!)
56 queries taking 0.1182 seconds, 353 records returned.
Powered by Minx 1.1.6c-pink.