Author Topic: Implied  (Read 6882 times)

0 Members and 1 Guest are viewing this topic.

Offline Dimster

  • Forum Resident
  • Posts: 500
    • View Profile
Implied
« on: August 23, 2020, 11:16:57 pm »
Has anyone worked with the relational operator "IMP"? The wiki doesn't give an example of it in a code. Wondering what the limits are to Implications in formulas. For example will it take a resultant value and imply a color ( a<= 13 IMP color Red) or is there a way for it to take a string value (Blue$ IMP Boys$ AND Pink$ IMP Girls$)??

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Implied
« Reply #1 on: August 23, 2020, 11:58:56 pm »
From the help page in the IDE:

Quote
The IMP logical operator converts the result of two comparative values and returns a bit result.

Syntax:
     result = firstValue IMP secondValue

Description:
  • Returns a different result from AND, OR or XOR.
  • Evaluates if firstValue implies secondValue.
  • If firstValue is true then secondValue must also be true.
  • So if firstValue is true, and secondValue false, then the condition is false, otherwise it is true.
I don't know what it is saying. "Different result". How is it different? I have no clue what they are IMPlying.
Here is a Microsoft Doc on the IMP operator. Still confused.
https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/imp-operator
« Last Edit: August 24, 2020, 12:17:02 am by SpriggsySpriggs »
Shuwatch!

Offline luke

  • Administrator
  • Seasoned Forum Regular
  • Posts: 324
    • View Profile
Re: Implied
« Reply #2 on: August 24, 2020, 02:04:15 am »
It's just another bitwise logical operator like AND, OR or XOR.

A truth table for all the logical operators is given on http://www.qb64.org/wiki/Boolean

IMP corrrsponds to the formal logic concept of implication, but if you're not familiar with that just stick with the truth table.

I've never found a use for it.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Implied
« Reply #3 on: August 24, 2020, 02:18:01 am »
Yeah it's for bit manipulations:
Code: QB64: [Select]
  1. _TITLE "IMP versus AND OR XOR" 'b+ 2020-08-24
  2. ' ref: http://qb64.org/wiki/IMP
  3. SCREEN _NEWIMAGE(900, 120, 32)
  4. _DELAY .25
  5. PRINT " A", " B", "NOT B", "A AND B", "A or B", "A XOR B", "A EQV B", "A IMP B"
  6. A = -1: B = -1: PRINT A, B, NOT B, A AND B, A OR B, A XOR B, A EQV B, A IMP B
  7. A = -1: B = 0: PRINT A, B, NOT B, A AND B, A OR B, A XOR B, A EQV B, A IMP B
  8. A = 0: B = -1: PRINT A, B, NOT B, A AND B, A OR B, A XOR B, A EQV B, A IMP B
  9. A = 0: B = 0: PRINT A, B, NOT B, A AND B, A OR B, A XOR B, A EQV B, A IMP B
  10.  

  [ You are not allowed to view this attachment ]  

The one application I can think of is wiring / coding connections with x amount of pins in your robot or printer but that may have changed since USB.
« Last Edit: August 24, 2020, 02:21:48 am by bplus »

Offline Dimster

  • Forum Resident
  • Posts: 500
    • View Profile
Re: Implied
« Reply #4 on: August 24, 2020, 09:30:28 am »
Thanks. I appreciate the comments of this unloved relational operator.

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Implied
« Reply #5 on: August 24, 2020, 12:57:07 pm »
Thanks for the lesson to Dimster, luke, SpriggysSpriggs and Bplus!

So IMP implies and it works giving TRUE if the second item is TRUE or if it is Equal to the first!
You can get TRUE using IMP if the 2 items are the same or if you pass from FALSE to TRUE but not vice versa.

Thanks to Bplus for the immediate comparative table:
 so we can see that  XOR and EQV are complementar,
NOT is selfcomplementary
while AND, OR and IMP have no complementar operator.
The complementar operator is that can revert, bringing back to the original state, the result tha has been got using the initial operator.

Ah logic, you conquer our brains!

