### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

### Author Topic: [SOLVED] Fuzzy Math  (Read 686 times)

#### Slyde

• Jr. Member
• Posts: 83
##### [SOLVED] Fuzzy Math
« on: June 27, 2022, 09:24:23 pm »
Being the lightweight in the world of Free Pascal and Lazarus that I am, I find myself at a loss as to why one of these works

Code: Pascal  [Select][+][-]
1. var i: integer;
2. // query here
3. i:= previousVec.Size - previousTracker; // previousVec is a vector, and previousTracker is a integer
4. DMod1.ZQ_Main.Params.ParamByName('id').AsInteger:= previousVec[i-1];// subtracting 1 from i works perfectly
and one doesn't.
Code: Pascal  [Select][+][-]
1. var i: integer;
2. // query here
3. i:= (previousVec.Size - previousTracker) - 1;// should be the same as subtracting 1 from i
4. DMod1.ZQ_Main.Params.ParamByName('id').AsInteger:= previousVec[i]; // but this fails

Can someone tell me the difference between the two math problems?

How is
Code: Pascal  [Select][+][-]
1. i:= previousVec.Size - previousTracker;
2. i - 1;

not the same as
Code: Pascal  [Select][+][-]
1. i:= (previousVec.Size - previousTracker) - 1;
« Last Edit: June 28, 2022, 03:50:10 am by Slyde »
Linux Mint 20.3 / Lazarus 2.2.2 / FPC 3.2.2

#### BobDog

• Sr. Member
• Posts: 299
##### Re: Fuzzy Math
« Reply #1 on: June 28, 2022, 12:58:52 am »

What exactly is i-1
i-=1 is not allowed in pascal.
i:=i-1 is allowed in code, although in a maths exam you would surely fail
i:=i-1
implies
i-i=-1
i.e.
0=-1

#### Thausand

• Sr. Member
• Posts: 282
##### Re: Fuzzy Math
« Reply #2 on: June 28, 2022, 01:17:56 am »
What exactly is i-1
i is value then subract 1

Quote
implies
No, it not implies.

question OP is:
Code: Pascal  [Select][+][-]
1. var
2.   i,j, k: integer;
3.
4. i := previousVec.size - previousTracker;
5. j := i-1;
6.
7. k := previousVec.size - previousTracker - 1;
8.
.. and why k <> j

make example:
Code: [Select]
`previousVec.Size := 10;previousTracker  := 8i := 10 - 8; (=2)j := 2 - 1;  (=1)k := 10 - 8 - 1; (=1)`
User slyde say k <> j

Is not logical but can if integer overflow

#### Slyde

• Jr. Member
• Posts: 83
##### Re: Fuzzy Math
« Reply #3 on: June 28, 2022, 02:26:29 am »
I didn't mean to confuse anyone.  I posted just the relevant code to my question, but I'll add to it.

This is the working code:
Code: Pascal  [Select][+][-]
1. procedure TForm1.btnPreviousClick(Sender: TObject);
2. var
3.   i: Integer;
4. begin
5.   btnVerify.Enabled:= False;
6.
7.   DMod1.ZQ_Main.Close;
8.   DMod1.ZQ_Main.SQL.Text := 'SELECT question, a1, a2, a3, a4, the_answer ' +
9.                               'FROM mult_choice '+
10.                               'WHERE id = :id';
11.   i:= previousVec.Size - previousTracker;  // previousVec is a global vector, and previousTracker is a global integer
12.   DMod1.ZQ_Main.Params.ParamByName('id').AsInteger:= previousVec[i-1];
13.   DMod1.ZQ_Main.Open;
14.   lblQuestion.Caption:= DMod1.ZQ_Main.Fields[0].AsString;
15.   rbtn_A1.Caption:= DMod1.ZQ_Main.Fields[1].AsString;
16.   rbtn_A2.Caption:= DMod1.ZQ_Main.Fields[2].AsString;
17.   rbtn_A3.Caption:= DMod1.ZQ_Main.Fields[3].AsString;
18.   rbtn_A4.Caption:= DMod1.ZQ_Main.Fields[4].AsString;
20.
21.   if i = 1 THEN btnPrevious.Enabled:= False;
22.
23.   previousTracker:= previousTracker + 1;
24. end;

