Logic question (handling inequalities in a BASIC compiler)

In summary, the proper output for this inequality is dependent on the programming language it's being used in.
  • #1
elusiveshame
169
35
Hey everyone, I have a logic question, though I'm not sure if this might belong in the math forum (probably not :P)

Anyway, I'm writing a BASIC compiler for the Motorola 68k processor by reverse engineering a dead project/compiler (with permission from the author as he's abandoned it years ago). Anyway, I've into an interesting conundrum. I've gotten to the logic operators, and was wondering what the proper output should be when it comes to inequalities.

The example I'm working with is: a <> 5 <> b

Assume both a and b are both equal to 0. Since both variables don't equal 5, it should return true, right?

The code I was testing with is:
if a <> 5 <> c then print "True"

In the abandoned compiler, this returns false and doesn't print "true" to the screen (actually, even if both variables are different values and not equal to 5, it returns false). I'm going to assume that's not the correct output, and should return true unless both variables don't equal the middle term, right?

Thanks! :)
 
Technology news on Phys.org
  • #2
elusiveshame said:
Hey everyone, I have a logic question, though I'm not sure if this might belong in the math forum (probably not :P)

Anyway, I'm writing a BASIC compiler for the Motorola 68k processor by reverse engineering a dead project/compiler (with permission from the author as he's abandoned it years ago). Anyway, I've into an interesting conundrum. I've gotten to the logic operators, and was wondering what the proper output should be when it comes to inequalities.

The example I'm working with is: a <> 5 <> b

Assume both a and b are both equal to 0. Since both variables don't equal 5, it should return true, right?

The code I was testing with is:
if a <> 5 <> c then print "True"

In the abandoned compiler, this returns false and doesn't print "true" to the screen (actually, even if both variables are different values and not equal to 5, it returns false). I'm going to assume that's not the correct output, and should return true unless both variables don't equal the middle term, right?
I don't think this is as simple as you seem to think. The expression a <> 5 <> b should probably return false, as you say the abandoned compiler does. If the associativity is left to right, this expression would be the same as (a <> 5) <> b. Since a is set to 0, the expression in parentheses is false, so continuing, we're comparing false with 0, and it's been so long since I've done any Basic that I don't know how those two values compare.
 
  • #3
Mark44 said:
I don't think this is as simple as you seem to think. The expression a <> 5 <> b should probably return false, as you say the abandoned compiler does. If the associativity is left to right, this expression would be the same as (a <> 5) <> b. Since a is set to 0, the expression in parentheses is false, so continuing, we're comparing false with 0, and it's been so long since I've done any Basic that I don't know how those two values compare.

I didn't think it was easy, but was hoping it was easy enough for someone here :P

One issue with the abandoned compiler is that it doesn't follow orders of operation unless you use parenthesis, but instead computes left to right, for example (assume b = 2): a = 5 / b + 2 * 3 would result in 9 (5/3 = 1 + 2 * 3 = 3 * 3), instead of it being 7.

I did test some of this in Visual Basic 6:
c = 4
If a <> 4 <> c Then
MsgBox "True"
End If

This will pop a message box when a <> 4, but will when c = 4, or when both a and c are the same value that isn't 4. The abandoned compiler never returns true with anything with the statement above.

I think I figured out what I needed to, though. The issue seems to be when I was using long integer variables, as the compiler is using SCC (set on clear carry) on long variables instead of SNE (set not equal), otherwise it operates the same as Visual Basic (for that expression) when using integers (SNE is used).

The way I was interpreting the expression to return was (a <> 5) AND (5 <> b), but with the VB testing, that's definitely not the case, so I was curious as to which the proper result would be outside of any programming language. The reason why I thought it was that is because if you were to do something like: 0 < x < 5, that expression is only true when x is both greater than 0 and less than 5. Unless I'm forgetting pre-calculus, which is entirely possible :)
 
  • #4
The logic for
Code:
a <> 4 <> c
is
Code:
(a <> 4) <> c

(a <> 4) is of type boolean, which when compared to c will either be 0 or 1.

So your true solutions are:
a <> 4 AND c == 1
a == 4 AND c == 0
All other solutions are false.
 
  • Like
Likes elusiveshame
  • #5
elusiveshame said:
The way I was interpreting the expression to return was (a <> 5) AND (5 <> b), but with the VB testing, that's definitely not the case, so I was curious as to which the proper result would be outside of any programming language. The reason why I thought it was that is because if you were to do something like: 0 < x < 5, that expression is only true when x is both greater than 0 and less than 5. Unless I'm forgetting pre-calculus, which is entirely possible :)
In C and languages derived from it, the expression 0 < x < 5 would always evaluate to true, regardless of the value of x. In these languages, the associativity of the < operator is left to right, so this expression is equivalent to (0 < x) < 5. The expression in parentheses will evaluate to either false (0) or true (1), both of which are less than 5, making the compound expression true.
 
  • Like
Likes elusiveshame
  • #6
newjerseyrunner said:
The logic for
Code:
a <> 4 <> c
is
Code:
(a <> 4) <> c