@Dimster
about
Quote
I appreciate the comments of this unloved relational operator
another not so used operator is XOR, but often we use OR at the place of XOR with no problem because we use it (OR) evaluating conditions that are mutual self-excludent (conditions that if the first  is TRUE the second cannot be TRUE and so we avoid the answer TRUE if all the two conditions are TRUE:  think about the X of a sprite that moves itself into a square form 10 to 200 as X axis. We often type code like this.... if x+Xacceleration <= 10 OR x+Xacceleration >=200 then Xacceleration = - Xacceleration to change direction of the sprite... but the logical correct operator is XOR because if for a magic wonder X is in the same time <=0 and >=200 we change continuosely Xacceleration ( the direction of movement) getting a stuck sprite on the screen. But we are lucky that the two conditions are mutual excludent.

Thanks also to read my long post!
Programming isn't difficult, only it's  consuming time and coffee

Offline Dimster

  • Forum Resident
  • Posts: 500
    • View Profile
Re: Implied
« Reply #6 on: August 24, 2020, 01:42:15 pm »
Here's an attempt to apply IMP. Hoping to input a color which is then to imply a gender but I'm missing something. Perhap the assignment of what implies what is more complicated.

Code: QB64: [Select]
  1. a = 1 ' Male
  2. b = 2 ' Female
  3. c = 3 'Blue
  4. d = 4 'Pink
  5.  
  6.  
  7.  
  8. Resultm = c IMP a
  9. Resultm2 = a IMP c
  10. Resultf = d IMP b
  11. Resultf2 = b IMP d
  12.  
  13. INPUT " 3 or 4   ", x
  14.  
  15. IF x = 3 THEN x$ = "Blue"
  16. IF x = 4 THEN x$ = "Pink"
  17.  
  18. ' Given the Color what is the Gender
  19.  
  20.  
  21.     CASE IS = a
  22.         PRINT "Case a = "; a
  23.         PRINT "Color of x$ is "; x$
  24.         PRINT "Value of Resultm$ is "; Resultm$
  25.         PRINT "Value of Resultm2$ is "; Resultm2$
  26.         SLEEP
  27.  
  28.     CASE IS = b
  29.         PRINT "Case b = "; b
  30.         PRINT "Color of x$ is "; x$
  31.         PRINT "Value of Resultm$ is "; Resultm$
  32.         PRINT "Value of Resultm2$ is "; Resultm2$
  33.         SLEEP
  34.  
  35.     CASE IS = c
  36.         PRINT "Case c = "; c
  37.         PRINT "Color of x$ is "; x$
  38.         IF Resultm = -1 THEN Resultm$ = "Male"
  39.         IF Resultm2 = -1 THEN Resultm2$ = "Male"
  40.         IF Resultm <> -1 XOR Resultm2 <> -1 THEN
  41.             Resultm$ = "Unknown"
  42.             Resultm2$ = "Unknown"
  43.         END IF
  44.         PRINT "Value of Resultm$ is "; Resultm$
  45.         PRINT "Value of Resultm2$ is "; Resultm2$
  46.         SLEEP
  47.  
  48.     CASE IS = d
  49.         PRINT "Case d = "; d
  50.         PRINT "Color of x$ is "; x$
  51.         IF Resultf = -1 THEN Resultf$ = "Female"
  52.         IF Resultf2 = -1 THEN Resultf2$ = "Female"
  53.         IF Resultf <> -1 XOR Resultf2 <> -1 THEN
  54.             Resultf$ = "Unknown"
  55.             Resultf2$ = "Unknown"
  56.         END IF
  57.         PRINT "Value of Resultf$ is "; Resultf$
  58.         PRINT "Value of Resultf2$ is "; Resultf2$
  59.         SLEEP
  60.  
  61.  
  62.  

Offline Gets

  • Newbie
  • Posts: 28
    • View Profile
Re: Implied
« Reply #7 on: August 24, 2020, 10:25:22 pm »
With IMP, 0 always turns a bit on and 1 always leaves it alone, so it works kind of like a bit mask where you can turn on a set of bits without effecting the rest:&B11101110 IMP X would be used to flip on the first and fifth bit in a byte, for instance.

so if a=1 and c=3 then, assuming they're unsigned bytes

c IMP a would equal 253. Only the second bit would be off.
Likewise a IMP c would equal 255, just flipping everything on

If you're not looking for those kinds of values,IMP won't help you form an association between a and c. You want to introduce some kind of unique change that you can then identify later.

Quote
Hoping to input a color which is then to imply a gender but I'm missing something.

This doesn't use IMP, but it does tag colors with gender values:

Code: [Select]


TYPE ColorType

    val AS INTEGER 'holds color value and gender tags
    title AS STRING 'name for the color

END TYPE

CONST Male = 1 'first bit
CONST Female = 2 'second bit

'A color with both bits off has no assignment, only the first bit is on is for males, only the second bit is on is for females, and both on would count as unisex

DIM Colors(10) AS ColorType

RESTORE colors
FOR i = 1 TO 10

    'since the first two bits are used to identify sex, the base color values are multiples of 4; then a random sex assignment value is added to them

    Colors(i).val = (4 * i) + INT(RND * 4)
    READ Colors(i).title

    PRINT "Color #";i; " is "; Colors(i).title; ", a color for "; GetGender$(Colors(i).val); " as can be seen by the color assignment value which is "; Colors(i).val MOD 4

NEXT i




colors:

DATA "RED","GREEN","BLUE","PURPLE","ORANGE","BROWN","BLACK","HEART","ATTACK","JACK"



FUNCTION GetGender$ (v)


    IF v AND Male THEN m = 1
    IF v AND Female THEN f = 2

    'v MOD 4 would have worked too, but only because everything in this example program is neatly arranged with multiples of 4.

    SELECT CASE m + f

        CASE 0

            GetGender$ = "which there are currently no assignments"

        CASE 1

            GetGender$ = "males!"

        CASE 2

            GetGender$ = "females!"

        CASE 3

            GetGender$ = "everyone!"



    END SELECT


END FUNCTION



Offline Dimster

  • Forum Resident
  • Posts: 500
    • View Profile
Re: Implied
« Reply #8 on: August 25, 2020, 08:36:58 am »
Thanks Gets

I am more looking to use IMP as an association between two values. There is so little info on this relational operator and yet it appears (at least on literal english meaning of IMPLIED) to offer some useful purpose in AI. Manipulating bits and bytes doesn't appear to be referenced in the wiki, so thanks for that insight.

Offline Dimster

  • Forum Resident
  • Posts: 500
    • View Profile
Re: Implied
« Reply #9 on: August 25, 2020, 09:38:04 am »
Here is my latest attempt at association using IMP. The result isn't what I'm looking for but Gender does go from a -1 to a zero.

Code: QB64: [Select]
  1. DIM Male
  2. DIM Female
  3. DIM Blue
  4. DIM Pink
  5. DIM Gender
  6.  
  7. Gender = Blue IMP Male AND Gender$ = "Male"
  8. Gender = Pink IMP Female AND Gender$ = "Female"
  9. PRINT "Value of Gender is "; Gender; " and the Value of Gender$ is "; Gender$
  10.  
  11.  
  12. INPUT "Type  either Pink or Blue"; Gender
  13. PRINT "Value of Gender is now "; Gender; " and the value of Gender$ is "; Gender$  

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Implied
« Reply #10 on: August 25, 2020, 10:28:22 am »
It goes from -1 to 0, due to this line:

INPUT "Type  either Pink or Blue"; Gender

Gender is defined as a SINGLE variable type, so hitting "Pink" on the keyboard gives you a value of 0....
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Implied
« Reply #11 on: August 25, 2020, 10:44:23 am »
I have a feeling you're doing something completely wrong here.

Gender = Blue IMP Male AND Gender$ = "Male"
Gender = Pink IMP Female AND Gender$ = "Female"

AND, in both these instances are binary operators.   When evaluating the results, what you'd have is basically:

Gender = Blue IMP (Male AND (Gender$ = "Male"))  -- IMP seems to evaluate last in this order of operations.  Personally, I find this odd, as I expected it to evaluate at the same precedence level as AND does.  I would've solved this problem as Gender = (Blue IMP Male) AND (Gender$ = "Male"), which would give completely different results.  Does anyone with more experience with these commands know which is the proper sequence?  Is QB64 bugged here, or is my thinking just off, and it's something which might need expanding upon in the wiki a little more?

Since Gender$ = "", when we evaluate Gender$ = "Male", we get a value of 0, which gives us:

Gender = Blue IMP (Male AND 0)

Now, since Male is 0, we can evaluate 0 AND 0, which gives us a value of 0.  We now have:

Gender = Blue IMP 0.  Since Blue is 0, we're basically doing a 0 IMP 0 calculation, which gives us a value of -1.

Gender = -1

And the same process occurs with the second statement, since all the values we substitute into the formula stays the same.



IMP is a math operation, much like addition.  It simply compares two binary values and then returns a result based on that comparison.  It's definitely not used the way you're attempting to use it.

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

Offline luke

  • Administrator
  • Seasoned Forum Regular
  • Posts: 324
    • View Profile
Re: Implied
« Reply #12 on: August 25, 2020, 11:28:41 am »
I don't have the QB manual handy because I'm lying in bed, but the precedence should be something like (from lowest to highest):
Code: [Select]
IMP
EQV
XOR
OR
AND
NOT
=, <>, <, >, <=, >=
+, - (subtraction)
MOD
\
*, /
- (negation)
^

Offline Dimster

  • Forum Resident
  • Posts: 500
    • View Profile
Re: Implied
« Reply #13 on: August 25, 2020, 04:09:04 pm »
Took me a minute Mr. Gets - Jack gets all the girls and everyone else gets a heart attack. I'm trying to study your code to see if I can make more sense of "bit" manipulation and I total over looked the message being printed. Loved it.

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
« Last Edit: August 26, 2020, 07:12:40 am by TempodiBasic »
Programming isn't difficult, only it's  consuming time and coffee