Author Topic: IMProbable Demonstrations of the IMPossible Logic Operator named IMP...  (Read 5885 times)

0 Members and 1 Guest are viewing this topic.

This topic contains a post which is marked as Best Answer. Press here if you would like to see it.

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Hi QB64 community
Hi QB64 guys and gals
It comes back: IMP the logical illogical Operator

Here a four demonstrations to IMPlement  the IMPly operator of logic functions.

How I have learnt from many ways, there is poor use of IMP in the code.
Why? It isn't IMPossible that its logic is a bit cryptic or uncommon.  It is IMPossible to use a linear process of evaluation to have an efficient IMP use. So here it follows a program that let you try and test 4 IMProbable FUNCTIONS that say you if a value is out of a range and  in the range of the 2 value passed, the lowest and the uppest of the give values.
The FUNCTIONS are build with the goal of using IMP to demonstrate how a logical expression can be changed in another using different logic operators.
The table of truth of IMP is this following
A     B      A IMP B    =      (( NOT A)    OR B)
T     T            T                     F             T
F     T            T                     T             T
F     F            T                     T             T
T     F            F                     F             F

in another IMP thread   Luke said IMP is equal to  NOT a  OR B
I thanks all you QB64 coders with your tips, informations, point of views, comments to let me enlarge my knowledge and learn more to code...
here the demo
press the yellow character to activate the correlate FUNCTION, press Escape to quit
the output shows the value tested and their graphic placement on the row 1-80 of SCREEN 0
Code: QB64: [Select]
  1. 'IMP demo in multiple cases
  2. _Title "IMProbable Demonstrations of the IMPossible Logic Operator named IMP..."
  3. 'demo  OutOfRange  InTheRange Lowest Uppest
  4.     n$ = UCase$(InKey$)
  5.     If n$ = Chr$(27) Then End
  6.     If n$ <> "" And InStr("OILU", n$) Then
  7.         Cls
  8.         For a = 1 To 10
  9.             b = Int(Rnd * 40) + 1
  10.             c = Int(Rnd * 30) + 2 + b
  11.             d = Int(Rnd * 79) + 1
  12.             Locate (a - 1) * 2 + 1, 1
  13.             Print b; "  "; c; "  "; d; "  ";
  14.             If n$ = "O" Then If outheRange(b, c, d) Then Print "out of range "; ' Else Print "in the range ";
  15.             If n$ = "I" Then If intheRange(b, c, d) = -1 Then Print "In the range<---";
  16.             If n$ = "L" Then If Lowest(b, c, d) Then Print "lowest  is "; d
  17.             If n$ = "U" Then If Uppest(b, c, d) Then Print "Uppest  is "; d
  18.             ' graphic rappresentation
  19.             Color 2, 0: Locate (a - 1) * 2 + 2, b: Print ">";: Locate , c: Print "<";: Color 14, 0: Locate , d: Print "O";: Color 7, 0
  20.         Next a
  21.     End If
  22.     Locate 23, 1: Print " Outherange Intherange Lowest Uppest, Escape to quit"
  23.     Color 14, 0: Locate 23, 2: Print "O";: Locate 23, 13: Print "I";: Locate 23, 24: Print "L";: Locate 23, 31: Print "U";: Color 7, 0
  24.     _Limit 10
  25.  
  26.  
  27. ' SUB and FUNCTION area  / area delle SUB e delle FUNCTION
  28. Function outheRange (min As Integer, max As Integer, value As Integer)
  29.     outheRange = 0
  30.     outheRange = (min <= value Imp value >= max)
  31.  
  32. Function intheRange (min As Integer, max As Integer, value As Integer)
  33.     intheRange = 0
  34.     intheRange = (value <= max) Imp (value <= min)
  35.     intheRange = Not intheRange
  36.  
  37. Function Lowest (a1%, a2%, T%)
  38.     'NOT a OR b
  39.     '3 8 1 -> F imp F = T
  40.     '3 8 4 -> T imp F = F  no lowest
  41.     '5 3 2 -> F imp T = T
  42.     '6 4 9 -> T imp T = T  --> bug that it solves ordering a1% vs a2%
  43.     If a1% > a2% Then Swap a1%, a2%
  44.     Lowest = a1% < T% Imp a1% > a2%
  45.  
  46. Function Uppest (a1%, a2%, T%)
  47.     'NOT a OR b
  48.     '3 8 9 -> F imp T = T
  49.     '3 2 6 -> F imp F = T
  50.     '5 6 2 -> T imp T = T  -->bug  that it solves ordering a1% vs a2%
  51.     '6 4 2 -> T imp F = F no uppest
  52.     If a1% < a2% Then Swap a1%, a2%
  53.     Uppest = a1% > T% Imp a1% < a2%
  54.  
