Forum > Databases
[SOLVED] Fuzzy Math
Slyde:
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 [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---var i: integer;// query herei:= previousVec.Size - previousTracker; // previousVec is a vector, and previousTracker is a integerDMod1.ZQ_Main.Params.ParamByName('id').AsInteger:= previousVec[i-1];// subtracting 1 from i works perfectlyand one doesn't.
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---var i: integer;// query herei:= (previousVec.Size - previousTracker) - 1;// should be the same as subtracting 1 from iDMod1.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 [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---i:= previousVec.Size - previousTracker;i - 1;
not the same as
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---i:= (previousVec.Size - previousTracker) - 1;
BobDog:
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:
--- Quote from: BobDog on June 28, 2022, 12:58:52 am ---What exactly is i-1
--- End quote ---
i is value then subract 1
--- Quote ---implies
--- End quote ---
No, it not implies.
question OP is:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---var i,j, k: integer; i := previousVec.size - previousTracker;j := i-1; k := previousVec.size - previousTracker - 1; .. and why k <> j
make example:
--- Code: ---previousVec.Size := 10;
previousTracker := 8
i := 10 - 8; (=2)
j := 2 - 1; (=1)
k := 10 - 8 - 1; (=1)
--- End code ---
User slyde say k <> j
Is not logical but can if integer overflow
Slyde:
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 [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm1.btnPreviousClick(Sender: TObject);var i: Integer;begin btnVerify.Enabled:= False; DMod1.ZQ_Main.Close; DMod1.ZQ_Main.SQL.Text := 'SELECT question, a1, a2, a3, a4, the_answer ' + 'FROM mult_choice '+ 'WHERE id = :id'; i:= previousVec.Size - previousTracker; // previousVec is a global vector, and previousTracker is a global integer DMod1.ZQ_Main.Params.ParamByName('id').AsInteger:= previousVec[i-1]; DMod1.ZQ_Main.Open; lblQuestion.Caption:= DMod1.ZQ_Main.Fields[0].AsString; rbtn_A1.Caption:= DMod1.ZQ_Main.Fields[1].AsString; rbtn_A2.Caption:= DMod1.ZQ_Main.Fields[2].AsString; rbtn_A3.Caption:= DMod1.ZQ_Main.Fields[3].AsString; rbtn_A4.Caption:= DMod1.ZQ_Main.Fields[4].AsString; lblTheAnswer.Caption:= DMod1.ZQ_Main.Fields[5].AsString; if i = 1 THEN btnPrevious.Enabled:= False; previousTracker:= previousTracker + 1;end;
Please note lines 11 and 12.
Now, here's the code that fails. Note the changes in lines 11 and 12:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm1.btnPreviousClick(Sender: TObject);var i: Integer;begin btnVerify.Enabled:= False; DMod1.ZQ_Main.Close; DMod1.ZQ_Main.SQL.Text := 'SELECT question, a1, a2, a3, a4, the_answer ' + 'FROM mult_choice '+ 'WHERE id = :id'; i:= (previousVec.Size - previousTracker) - 1; DMod1.ZQ_Main.Params.ParamByName('id').AsInteger:= previousVec[i]; DMod1.ZQ_Main.Open; lblQuestion.Caption:= DMod1.ZQ_Main.Fields[0].AsString; rbtn_A1.Caption:= DMod1.ZQ_Main.Fields[1].AsString; rbtn_A2.Caption:= DMod1.ZQ_Main.Fields[2].AsString; rbtn_A3.Caption:= DMod1.ZQ_Main.Fields[3].AsString; rbtn_A4.Caption:= DMod1.ZQ_Main.Fields[4].AsString; lblTheAnswer.Caption:= DMod1.ZQ_Main.Fields[5].AsString; if i = 1 THEN btnPrevious.Enabled:= False; previousTracker:= previousTracker + 1;end;
In the top block of code, I use
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} --- i:= previousVec.Size - previousTracker;
Then I use the value of i and subtract 1 from it:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---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 [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} --- 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.
Thausand:
--- Quote from: Slyde on June 28, 2022, 02:26:29 am ---I didn't mean to confuse anyone.
--- End quote ---
It clear :)
--- Quote ---However, when I combine the math in the second block of code:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} --- 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.
--- End quote ---
It is produce same.
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program vectortest; uses gvector; type TPreviousVec=specialize TVector<integer>; var PreviousVec:TPreviousVec; PreviousTracker:integer; n,i,j:integer;begin PreviousVec:=TPreviousVec.Create; for n:=10 to 20 do PreviousVec.PushBack(n); PreviousTracker:=0; while previousTracker<PreviousVec.Size do begin i:=PreviousVec.Size-PreviousTracker; j:=PreviousVec.Size-PreviousTracker-1; writeln('previousVec[i-1]= ',previousVec[i-1]:3,' previousVec[j]= ',previousVec[j]:3,' (i=',i:3,', i-1=',i-1:3,', j=',j:3,')'); inc(previousTracker, 1) end; PreviousVec.Freeend.
output:
--- Code: ---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)
--- End code ---
If not overflow integer then is needed more code. But ... you can make simple compare and show msg.
Navigation
[0] Message Index
[#] Next page