Recent

Author Topic: [SOLVED] Program compiles, but never completes when executed  (Read 2268 times)

Nitorami

  • Hero Member
  • *****
  • Posts: 605
Re: [SOLVED] Program compiles, but never completes when executed
« Reply #30 on: December 02, 2025, 01:49:20 pm »
Ok, using an external library could be a viable alternative.  Is this "m" something Linux specific, or self made ?

tetrastes

  • Hero Member
  • *****
  • Posts: 733
Re: [SOLVED] Program compiles, but never completes when executed
« Reply #31 on: December 02, 2025, 01:52:33 pm »
This is standart unix math library libm.so.

And for windows, but here it doesn't much faster than one using fpc RTL.
Code: Pascal  [Select][+][-]
  1. {$ifdef unix}
  2. const libm = 'm';
  3. {$endif}
  4. {$ifdef mswindows}
  5. const libm = 'ucrtbase.dll';
  6. {$endif}
  7.  
  8. function sinf(x: single): single; cdecl; external libm;
  9. function cosf(x: single): single; cdecl; external libm;
  10. function tanhf(x: single): single; cdecl; external libm;
  11.  
  12. Const
  13.   MAX = 99999999;
  14.  
  15. Var
  16.   x : longint;
  17.   c : single;
  18.  
  19. Begin
  20.   x := 0;
  21.   While x < MAX Do
  22.     Begin
  23.       c := sinf(cosf(tanhf(x)));
  24.       inc(x);
  25.     End;
  26. End.

PS.
BTW, at windows using float/single instead of double in this code results in slower exe, contrary to that at linux. Tested with gcc, visual c and fpc.
« Last Edit: December 02, 2025, 02:32:16 pm by tetrastes »

dbannon

  • Hero Member
  • *****
  • Posts: 3647
    • tomboy-ng, a rewrite of the classic Tomboy
Re: [SOLVED] Program compiles, but never completes when executed
« Reply #32 on: December 03, 2025, 01:22:24 am »
Here you are (unix only)  :D

Thanks tetrastes, that is sure the smoking gun !

For those following along from home, tetrasts' code delivers (for 100 million) a substantial speed up (but the wrong answer if you total them up) -

real   0m1.017s
user   0m1.017s
sys   0m0.000s

Significantly, tetrasts's method can produces what might be the right answers with a hybrid system ! These two blocks call the m library directly using single precision but either Double or Single accumulator.  You will notice that using a Single accumulator is only useful up to 100k iterations !

Double - Iterations =1E5 total=5.14395E+04    2mS
Double - Iterations =1E6 total=5.14395E+05  12mS
Double - Iterations =1E7 total=5.14395E+06  115mS
Double - Iterations =1E8 total=5.14395E+07   1.15 S
Double - Iterations =1E9 total=5.14395E+08   12.3 S   // 1,000,000,000 iterations

Single - Iterations =1E5 total=5.14633E+04   2mS     // 100k iterations, correct total
Single - Iterations =1E6 total=5.07848E+05   11mS
Single - Iterations =1E7 total=5.00785E+06   0.1 S
Single - Iterations =1E8 total=1.67772E+07   1 S
Single - Iterations =1E9 total=1.67772E+07   10.4 S  // 1,000,000,000 but total is wrong !

For reference, "uses math", Double  and 100 Million iterations (1E8) gives the right answer but takes 6 seconds.

Conclusion
FPC "math" might not do Single Precision in any useful manner depending on your app. If you need that speed up and are really, really certain you can accept its limits (the compiler does not check for float overflow), go directly to the libm.so. Maybe consider using Single for calcs but keep (very big or very small) totals in a Double ?

Davo
« Last Edit: December 03, 2025, 01:29:14 am by dbannon »
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

Schmitty2005

  • Jr. Member
  • **
  • Posts: 52
Re: [SOLVED] Program compiles, but never completes when executed
« Reply #33 on: December 11, 2025, 07:35:39 pm »
This is standart unix math library libm.so.

And for windows, but here it doesn't much faster than one using fpc RTL.
Code: Pascal  [Select][+][-]
  1. {$ifdef unix}
  2. const libm = 'm';
  3. {$endif}
  4. {$ifdef mswindows}
  5. const libm = 'ucrtbase.dll';
  6. {$endif}
  7.  
  8. function sinf(x: single): single; cdecl; external libm;
  9. function cosf(x: single): single; cdecl; external libm;
  10. function tanhf(x: single): single; cdecl; external libm;
  11.  
  12. Const
  13.   MAX = 99999999;
  14.  
  15. Var
  16.   x : longint;
  17.   c : single;
  18.  
  19. Begin
  20.   x := 0;
  21.   While x < MAX Do
  22.     Begin
  23.       c := sinf(cosf(tanhf(x)));
  24.       inc(x);
  25.     End;
  26. End.

PS.
BTW, at windows using float/single instead of double in this code results in slower exe, contrary to that at linux. Tested with gcc, visual c and fpc.

Thank you!  This is a large improvement !  It is a little alarming that Win runs the same regardless.   I would still think that a single is a single, and the programmer should decide if a double is needed.  If you want high accuracy, use a double.  If you don't need that high of accuracy, use a single and gain some speed.  If you want a more accurate single, use double for some calcs and cast to single.....



 

TinyPortal © 2005-2018