Thanks to try, welcome feedbacks



Programming isn't difficult, only it's  consuming time and coffee

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
TempodiBasic function:
Code: QB64: [Select]
  1. Function Lowest (a1%, a2%, T%)
  2.     'NOT a OR b
  3.     '3 8 1 -> F imp F = T
  4.     '3 8 4 -> T imp F = F  no lowest
  5.     '5 3 2 -> F imp T = T
  6.     '6 4 9 -> T imp T = T  --> bug that it solves ordering a1% vs a2%
  7.     If a1% > a2% Then Swap a1%, a2%
  8.     Lowest = a1% < T% Imp a1% > a2%
  9.  

Bad idea to swap a1% and a2% specially when there is no need:
If a1% < a2% then lowest% = a1% else lowest% = a2%
« Last Edit: July 26, 2021, 08:04:26 am by bplus »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
@TempodiBasic

I tested your intheRange function and found inconsistency:
Code: QB64: [Select]
  1. 'IMP demo in multiple cases
  2. _Title "IMProbable Demonstrations of the IMPossible Logic Operator named IMP..."
  3. 'demo  OutOfRange  InTheRange Lowest Uppest
  4. Dim As Integer rLow, rHigh, test
  5. rLow = -5: rHigh = 5
  6.     Input "Enter number to test in range -5 to 5 (use 0 (in Range) to quit) "; test
  7.     If intheRange(rLow, rHigh, test) Then Print "In the range -5 to 5." Else Print "Not in the range -5 to 5."
  8. Loop Until test = 0
  9.  
  10. ' SUB and FUNCTION area  / area delle SUB e delle FUNCTION
  11. Function outheRange (min As Integer, max As Integer, value As Integer)
  12.     outheRange = 0
  13.     outheRange = (min <= value Imp value >= max)
  14.  
  15. Function intheRange (min As Integer, max As Integer, value As Integer)
  16.     intheRange = 0
  17.     intheRange = (value <= max) Imp (value <= min)
  18.     intheRange = Not intheRange
  19.  
  20. Function Lowest (a1%, a2%, T%)
  21.     'NOT a OR b
  22.     '3 8 1 -> F imp F = T
  23.     '3 8 4 -> T imp F = F  no lowest
  24.     '5 3 2 -> F imp T = T
  25.     '6 4 9 -> T imp T = T  --> bug that it solves ordering a1% vs a2%
  26.     If a1% > a2% Then Swap a1%, a2%
  27.     Lowest = a1% < T% Imp a1% > a2%
  28.  
  29. Function Uppest (a1%, a2%, T%)
  30.     'NOT a OR b
  31.     '3 8 9 -> F imp T = T
  32.     '3 2 6 -> F imp F = T
  33.     '5 6 2 -> T imp T = T  -->bug  that it solves ordering a1% vs a2%
  34.     '6 4 2 -> T imp F = F no uppest
  35.     If a1% < a2% Then Swap a1%, a2%
  36.     Uppest = a1% > T% Imp a1% < a2%
  37.  
  38.  
  39.  

If 5 is in the range then -5 should be also.

-5 is wrong.PNG
* -5 is wrong.PNG (Filesize: 37.85 KB, Dimensions: 797x638, Views: 203)
« Last Edit: July 26, 2021, 08:18:39 am by bplus »

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Hi Bplus
Thanks for you feedbacks!

1.
about bad swap in lowest...
Code: QB64: [Select]
  1. 'NOT a OR b
  2.     '3 8 1 -> F imp F = T
  3.     '3 8 4 -> T imp F = F  no lowest
  4.     '5 3 2 -> F imp T = T
  5.     '6 4 9 -> T imp T = T  --> bug that it solves ordering a1% vs a2%
  6.     IF a1% > a2% THEN SWAP a1%, a2%
  7.     Lowest = a1% < T% IMP a1% > a2%