Please note lines 11 and 12.

Now, here's the code that fails.  Note the changes in lines 11 and 12:
Code: Pascal  [Select][+][-]
1. procedure TForm1.btnPreviousClick(Sender: TObject);
2. var
3.   i: Integer;
4. begin
5.   btnVerify.Enabled:= False;
6.
7.   DMod1.ZQ_Main.Close;
8.   DMod1.ZQ_Main.SQL.Text := 'SELECT question, a1, a2, a3, a4, the_answer ' +
9.                               'FROM mult_choice '+
10.                               'WHERE id = :id';
11.   i:= (previousVec.Size - previousTracker) - 1;
12.   DMod1.ZQ_Main.Params.ParamByName('id').AsInteger:= previousVec[i];
13.   DMod1.ZQ_Main.Open;
14.   lblQuestion.Caption:= DMod1.ZQ_Main.Fields[0].AsString;
15.   rbtn_A1.Caption:= DMod1.ZQ_Main.Fields[1].AsString;
16.   rbtn_A2.Caption:= DMod1.ZQ_Main.Fields[2].AsString;
17.   rbtn_A3.Caption:= DMod1.ZQ_Main.Fields[3].AsString;
18.   rbtn_A4.Caption:= DMod1.ZQ_Main.Fields[4].AsString;
20.
21.   if i = 1 THEN btnPrevious.Enabled:= False;
22.
23.   previousTracker:= previousTracker + 1;
24. end;

In the top block of code, I use
Code: Pascal  [Select][+][-]
1.  i:= previousVec.Size - previousTracker;

Then I use the value of i and subtract 1 from it:
Code: Pascal  [Select][+][-]
1. DMod1.ZQ_Main.Params.ParamByName('id').AsInteger:= previousVec[i-1];

This works perfectly. No issues at all.

However, when I combine the math in the second block of code:
Code: Pascal  [Select][+][-]
1.  i:= (previousVec.Size - previousTracker) - 1;

it should provide me the same outcome as the code in the first block.  But it fails.  So I was thinking I cld get clarity on this here in the forum.
Linux Mint 20.3 / Lazarus 2.2.2 / FPC 3.2.2

#### Thausand

• Sr. Member
• Posts: 282
##### Re: Fuzzy Math
« Reply #4 on: June 28, 2022, 03:28:57 am »
I didn't mean to confuse anyone.
It clear

Quote
However, when I combine the math in the second block of code:
Code: Pascal  [Select][+][-]
1.  i:= (previousVec.Size - previousTracker) - 1;

it should provide me the same outcome as the code in the first block.  But it fails.  So I was thinking I cld get clarity on this here in the forum.

It is produce same.
Code: Pascal  [Select][+][-]
1. program vectortest;
2.
3. uses
4.   gvector;
5.
6. type
7.   TPreviousVec=specialize TVector<integer>;
8.
9. var
10.   PreviousVec:TPreviousVec;
11.   PreviousTracker:integer;
12.   n,i,j:integer;
13. begin
14.   PreviousVec:=TPreviousVec.Create;
15.
16.   for n:=10 to 20
17.     do PreviousVec.PushBack(n);
18.
19.   PreviousTracker:=0;
20.
21.   while previousTracker<PreviousVec.Size do
22.   begin
23.     i:=PreviousVec.Size-PreviousTracker;
24.     j:=PreviousVec.Size-PreviousTracker-1;
25.     writeln('previousVec[i-1]= ',previousVec[i-1]:3,'  previousVec[j]= ',previousVec[j]:3,'  (i=',i:3,', i-1=',i-1:3,', j=',j:3,')');
26.     inc(previousTracker, 1)
27.   end;
28.
29.   PreviousVec.Free
30. end.
31.

