Recent

Author Topic: This is WIERD  (Read 955 times)

J-G

  • Hero Member
  • *****
  • Posts: 1023
Re: This is WIERD
« Reply #30 on: May 28, 2026, 11:13:23 pm »
EDIT  -  What happened to @WP's post ??
I had deleted it after seeing emersonline's message which told me that my idea of the "optimized-away" expression with unused result was nonsense.
Shame  -  without seeing that, I wouldn't have realised that the semi-colon was the 'Key'.
FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

wp

  • Hero Member
  • *****
  • Posts: 13550
Re: This is WIERD
« Reply #31 on: May 29, 2026, 01:33:52 am »
No, it's not the key. You just have to press F8 one or two more times so that the red line is at the end of the procedure - then D has its correct value (*). Or - without the debugger - try this: use only the compact line, and put the result into variable A rather than D:
Code: Pascal  [Select][+][-]
  1.   Procedure Find_A;
  2.   begin
  3.     if sin_b = 0 then
  4.       begin
  5.       {
  6.         A := b*b + c*c;
  7.         A := A - (2*b*c * cos_a);
  8.         A := Sqrt(A);
  9.         }
  10.         A := Sqrt((b*b + c*c) - ((2*b*c) * cos_a))
  11.       end
  12.     else
  13.       A := (B * sin_a) / sin_b;
  14.   end;

(*) Of course, you can ask why this is necessary ... But then ask Martin_fr, the debugger magician.

J-G

  • Hero Member
  • *****
  • Posts: 1023
Re: This is WIERD
« Reply #32 on: May 29, 2026, 11:57:52 am »
Missed an opportunity to optimize  Tan_X  as  Sin_X / Cos_X  :P

I've been considering this option and have come to the conclusion that using the existing [Tan] Function is the better method.

If the angle is 90° (which, when talking of Triangles, is often likely!)  then Cos(90) is zero so dividing by zero will create an error.   I have to assume that the in-built Tan function takes care of such so using that I don't need to take the precaution.

I'd appreciate a counter argument :)
FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

emersonline

  • Newbie
  • Posts: 4
Re: This is WIERD
« Reply #33 on: May 29, 2026, 04:22:38 pm »
No, it's not the key. You just have to press F8 one or two more times so that the red line is at the end of the procedure - then D has its correct value (*). Or - without the debugger - try this: use only the compact line, and put the result into variable A rather than D:
Code: Pascal  [Select][+][-]
  1.   Procedure Find_A;
  2.   begin
  3.     if sin_b = 0 then
  4.       begin
  5.       {
  6.         A := b*b + c*c;
  7.         A := A - (2*b*c * cos_a);
  8.         A := Sqrt(A);
  9.         }
  10.         A := Sqrt((b*b + c*c) - ((2*b*c) * cos_a))
  11.       end
  12.     else
  13.       A := (B * sin_a) / sin_b;
  14.   end;

(*) Of course, you can ask why this is necessary ... But then ask Martin_fr, the debugger magician.

I had already come across this peculiar behavior with the Lazarus debugger before (where the cursor passes over a statement without executing it, reaches the end of the block, and then re-enters the block to finally execute the instruction), but I didn't remember it during my first test. Anyway, your observation about including the missing semicolon was not only effective but also reinforces a good practice. Even though the compiler allows it, it can lead to strange results/behaviors, as we just saw in this case.

 

TinyPortal © 2005-2018