I want to stress the Illogical Logical table of IMP with this comment left in the function
Quote
'6 4 9 -> T imp T = T  --> bug that it solves ordering a1% vs a2%
in this example test, the function returns a wrong result returning True (as IMP table shows)
So I agree with you that it is LOGICAL that if T > a1  AND a1>a2 why should we swap these last two?
But wait a moment  the expression is T>a1 IMP a1>a2 ! That is the point.
The swap brings the evaluation to the 2 first rows where the function Lowest returns the right value True/False.

And dear friend I agree with you that it is not so elegant as a single row of evaluation statement but it works.
PS: now you get why this is an IMProbable Demonstrations of the IMPossible Logic Operator named IMP  ;)


2.
using negative numbers in the range...
I must admit that this point of view is interesting and I must declare that originally the function has been built thinking to a position in the Right Upper sector of a Cartesian plan with the two dimensions X and Y from 0 to + infinite.
Maybe that this choice has been made because it is naturally thinking of a position as something that exists. But using a different cohordinates system a negative value is possible.
Well I'll use your feedback to expand (if it will be possible) the functions to the whole gamma of cohordinates of Cartesian bidimensional plan.

Thanks again
Programming isn't difficult, only it's  consuming time and coffee

Offline Dimster

  • Forum Resident
  • Posts: 500
    • View Profile
I love this topic Tempodi .

The statement " intheRange = (value <= max) Imp (value <= min) "  ...which shows up as line 38 for me....How does the ... IMP (value <= min) .....actually work correctly. The implied should be the value can be equal to min but not a value less than min. A value less than min should not be intheRange. If our range is  10 to 20 and the value is 9, that 9 would be less than the min of 10. ??? so what am I missing in this Logical Operator???

Thanks very much for tackling it. I have had no confidence in the results I'm getting but it seems like it should be a useful tool with logic applications.

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Hi DImster
thanks for feedback and ideas to talk about.

Quote
I love this topic Tempodi
My pleasure to get the interest of QB64 coders.


Quote
The implied should be the value can be equal to min but not a value less than min. A value less than min should not be intheRange. If our range is  10 to 20 and the value is 9, that 9 would be less than the min of 10. ??? so what am I missing in this Logical Operator???

At the starting time, also I was thinking about IMP as the word imply but this is wrong and it brings us in the wrong direction. We use in human language this word with the meaning IF...THEN... while IMP seems to be a mathematical  imply that formally has as name Material Implication or Material Conditional.
read here if you want to get more deep informations  https://en.wikipedia.org/wiki/Material_conditional
So it seems that we must think about IMP as we think about + of the sum with its feature. It is an operator that gives False only if the first condition is True and the second condition is False. This is the path to set the right conditions in the two branches of IMP and to get back the desired result.

Well the function has the goal to return True (-1/ NOT 0) if the third parameter is between the first and the second parameters.
let's test the expression   
Code: QB64: [Select]
  1.  intheRange = 0  '<---- this is the default result (False/ No)
  2.     intheRange = (value <= max) IMP (value <= min)   '<-- this is the expression that using IMP answer to the question " Is value between min and max?
  3.     intheRange = NOT intheRange' <-- really the above expression calculates the OutOfRange so we must NOT the result

Min  Max  Value    (value <= max) IMP (value <= min)            (NOT    (value<=max))  OR  (value <=min)
 10   20      9        (9<=20)   IMP  (9<=10)                              (NOT     (9<=20))        OR    (9<= 10)
                               True   IMP    True   -------> True                (NOT   True)       OR  (True)  ------> True
 10  20     21         21 <= 20  IMP  21<= 10                               NOT (21<=20)   OR (21<= 10)
                                  False  IMP   False  ---> True                     NOT False OR False ------> True
 10  20     11         11<= 20  IMP 11<=10                                NOT(11<=20)  OR (11<=10)
                                  True  IMP    False ---> False                     NOT True OR  False  -----> False

changing the > with < and viceversa we get the one shot InTheRange
Min  Max  Value    (value >= max) IMP (value >= min)            (NOT    (value>=max))  OR  (value >=min)
 10   20      9        (9>=20)   IMP  (9>=10)                              (NOT     (9>=20))        OR    (9>= 10)
                               False   IMP    False   -------> True                (NOT   False)       OR  (False)  ------> True
 10  20     21         21 >= 20  IMP  21>= 10                               NOT (21>=20)   OR (21>= 10)
                                  True  IMP   True  ---> True                     NOT True OR True ------> True
 10  20     11         11>= 20  IMP 11>=10                                NOT(11>=20)  OR (11>=10)
                                False    IMP   True ---> True                     NOT False OR  True  -----> True