output:
Code: [Select]
`previousVec[i-1]=  20  previousVec[j]=  20  (i= 11, i-1= 10, j= 10)previousVec[i-1]=  19  previousVec[j]=  19  (i= 10, i-1=  9, j=  9)previousVec[i-1]=  18  previousVec[j]=  18  (i=  9, i-1=  8, j=  8)previousVec[i-1]=  17  previousVec[j]=  17  (i=  8, i-1=  7, j=  7)previousVec[i-1]=  16  previousVec[j]=  16  (i=  7, i-1=  6, j=  6)previousVec[i-1]=  15  previousVec[j]=  15  (i=  6, i-1=  5, j=  5)previousVec[i-1]=  14  previousVec[j]=  14  (i=  5, i-1=  4, j=  4)previousVec[i-1]=  13  previousVec[j]=  13  (i=  4, i-1=  3, j=  3)previousVec[i-1]=  12  previousVec[j]=  12  (i=  3, i-1=  2, j=  2)previousVec[i-1]=  11  previousVec[j]=  11  (i=  2, i-1=  1, j=  1)previousVec[i-1]=  10  previousVec[j]=  10  (i=  1, i-1=  0, j=  0)`
If not overflow integer then is needed more code. But ... you can make simple compare and show msg.

#### Slyde

• Jr. Member
• Posts: 83
##### Re: Fuzzy Math
« Reply #5 on: June 28, 2022, 03:49:47 am »
That's as good of an explanation as anyone cld ever get.  Thanks, Thausand.  Thanks for your time.
Linux Mint 20.3 / Lazarus 2.2.2 / FPC 3.2.2

#### Thausand

• Sr. Member
• Posts: 282
##### Re: Fuzzy Math
« Reply #6 on: June 28, 2022, 03:50:34 am »

This is the working code:
Code: Pascal  [Select][+][-]
1. procedure TForm1.btnPreviousClick(Sender: TObject);
2. var
3.   i: Integer;
4. begin
5.   btnVerify.Enabled:= False;
6.
7.   DMod1.ZQ_Main.Close;
8.   DMod1.ZQ_Main.SQL.Text := 'SELECT question, a1, a2, a3, a4, the_answer ' +
9.                               'FROM mult_choice '+
10.                               'WHERE id = :id';
11.   i:= previousVec.Size - previousTracker;  // previousVec is a global vector, and previousTracker is a global integer
12.   DMod1.ZQ_Main.Params.ParamByName('id').AsInteger:= previousVec[i-1];
13.   DMod1.ZQ_Main.Open;
14.   lblQuestion.Caption:= DMod1.ZQ_Main.Fields[0].AsString;
15.   rbtn_A1.Caption:= DMod1.ZQ_Main.Fields[1].AsString;
16.   rbtn_A2.Caption:= DMod1.ZQ_Main.Fields[2].AsString;
17.   rbtn_A3.Caption:= DMod1.ZQ_Main.Fields[3].AsString;
18.   rbtn_A4.Caption:= DMod1.ZQ_Main.Fields[4].AsString;
20.
21.   if i = 1 THEN btnPrevious.Enabled:= False;
22.
23.   previousTracker:= previousTracker + 1;
24. end;

Now, here's the code that fails.  Note the changes in lines 11 and 12:
Code: Pascal  [Select][+][-]
1. procedure TForm1.btnPreviousClick(Sender: TObject);
2. var
3.   i: Integer;
4. begin
5.   btnVerify.Enabled:= False;
6.
7.   DMod1.ZQ_Main.Close;
8.   DMod1.ZQ_Main.SQL.Text := 'SELECT question, a1, a2, a3, a4, the_answer ' +
9.                               'FROM mult_choice '+
10.                               'WHERE id = :id';
11.   i:= (previousVec.Size - previousTracker) - 1;
12.   DMod1.ZQ_Main.Params.ParamByName('id').AsInteger:= previousVec[i];
13.   DMod1.ZQ_Main.Open;
14.   lblQuestion.Caption:= DMod1.ZQ_Main.Fields[0].AsString;
15.   rbtn_A1.Caption:= DMod1.ZQ_Main.Fields[1].AsString;
16.   rbtn_A2.Caption:= DMod1.ZQ_Main.Fields[2].AsString;
17.   rbtn_A3.Caption:= DMod1.ZQ_Main.Fields[3].AsString;
18.   rbtn_A4.Caption:= DMod1.ZQ_Main.Fields[4].AsString;
20.
21.   if i = 1 THEN btnPrevious.Enabled:= False;
22.
23.   previousTracker:= previousTracker + 1;
24. end;

