Re: Could you please explain me whats happening in this code.


[ Delphi Forum ] [ Delphi Tutorials -- by DelphiLand ]

Posted by webmaster Guido on July 03, 2003 at 16:34:29:

In Reply to: Could you please explain me whats happening in this code. posted by p12297 on July 01, 2003 at 18:32:01:

: Could you please explain me whats happening in this code.

: if (abs (err)>epsilon) OR (abs (Overshoot)>epsilon) then
: [ and do on... see previous message]
--------------

In order to analyze the code, I rearranged it slightly so that we can have a look at the structure.
There's a small typo in the code sample: "if abs(err) 0" probably should be "if abs(err) > 0". Further on, the final "end;" has no corresponding "begin", so I left that out. This gives:

if (Abs(Err) > Epsilon) or (Abs(Overshoot) > Epsilon) then 
begin
  if Sign(Err) = Sign(Overshoot) then 
  begin
    if Abs(Err) > 0 then 
      MyForm.Info.Lines.Append(S + ' ')
    else 
       MyForm.Info.Lines.Append(S + ' -');
    Overshoot := - Sign(Overshoot) * Epsilon;
  end
  else 
    Overshoot := Err;
end
else 
  if Overshoot = 0 then 
    Overshoot := Err;

I suppose that "Err" contains an error value, a positive or negative deviation from some reference value. "Epsilon" seems to be a positive constant, to which "Err" and "Overshoot" are compared.
Sign(X) looks like a function that returns 1 if X >= 0 and returns -1 if X < 0.

Looks like the code is written to be called repeatedly for different values of "Err", to recalculate Overhoot each time. In pseudo code:

if (absolute value of Err) > Epsilon or
   (absolute value of OverShoot) > Epsilon then 
begin
   if Err and Overshoot have the same sign then 
   begin
      invert sign of Overshoot, multiply result by Epsilon
      // also add a line to memo-component "Info"
      if (abs value of Err) > 0 then
         add string S followed by a space 
      else // (abs value of Err) = 0 
         add string S followed by space and minus sign
   end
   else  // Err and Overshoot have different signs
      Overshoot := Err 
end  
else  // neither of the absolute values is greater than Epsilon
   if Overshoot = 0 then 
      Overshoot := Err
   // else don't change Overshoot

Hope that this is what you're asking for... please correct me if I'm wrong :)


Related Articles and Replies:


[ Delphi Forum ]
[ DelphiLand: free Delphi source code, tips, tutorials ]