So the code of the FUNCTION InTheRange will be
Code: QB64: [Select]
  1.  intheRange = 0  '<---- this is the default result (False/ No)
  2.     intheRange = (value >= max) IMP (value >= min)   '<-- this is the expression that using IMP answer to the question " Is value between min and max?

And with this last sentence I feel to have finished to share my new knowledge about IMP.

PS Thank you, I have got a new InTheRange in oneshot!
Now I'll try to give an universal wellworking also with negative numbers. Next time.
Programming isn't difficult, only it's  consuming time and coffee

Offline Dimster

  • Forum Resident
  • Posts: 500
    • View Profile
Quote
At the starting time, also I was thinking about IMP as the word imply but this is wrong and it brings us in the wrong direction. We use in human language this word with the meaning IF...THEN... while IMP seems to be a mathematical  imply that formally has as name Material Implication or Material Conditional.

All the other logical operators (And, Or, Xor, Eqv) seem to be Basic logic.

IMP is false if its first operand is true and the second is false. Otherwise IMP is true.. My parents always said 2 wrongs do not make a right but it would appear with this Material Implicator , 2 wrongs do imply a right ( if First Operand is False and second operand is False then result is True). Two wrongs making a right.

Thanks again, I'll smoke a little more marijuana and a bottle of whiskey and I know I'll understand it perfectly.

Marked as best answer by TempodiBasic on July 28, 2021, 01:17:26 am

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
IMP is a NOT A OR B evaluation.

If you’re NOT sick, OR if you’re a woman, then you get a discount on health insurance, which breaks down to:

SICK, WOMAN = DISCOUNT
YES, YES = TRUE (you’re a woman)
NO, YES = TRUE (you’re a woman)
NO, NO = FALSE (you’re sick and not a woman)
YES, NO = TRUE (you’re not sick)

SICK IMP WOMAN = DISCOUNT
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Hi Qb64 boys and Girls

I come back on topic...

@SMcNeill  Great Steve, an example enlighting and very practical!

@Dimster
Yes it is not under common sense that two lies brings to a truth! But also in common speaking IMP is there:
1 -You are a good guy  = You aren't a bad guy   
2 - this sentence isn't false = this sentence is true
3 - the shirt is clean = the shirt is not dirty
As human we use a not strictly binary system of values, so until the reference system is with only two choices we feel IMP  as normal and right (see sentence 2), while we use a reference system with more than two options we feel that IMP is not so accurate (see sentence 1 and 3) because we cut out a choice but there are more than one alternative.
you aren't a bad guy = you are normal guy  or you are a good guy or....what other meaning you can add?

@bplus
Thanks for feedback... I was searching far far away the solution! But with your test you have already suggested  me also the solution!
Yes deleting an = the function gets accuracy again
Here the code with testing:
Code: QB64: [Select]
  1. 'IMP demo in multiple cases
  2. _Title "IMProbable Demonstrations of the IMPossible Logic Operator named IMP..."
  3. 'demo  OutOfRange  InTheRange Lowest Uppest
  4. Dim As Integer rLow, rHigh, test
  5. rLow = -5: rHigh = 5
  6.     Input "Enter number to test in range -5 to 5 (use 100 (in Range) to quit) "; test
  7.     If intheRange(rLow, rHigh, test) Then Print "In the range -5 to 5." Else Print "Not in the range -5 to 5."
  8. Loop Until test = 100
  9.  
  10. ' SUB and FUNCTION area  / area delle SUB e delle FUNCTION
  11. Function outheRange (min As Integer, max As Integer, value As Integer)
  12.     outheRange = 0
  13.     outheRange = (min <= value Imp value >= max)
  14.  
  15. Function intheRange (min As Integer, max As Integer, value As Integer)
  16.     intheRange = 0
  17.     intheRange = (value <= max) Imp (value < min)
  18.     intheRange = Not intheRange
  19.  
  20. Function Lowest (a1%, a2%, T%)
  21.     'NOT a OR b
  22.     '3 8 1 -> F imp F = T
  23.     '3 8 4 -> T imp F = F  no lowest
  24.     '5 3 2 -> F imp T = T
  25.     '6 4 9 -> T imp T = T  --> bug that it solves ordering a1% vs a2%
  26.     If a1% > a2% Then Swap a1%, a2%
  27.     Lowest = a1% < T% Imp a1% > a2%
  28.  
  29. Function Uppest (a1%, a2%, T%)
  30.     'NOT a OR b
  31.     '3 8 9 -> F imp T = T
  32.     '3 2 6 -> F imp F = T
  33.     '5 6 2 -> T imp T = T  -->bug  that it solves ordering a1% vs a2%
  34.     '6 4 2 -> T imp F = F no uppest
  35.     If a1% < a2% Then Swap a1%, a2%
  36.     Uppest = a1% > T% Imp a1% < a2%
  37.  