(a <> 4) is of type boolean, which when compared to c will either be 0 or 1.

So your true solutions are:
a <> 4 AND c == 1
a == 4 AND c == 0
All other solutions are false.

And this is exactly what I was looking for. I asked another forum as well, as it didn't even dawn on me that the result of the first operator will return 0 or 1. I mean, I knew it did, but completely spaced that that was the result being compared.

In VB6, a <> 4 <> true returns false, while a <> 4 <> false returns true.

Thank you to both you, and @Mark44 for your help!
 
  • Like
Likes newjerseyrunner
  • #7
I would advise you to rewrite the expression in simpler form till it is less ambiguous and gives the same predictable result for both compilers. Try (a<> 4) And ( 4 <> c). If that would require a great deal of code modification, I think you should bite the bullet and make all the changes.
 
  • Like
Likes elusiveshame and symbolipoint
  • #8
FactChecker said:
I would advise you to rewrite the expression in simpler form till it is less ambiguous and gives the same predictable result for both compilers. Try (a<> 4) And ( 4 <> c). If that would require a great deal of code modification, I think you should bite the bullet and make all the changes.

I honestly wouldn't write any expression in that manner, just because of the ambiguity of it. I would rather it be a little more code with explicitly defined parameters like your example above.

As for your last sentence... lol the reason I'm asking this question is because of a rewrite of the logic handling. I had added a few things to test for functions and completely broke it, and because it was a "hmm, I'll add that later" thing, it was ill planned. Lesson learned (sort of :P)
 
  • Like
Likes FactChecker
  • #9
I don't think that Basic enforces any kind of type. So if a and/or c contain a non-integer value, results may be erratic. This is one of the issues in using a language which does not require you to explicitly declare the type of variable.
 
  • #10
FactChecker said:
I would advise you to rewrite the expression in simpler form till it is less ambiguous and gives the same predictable result for both compilers. Try (a<> 4) And ( 4 <> c). If that would require a great deal of code modification, I think you should bite the bullet and make all the changes.

elusiveshame said:
I honestly wouldn't write any expression in that manner, just because of the ambiguity of it. I would rather it be a little more code with explicitly defined parameters like your example above.
If by "that manner" you're referring to the expression that FactChecker wrote, it's not at all ambiguous. The one that would be ambiguous would be the one in your first post: a <> 5 <> b, for reasons already stated.
 
  • #11
Mark44 said:
If by "that manner" you're referring to the expression that FactChecker wrote, it's not at all ambiguous. The one that would be ambiguous would be the one in your first post: a <> 5 <> b, for reasons already stated.
The expression that I wrote in my first post :) The way FactChecker wrote it is how I write my expressions.

The reason why I asked about this expression and not something more clear is because while I'm building a new compiler, I'm also trying to make it as compatible to the compiler I'm reverse engineering, so that way when people adapt to it, their existing code still works. If it were up to me, I'd throw an exception error stating that the expression was too ambiguous :P
 
  • #12
I seem to recall that the TRUE logic value was endowed with an integer value of -1 (and FALSE was integer 0) and this way logic statements could be embedded within arithmetic, in the microcomputer implementations I toyed with. [emoji45]

In a logic expression, a variable of value 0 was treated as FALSE and non-zero treated as TRUE.
 
  • Like
Likes jim mcnamara

1. How does a BASIC compiler handle inequalities in logic questions?

A BASIC compiler handles inequalities in logic questions by using conditional statements such as "IF...THEN" or "WHILE...DO" to evaluate the logic and determine if the inequality is true or false. The compiler will then execute the appropriate code based on the result.

2. What types of inequalities can a BASIC compiler handle?

A BASIC compiler can handle various types of inequalities, including numerical comparisons (e.g. <, >, =), string comparisons (e.g. <>, =), and logical comparisons (e.g. AND, OR). It can also handle multiple inequalities in a single logic question.

3. Can a BASIC compiler handle complex logic questions with multiple inequalities and conditions?

Yes, a BASIC compiler is capable of handling complex logic questions with multiple inequalities and conditions. It can use nested conditional statements and logical operators to evaluate the logic and execute the appropriate code based on the result.

4. How does a BASIC compiler handle errors in logic questions with inequalities?

If there is an error in a logic question with inequalities, such as a missing or incorrect operator, the BASIC compiler will typically display an error message and halt the compilation process. This allows the programmer to identify and correct the error before running the program.

5. Are there any limitations to how a BASIC compiler handles inequalities in logic questions?

There may be some limitations to how a BASIC compiler handles inequalities in logic questions, depending on the specific compiler and programming language. For example, some compilers may have restrictions on the types of data that can be compared or the number of nested conditional statements that can be used. It is important for the programmer to consult the compiler's documentation for any limitations or best practices.

Similar threads

  • Programming and Computer Science
Replies
20
Views
2K
  • Programming and Computer Science
Replies
1
Views
3K
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
1
Views
944
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
11
Views
1K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
2
Views
3K
  • Programming and Computer Science
Replies
7
Views
1K
Back
Top