look highlight line. If change i then change if

#### Slyde

• Jr. Member
• Posts: 83
##### Re: [SOLVED] Fuzzy Math
« Reply #7 on: June 28, 2022, 04:40:21 am »
It means the user has seen all previous data.  So I disable the button at i = 1, the first element of previousVec record.  There's nothing left to see.  It makes sure the user can't click the button and try to read an element from the vector that's not there.  And the next click of the Next button re-enables it.  So far, everything's working out in my little program.  But I have to say that getting the bugs out of the previous button click stuff was about as much fun as having a bowl of rocks for breakfast
« Last Edit: June 28, 2022, 06:20:57 am by Slyde »
Linux Mint 20.3 / Lazarus 2.2.2 / FPC 3.2.2

#### Thausand

• Sr. Member
• Posts: 282
##### Re: [SOLVED] Fuzzy Math
« Reply #8 on: June 28, 2022, 05:26:53 am »
It means the user has seen all previous data.
Yes, i now that

Quote
So I disable the button at i = 1, the first element of previousVec record.
Yes/No

In first code answer yes. i=1, vectorindex = i-1 = 0

In second code answer no. i=1 vectorindex = i = 1.

Vector index is run zero ... size-1.

Quote
There's nothing left to see. It makes sure the user can't click the button and try to read an element from the vector that's not there.
If i = 1 then is miss zero vector index that have value.

Code: Pascal  [Select][+][-]
1.   btnPrevious.Enabled := (i=1);
2.

but if want have good disable button then have show vector index zero to, is better code:
Code: Pascal  [Select][+][-]
1.   btnPrevious.Enabled := (i=0);
2.

But can also make button enable before subtract.

Quote
But I have to say that getting the bugs out of the previous button click stuff was about as much fun having a bowl of rocks for breakfast
If you have solved problem then good

#### Slyde

• Jr. Member
• Posts: 83
##### Re: [SOLVED] Fuzzy Math
« Reply #9 on: June 28, 2022, 06:28:10 am »
I'll show you all the code when I finish this.  But it isn't my fault when you go crazy after seeing it

But yes, it does work as is.  And I understand what you mean, in that vectors are zero-based.  But what I showed isn't the finished product.  I don't like how it looks either.  It shld be 0 and not 1.  But...like I said: I'm a
Quote
lightweight in the world of Free Pascal and Lazarus

But I'll get there.  With a little help from my friends.

I appreciate your input, Thausand.  I really do.  Constructive words are always welcome.
« Last Edit: June 28, 2022, 06:49:54 am by Slyde »
Linux Mint 20.3 / Lazarus 2.2.2 / FPC 3.2.2

#### Thausand

• Sr. Member
• Posts: 282
##### Re: [SOLVED] Fuzzy Math
« Reply #10 on: June 28, 2022, 07:05:16 am »
But...like I said: I'm a
Quote
lightweight in the world of Free Pascal and Lazarus

But... you eat all that rocks

Quote
But I'll get there.
Eat more rocks ?

Quote
I appreciate your input, Thausand.  I really do.  Constructive words are always welcome.
Thank you for kind words

• Hero Member
• Posts: 11774
##### Re: [SOLVED] Fuzzy Math
« Reply #11 on: June 28, 2022, 08:11:30 am »
Note - according to subject - I already posted a full fuzzy logic unit a couple of years ago. And based on science, not rumours (or in some cases above sheer incompetence ill informed or not schooled on the subject).
See here: https://forum.lazarus.freepascal.org/index.php/topic,41144.msg295348.html#msg295348
This is the Zadeh logic which is most common. And the unit is much simpler........

And before some human starts complaining: this is on topic and a better way to solve OP's problem.
« Last Edit: June 28, 2022, 08:48:17 am by Thaddy »
Black themes should be banned.

#### Slyde

• Jr. Member
• Posts: 83
##### Re: [SOLVED] Fuzzy Math
« Reply #12 on: June 28, 2022, 03:37:43 pm »
That's some pretty fuzzy-looking stuff, Thaddy

Not sure how I'd apply it in my program, but I'll sure study on it.  Thanks for the offering.
Linux Mint 20.3 / Lazarus 2.2.2 / FPC 3.2.2