@TempodiBasic
please don't run to make mistakes
this code is buggy for now no oneshot InTheRange !
Quote
intheRange = 0  '<---- this is the default result (False/ No)
    intheRange = (value >= max) IMP (value >= min)   '<-- this is the expression that using IMP answer to the question " Is value between min and max?



Programming isn't difficult, only it's  consuming time and coffee

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
@TempodiBasic

I think I will take a range check on IMP.

Offline Dimster

  • Forum Resident
  • Posts: 500
    • View Profile
So if I'm understanding this correctly. Tempodi's assertion that we can't interpret IMP as meaning Imply in the traditional sense, and applying Steve's example:

The convention of A IMP B, where A = Sick and B =Woman can be rewritten as     ((Sick)*-1) OR Woman = Discount.
The convention therefore should be thought more as (A*-1) or B IMP  D

In effect the Left side of the IMP statement sets up exclusionary value(s) and the Right side of the IMP statement provides more exclusionary values to consider. They are really not implying each other but rather building up exclusionary values. And test is to see if D falls within those exclusionary values. 

The way the convention of A IMP B is written had me thinking the A in fact did imply B and as a result we came up with D. So Sick IMPLIED Woman and therefore it effects the Discount. In my mind I could see a Health Plan only for Women and if a woman was sick when applying for the plan then if effected the Discount but Steve's example makes so much more sense because it would include both males and females.

Lord, don't you love the logic of people v's machines?


Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
I think AND and OR and NOT work in conventional sense but IMP is purely for bit math users.

Implies, logically to me, usually means if A is element or subset of B then A has all the characteristics that identify an element or subset as B. eg if A is a man then A is a human, if C is a women then C is a human.



Offline Dimster

  • Forum Resident
  • Posts: 500
    • View Profile
So b+ , the A IMP B are not providing a different set of values but the B would be a value(s) held within the A ? So the A could be a range and the B a specific value? Or even vice versa, the A carrying a specific value and the B the range containing the A? That concept would bring back that idea that IMP does mean Imply. If the range is 10 to 20 then it certainly does imply 12 but not 9.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Yeah I think Basic's IMP exists only to round out a set of bit manipulations you can do with 0's and 1's or True and False values there may be some esoteric need for it. You certainly don't need it for range determination.

IF x > high_range OR x < low_range THEN PRINT x ;" is out of range"

What could be simpler to understand for range?

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
IMP is a NOT A OR B evaluation.

If you’re NOT sick, OR if you’re a woman, then you get a discount on health insurance, which breaks down to:

SICK, WOMAN = DISCOUNT
YES, YES = TRUE (you’re a woman)
NO, YES = TRUE (you’re a woman)
NO, NO = FALSE (you’re sick and not a woman)
YES, NO = TRUE (you’re not sick)

SICK IMP WOMAN = DISCOUNT

Actually, I wrote that wrong.  :P

SICK, WOMAN = DISCOUNT
YES, YES = TRUE (you’re a woman)
NO, YES = TRUE (you’re a woman)
NO, NO = TRUE (you’re not sick)
YES, NO = FALSE (you’re sick and not a woman)

See if that doesn’t look a little more like the logic chart Tempodi shared:

The table of truth of IMP is this following
A     B      A IMP B
T     T            T             
F     T            T                   
F     F            T                 
T     F            F             

Why I reversed my bottom two lines, I have no clue, but when I looked back over them, they were swapped.  :P

IMP is one of those operators which I’ve never personally needed.  Honestly, I’d rather write this:

DISCOUNT = (NOT SICK) OR WOMAN

than:

DISCOUNT = SICK IMP WOMAN

They’re the same thing, and IMP might save a few microprocesses, but I find the first much more readable and able to be understood for my personal code and use.

https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!