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

0 Members and 1 Guest are viewing this topic.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Another way to break down the logic of IMP is to remember with A IMP B:

Your result is *always* going to have all the bits of B set…

For example, let’s assume A and B are both _UNSIGNED _BYTEs.

Now if B =3, the result of A IMP B will be = ??????11, depending on A to fill in the ?
And if B = 5, the result of A IMP B will be = ?????1?1, depending on A to fill in the ?

*Whatever* the final result is, it’s going to have every bit set that B already has set.



And, with that half of the process solved, it’s *also* going to set any bits that A *DOES NOT* have set.

A = 2.  B = 3.

In binary, those are:
A = 00000010
B = 00000011

A IMP B is solved by first setting all the bits in the answer to match B:   ??????11
Then we toggle all the bits in A: 11111101.
And we set the ones that are on, for our answer: 11111111

2 IMP 3 = 255



(NOT A) OR B

That’s the breakdown of what IMP is doing.

(NOT A) says the result is going to have all the bits set that A does NOT have.
OR B says our result is *also* going to have all the bits set that B does.

A IMP B = (NOT A) OR B

Really, that’s all there is to it.  It’s convoluted, and not really something I think most folks ever really need, but that’s all the does in a nutshell.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Last news on IMP
1. It is supported until VB6 but it was dismissed in VB.NET  for the equivalence to NOT(A) OR B !
2. It is useful if you must make a choice in which the determinant element is the second... in fact IMP gives False only if the second element is False and the first element is True....
here an example in VB6 to make a quick choice in a  cascade IF (use Google for get the text but code seems readable)
https://masterdrive.it/visual-basic-6-17/lha-visto-loperatore-logico-imp-41456/

In sum IMP is there and can be used! Let think about it like it would be the equivalent of  SQR or LOG operation versus +-*/. It is not so times used but it can be more useful in specific setting or with specific research to use it.
Code: QB64: [Select]
  1. IMPuse IMP IMPoperator
Programming isn't difficult, only it's  consuming time and coffee

Offline Dimster

  • Forum Resident
  • Posts: 500
    • View Profile
What an education this tread has been for me. And I have to say that I feel for Fabel Schoolboy. He shared his discovery on IMP but seems the criticism was taken so personally. I guess this is one of the things I like about the comments many of the coders on this site take. Even though we may use the words "I, You, me etc" those words which are a personal reference to yourself, invariably should trigger a personal reply and that's when we run into those scenarios where we have to defend ourselves, sometimes heatedly. But you guys, for the most part, keep your responses directed to the code and not the person. Your criticism of the code is much more helpful from a learning perspective, and I for one appreciate that very much.

I don't think the logical operator IMP has ever had as strong a light shined on it. Maybe this tread could be referenced in a Best Practice or Learning Section. You guys rock.

Offline OldMoses

  • Seasoned Forum Regular
  • Posts: 469
    • View Profile
I can't say I've gotten any ideas on how/where/when to use IMP, as NOT A OR B seems more intuitive, but I did rather enjoy Steve's sick woman example...;)

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
I can't say I've gotten any ideas on how/where/when to use IMP, as NOT A OR B seems more intuitive, but I did rather enjoy Steve's sick woman example...;)

Honestly, I doubt I’d even do (NOT A) OR B  very often.  I’m much more comfortable, and likely to use, something like:

Healthy = NOT Sick
Discount = Healthy OR Woman

Even though the above is two lines, *anyone* should be able to just glance at those and understand what they’re doing and what the intended result of them should be, quite easily.

Whereas you’re going to need a comment or two, or a very experienced programmer, to instantly know the IMP method:

Discount = Sick IMP Woman



The *only* time I’d be likely to worry with IMP, is if my boss said, “We’re having a competition for a $1,000 bonus this Xmas, and it’ll go to whoever writes a program to calculate discounts the fastest!”  In this very specific case, saving that singular math operation for speed over readability would probably be necessary.

Any other time though?  Readability and ease of maintenance for the source code, trumps saving one math process, all the rest of the time.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: IMProbable Demonstrations of the IMPossible Logic Operator named IMP...
« Reply #20 on: August 03, 2021, 07:29:01 am »
Hi QB64 boys and girls
here the final cut of my little IMPossible IMPortant library with 4 general purpouse functions:

-OutheRange: it returns if a value is out of a range
(Thanks to Bplus for feedback and showing a point of weakness of previous formula)

-IntheRange: it returns if a value is in a range

-Lower: it returns if the third value is the lowest among 3 passed

-Uppest: it returns if the third values is the uppest among 3 passed

They works also with negative numbers.
They can be easily modified to work with different kinds of numer data redifining the type of parameters and of the function.

Code: QB64: [Select]
  1. 'IMP demo in multiple cases
  2. _Title "IMProbable Demonstrations of the IMPossible Logic Operator named IMP..."
  3.  
  4. Dim As Integer rLow, rHigh, test
  5. rLow = -5: rHigh = 5
  6. Print "Min   Max   Value   Intherange OutheRange Lowest Uppest"
  7. For test = -6 To 6
  8.     Print rLow; "   "; rHigh; "    "; test; "     "; intheRange(rLow, rHigh, test); "        "; outheRange(rLow, rHigh, test); "        "; Lowest(rLow, rHigh, test); "       "; Uppest(rLow, rHigh, test)
  9.     Swap rLow, rHigh
  10.     ' ------- output debugging STEP BY STEP -------------
  11.     'Print "TEST ="; test
  12.     'Print "Min "; rLow; "  Value "; test; "  Max "; rHigh
  13.     'Print "In the range -5 to 5.";: Print intheRange(rLow, rHigh, test)
  14.     'Print "Not in the range -5 to 5.";: Print outheRange(rLow, rHigh, test)
  15.     'Print "It is the lowest";: Print Lowest(rLow, rHigh, test)
  16.     'Print "It is the uppest";: Print Uppest(rLow, rHigh, test)
  17.     'Print " Press a key to continue..."
  18.     'Sleep: Cls
  19.     '-----------------------------------------------------
  20. '------------------------------------------------------------------------
  21. ' SUB and FUNCTION area  / area delle SUB e delle FUNCTION
  22. Function outheRange (Min As Integer, Max As Integer, Value As Integer)
  23.     '3 8 1 -> F imp F = T
  24.     '3 8 4 -> T imp F = F  no OUTHERANGE
  25.     '3 8 9 -> T imp T = T
  26.     '6 4 9 -> T imp T = T  --> bug that it solves ordering a1% vs a2%
  27.     If Min > Max Then Swap Min, Max
  28.     outheRange = 0
  29.     outheRange = (Min <= Value) Imp (Value > Max)
  30.  
  31. Function intheRange (Min As Integer, Max As Integer, Value As Integer)
  32.     '3 8 1 -> F imp F = T -->NOT()--> F
  33.     '3 8 4 -> T imp F = F -->NOT() -->T   INTHERANGE
  34.     '3 8 9 -> T imp T = T -->NOT() --> F
  35.     If Min > Max Then Swap Min, Max
  36.     intheRange = 0
  37.     intheRange = Not ((Min <= Value) Imp (Value > Max))
  38.  
  39. Function Lowest (a1%, a2%, T%)
  40.     'NOT a OR b
  41.     '3 8 1 -> F imp F = T
  42.     '3 8 4 -> T imp F = F  no lowest
  43.     '5 3 2 -> F imp T = T
  44.     '6 4 9 -> T imp T = T  --> bug that it solves ordering a1% vs a2%
  45.     If a1% > a2% Then Swap a1%, a2%
  46.     Lowest = a1% < T% Imp a1% > a2%
  47.  
  48. Function Uppest (a1%, a2%, T%)
  49.     'NOT a OR b
  50.     '3 8 9 -> F imp T = T
  51.     '3 2 6 -> F imp F = T
  52.     '5 6 2 -> T imp T = T  -->bug  that it solves ordering a1% vs a2%
  53.     '6 4 2 -> T imp F = F no uppest
  54.     If a1% < a2% Then Swap a1%, a2%
  55.     Uppest = a1% > T% Imp a1% < a2%
  56.  

So you can IMProve your experience of IMP with these blackbox functions.

PS don't read this thread if you are not interesting to IMP, why do you waste your time?
Programming isn't difficult, only it's  consuming time and coffee