Recent

Author Topic: need help with StdOut  (Read 350 times)

srvaldez

  • Full Member
  • ***
  • Posts: 201
need help with StdOut
« on: April 24, 2026, 06:03:45 pm »
I need help with using StdOut
just for fun I wanted to run this simple libflint test
Code: Pascal  [Select][+][-]
  1. program fmpq_series;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   ctypes, SysUtils;
  7.  
  8. { ===== GMP / FLINT type definitions ===== }
  9.  
  10. type
  11.   mp_limb_t         = culong;
  12.   mp_limb_signed_t  = clong;
  13.   slong             = mp_limb_signed_t;
  14.   fmpz              = mp_limb_signed_t;
  15.   Pfmpz             = ^fmpz;
  16.  
  17.   fmpz_t = array[0..0] of fmpz;
  18.   Pfmpz_t = ^fmpz_t;
  19.  
  20.   fmpq = record
  21.     num : fmpz;
  22.     den : fmpz;
  23.   end;
  24.   Pfmpq = ^fmpq;
  25.   fmpq_t = fmpq;
  26.   Pfmpq_t = ^fmpq_t;
  27.  
  28.   fmpq_poly_struct = record
  29.     coeffs : Pfmpz;
  30.     den    : fmpz_t;
  31.     alloc  : clong;
  32.     length : slong;
  33.   end;
  34.   Pfmpq_poly_struct = ^fmpq_poly_struct;
  35.   fmpq_poly_t = fmpq_poly_struct;
  36.   Pfmpq_poly_t = ^fmpq_poly_t;
  37.  
  38.   { Minimal FILE type placeholder for stdout passing }
  39.   PFILE = Pointer;
  40.  
  41. { ===== External FLINT declarations ===== }
  42.  
  43. procedure _fmpz_clear_mpz(f: fmpz); cdecl; external 'flint';
  44. procedure fmpq_poly_init(poly: Pfmpq_poly_struct); cdecl; external 'flint';
  45. function  fmpq_poly_set_str(poly: Pfmpq_poly_struct; str: PChar): clong; cdecl; external 'flint';
  46. procedure fmpq_poly_revert_series_newton(res: Pfmpq_poly_struct; poly: Pfmpq_poly_struct; n: mp_limb_signed_t); cdecl; external 'flint';
  47. function  fmpq_poly_fprint_pretty(f: PFILE; poly: Pfmpq_poly_struct; varname: PChar): clong; cdecl; external 'flint';
  48. function  fmpq_poly_get_str(poly: Pfmpq_poly_struct): PChar; cdecl; external 'flint';
  49. function  fmpq_poly_get_str_pretty(poly: Pfmpq_poly_struct; varname: PChar): PChar; cdecl; external 'flint';
  50. procedure fmpq_poly_set_coeff_fmpq(poly: Pfmpq_poly_struct; n: mp_limb_signed_t; x: Pfmpq); cdecl; external 'flint';
  51. procedure fmpq_poly_clear(poly: Pfmpq_poly_struct); cdecl; external 'flint';
  52. procedure fmpq_set_si(res: Pfmpq; p: mp_limb_signed_t; q: mp_limb_t); cdecl; external 'flint';
  53. procedure fmpz_set_si(f: Pfmpz; val: mp_limb_signed_t); cdecl; external 'flint';
  54. procedure fmpz_fac_ui(f: Pfmpz; n: mp_limb_t); cdecl; external 'flint';
  55. procedure fmpq_set_fmpz_frac(res: Pfmpq; p: Pfmpz; q: Pfmpz); cdecl; external 'flint';
  56.  
  57. { ===== Inline helpers (mirrors FreeBasic inline subs) ===== }
  58.  
  59. procedure fmpz_init(f: Pfmpz); inline;
  60. begin
  61.   f^ := 0;
  62. end;
  63.  
  64. procedure fmpz_clear(f: Pfmpz); inline;
  65. begin
  66.   { If the top bit pattern signals an MPZ pointer, call the real clear }
  67.   if (f^ shr (32 - 2)) = 1 then
  68.     _fmpz_clear_mpz(f^);
  69. end;
  70.  
  71. procedure fmpq_init(x: Pfmpq); inline;
  72. begin
  73.   x^.num := 0;
  74.   x^.den := 1;
  75. end;
  76.  
  77. procedure fmpq_clear(x: Pfmpq); inline;
  78. begin
  79.   fmpz_clear(@x^.num);
  80.   fmpz_clear(@x^.den);
  81. end;
  82.  
  83.  
  84. { FreePascal does not expose a raw FILE* stdout directly in the same way.
  85.   We declare it from libc instead. }
  86. //var
  87.   //libc_stdout: PFILE; external 'c' name 'stdout';
  88. //StdOut: Text;
  89. { ===== Main program ===== }
  90.  
  91. var
  92.   i, sone : slong;
  93.   series  : fmpq_poly_t;
  94.   rseries : fmpq_poly_t;
  95.   term    : fmpq_t;
  96.   mpz_num : fmpz;
  97.   mpz_den : fmpz;
  98.   size    : Integer;
  99.   sp      : PChar;
  100.  
  101. begin
  102.   WriteLn('starting');
  103.  
  104.   size := 28;
  105.  
  106.   fmpz_init(@mpz_num);
  107.   fmpz_init(@mpz_den);
  108.   fmpq_init(@term);
  109.   fmpq_poly_init(@series);
  110.   fmpq_poly_init(@rseries);
  111.  
  112.   { ---- arctan series via string ---- }
  113.   WriteLn('assign the arctan series to series');
  114.   WriteLn('fmpq_poly_set_str(@series, "10  0 1 0 -1/3 0 1/5 0 -1/7 0 1/9")');
  115.   fmpq_poly_set_str(@series, '10  0 1 0 -1/3 0 1/5 0 -1/7 0 1/9');
  116.   WriteLn;
  117.  
  118.   WriteLn('now get the series back into a string');
  119.   WriteLn('sp=fmpq_poly_get_str(@series)');
  120.   WriteLn;
  121.   sp := fmpq_poly_get_str(@series);
  122.   WriteLn('--> ', sp);
  123.   WriteLn;
  124.  
  125.   WriteLn('get a pretty reprentation into string');
  126.   WriteLn('sp=fmpq_poly_get_str_pretty(@series, "x")');
  127.   WriteLn;
  128.   sp := fmpq_poly_get_str_pretty(@series, 'x');
  129.   WriteLn('--> ', sp);
  130.   WriteLn;
  131.  
  132.   WriteLn('now reverse the series to get the tangent series');
  133.   WriteLn('fmpq_poly_revert_series_newton(@rseries, @series , 10)');
  134.   WriteLn;
  135.   fmpq_poly_revert_series_newton(@rseries, @series, 10);
  136.  
  137.   WriteLn('print the series');
  138.   WriteLn('fmpq_poly_fprint_pretty(stdout, @rseries, "x")');
  139.   WriteLn;
  140.   Write('--> ');
  141.   fmpq_poly_fprint_pretty(@stdout, @rseries, 'x');
  142.   WriteLn;
  143.   WriteLn('==========================================');
  144.   WriteLn;
  145.  
  146.   { ---- sine series via loop ---- }
  147.   WriteLn('populate series with the sine series');
  148.   WriteLn('x - x^3/3! + x^5/5! ... using a program block');
  149.   WriteLn('and then reverse the series');
  150.   WriteLn;
  151.   Write('--> ');
  152.  
  153.   sone := 1;
  154.   i    := 1;
  155.   while i < size do
  156.   begin
  157.     fmpz_set_si(@mpz_num, sone);
  158.     fmpz_fac_ui(@mpz_den, mp_limb_t(i));
  159.     fmpq_set_fmpz_frac(@term, @mpz_num, @mpz_den);
  160.     fmpq_poly_set_coeff_fmpq(@series, i, @term);
  161.     Inc(i);
  162.     fmpq_set_si(@term, 0, 1);   { even power terms are 0 }
  163.     fmpq_poly_set_coeff_fmpq(@series, i, @term);
  164.     Inc(i);
  165.     sone := -sone;
  166.   end;
  167.  
  168.   fmpq_poly_revert_series_newton(@rseries, @series, size);
  169.   fmpq_poly_fprint_pretty(@stdout, @rseries, 'x');
  170.   WriteLn;
  171.   WriteLn;
  172.  
  173.   { ---- arctan series via loop ---- }
  174.   WriteLn('do the same with the arctan series');
  175.   WriteLn('x - x^3/3 + x^5/5 ...');
  176.   WriteLn;
  177.   Write('--> ');
  178.  
  179.   sone := 1;
  180.   i    := 1;
  181.   while i < size do
  182.   begin
  183.     fmpq_set_si(@term, sone, mp_limb_t(i));
  184.     fmpq_poly_set_coeff_fmpq(@series, i, @term);
  185.     Inc(i);
  186.     fmpq_set_si(@term, 0, 1);   { even power terms are 0 }
  187.     fmpq_poly_set_coeff_fmpq(@series, i, @term);
  188.     Inc(i);
  189.     sone := -sone;
  190.   end;
  191.  
  192.   fmpq_poly_revert_series_newton(@rseries, @series, size);
  193.   fmpq_poly_fprint_pretty(@stdout, @rseries, 'x');
  194.   WriteLn;
  195.  
  196.   { ---- cleanup ---- }
  197.   fmpq_clear(@term);
  198.   fmpq_poly_clear(@series);
  199.   fmpq_poly_clear(@rseries);
  200.   fmpz_clear(@mpz_den);
  201.   fmpz_clear(@mpz_num);
  202. end.
  203.  
the program compiles just fine but crashes when run
if you want to compile the program here are the Windows x64 dlls https://u.pcloud.link/publink/show?code=XZM6qE5Z6hypS8RlxPLgERcpvGTmOLnI4cN7

Thaddy

  • Hero Member
  • *****
  • Posts: 19156
  • Glad to be alive.
Re: need help with StdOut
« Reply #1 on: April 24, 2026, 06:07:52 pm »
There are at least 3 dll's missing.
1. libgimp-10.dll
2. libmpfr-6.dll
3. libquadmath-0.dll

All of these are non-standard and need to be included in your distribution.

The good news is your program did not crash here... It recovered beautifully.

I can't help any further before that is fixed. (and 32 or 64 bit is known)

Oh, for the time being, add {$packrecords C} ...... I can't find any pack settings?
« Last Edit: April 24, 2026, 06:18:29 pm by Thaddy »
objects are fine constructs. You can even initialize them with constructors.

srvaldez

  • Full Member
  • ***
  • Posts: 201
Re: need help with StdOut
« Reply #2 on: April 24, 2026, 06:24:00 pm »
thanks Thaddy  :)
ok, here are the dlls https://u.pcloud.link/publink/show?code=XZUIqE5Z1SS96paTbFJ2GfSwMYlmVux4U5Tk
64-bit Windows

srvaldez

  • Full Member
  • ***
  • Posts: 201
Re: need help with StdOut
« Reply #3 on: April 24, 2026, 09:17:35 pm »
using fmpq_poly_get_str_pretty instead of fmpq_poly_fprint_pretty fixes the problem
had to change  mp_limb_t and mp_limb_signed_t to ptruint, they were wrong

Code: Pascal  [Select][+][-]
  1. program fmpq_series;
  2.  
  3. {$mode objfpc}{$H+}
  4. {$packrecords C}
  5.  
  6. uses
  7.   gmp, ctypes, SysUtils;
  8.  
  9. { ===== GMP / FLINT type definitions ===== }
  10.  
  11. type
  12.   mp_limb_t         = ptruint;
  13.   mp_limb_signed_t  = ptruint;
  14.   slong             = mp_limb_signed_t;
  15.   fmpz              = mp_limb_signed_t;
  16.   Pfmpz             = ^fmpz;
  17.  
  18.   fmpz_t = array[0..0] of fmpz;
  19.   Pfmpz_t = ^fmpz_t;
  20.  
  21.   fmpq = record
  22.     num : fmpz;
  23.     den : fmpz;
  24.   end;
  25.   Pfmpq = ^fmpq;
  26.   fmpq_t = fmpq;
  27.   Pfmpq_t = ^fmpq_t;
  28.  
  29.   fmpq_poly_struct = record
  30.     coeffs : Pfmpz;
  31.     den    : fmpz_t;
  32.     alloc  : clong;
  33.     length : slong;
  34.   end;
  35.   Pfmpq_poly_struct = ^fmpq_poly_struct;
  36.   fmpq_poly_t = fmpq_poly_struct;
  37.   Pfmpq_poly_t = ^fmpq_poly_t;
  38.  
  39.   { Minimal FILE type placeholder for stdout passing }
  40.   PFILE = Pointer;
  41.  
  42. { ===== External FLINT declarations ===== }
  43.  
  44. procedure _fmpz_clear_mpz(f: fmpz); cdecl; external 'flint';
  45. procedure fmpq_poly_init(poly: Pfmpq_poly_struct); cdecl; external 'flint';
  46. function  fmpq_poly_set_str(poly: Pfmpq_poly_struct; str: PChar): clong; cdecl; external 'flint';
  47. procedure fmpq_poly_revert_series_newton(res: Pfmpq_poly_struct; poly: Pfmpq_poly_struct; n: mp_limb_signed_t); cdecl; external 'flint';
  48. function  fmpq_poly_fprint_pretty(f: PFILE; poly: Pfmpq_poly_struct; varname: PChar): clong; cdecl; external 'flint';
  49. function  fmpq_poly_get_str(poly: Pfmpq_poly_struct): PChar; cdecl; external 'flint';
  50. function  fmpq_poly_get_str_pretty(poly: Pfmpq_poly_struct; varname: PChar): PChar; cdecl; external 'flint';
  51. procedure fmpq_poly_set_coeff_fmpq(poly: Pfmpq_poly_struct; n: mp_limb_signed_t; x: Pfmpq); cdecl; external 'flint';
  52. procedure fmpq_poly_clear(poly: Pfmpq_poly_struct); cdecl; external 'flint';
  53. procedure fmpq_set_si(res: Pfmpq; p: mp_limb_signed_t; q: mp_limb_t); cdecl; external 'flint';
  54. procedure fmpz_set_si(f: Pfmpz; val: mp_limb_signed_t); cdecl; external 'flint';
  55. procedure fmpz_fac_ui(f: Pfmpz; n: mp_limb_t); cdecl; external 'flint';
  56. procedure fmpq_set_fmpz_frac(res: Pfmpq; p: Pfmpz; q: Pfmpz); cdecl; external 'flint';
  57.  
  58. { ===== Inline helpers (mirrors FreeBasic inline subs) ===== }
  59.  
  60. procedure fmpz_init(f: Pfmpz); inline;
  61. begin
  62.   f^ := 0;
  63. end;
  64.  
  65. procedure fmpz_clear(f: Pfmpz); inline;
  66. begin
  67.   { If the top bit pattern signals an MPZ pointer, call the real clear }
  68.   if (f^ shr (32 - 2)) = 1 then
  69.     _fmpz_clear_mpz(f^);
  70. end;
  71.  
  72. procedure fmpq_init(x: Pfmpq); inline;
  73. begin
  74.   x^.num := 0;
  75.   x^.den := 1;
  76. end;
  77.  
  78. procedure fmpq_clear(x: Pfmpq); inline;
  79. begin
  80.   fmpz_clear(@x^.num);
  81.   fmpz_clear(@x^.den);
  82. end;
  83.  
  84.  
  85. { FreePascal does not expose a raw FILE* stdout directly in the same way.
  86.   We declare it from libc instead. }
  87. //var
  88.   //libc_stdout: PFILE; external 'c' name 'stdout';
  89. //StdOut: Text;
  90. { ===== Main program ===== }
  91.  
  92. var
  93.   i, sone : slong;
  94.   series  : fmpq_poly_t;
  95.   rseries : fmpq_poly_t;
  96.   term    : fmpq_t;
  97.   mpz_num : fmpz;
  98.   mpz_den : fmpz;
  99.   size    : Integer;
  100.   sp      : PChar;
  101.  
  102. begin
  103.   WriteLn('starting');
  104.  
  105.   size := 28;
  106.  
  107.   fmpz_init(@mpz_num);
  108.   fmpz_init(@mpz_den);
  109.   fmpq_init(@term);
  110.   fmpq_poly_init(@series);
  111.   fmpq_poly_init(@rseries);
  112.  
  113.   { ---- arctan series via string ---- }
  114.   WriteLn('assign the arctan series to series');
  115.   WriteLn('fmpq_poly_set_str(@series, "10  0 1 0 -1/3 0 1/5 0 -1/7 0 1/9")');
  116.   fmpq_poly_set_str(@series, '10  0 1 0 -1/3 0 1/5 0 -1/7 0 1/9');
  117.   WriteLn;
  118.  
  119.   WriteLn('now get the series back into a string');
  120.   WriteLn('sp=fmpq_poly_get_str(@series)');
  121.   WriteLn;
  122.   sp := fmpq_poly_get_str(@series);
  123.   WriteLn('--> ', sp);
  124.   WriteLn;
  125.  
  126.   WriteLn('get a pretty reprentation into string');
  127.   WriteLn('sp=fmpq_poly_get_str_pretty(@series, "x")');
  128.   WriteLn;
  129.   sp := fmpq_poly_get_str_pretty(@series, 'x');
  130.   WriteLn('--> ', sp);
  131.   WriteLn;
  132.  
  133.   WriteLn('now reverse the series to get the tangent series');
  134.   WriteLn('fmpq_poly_revert_series_newton(@rseries, @series , 10)');
  135.   WriteLn;
  136.   fmpq_poly_revert_series_newton(@rseries, @series, 10);
  137.  
  138.   WriteLn('print the series');
  139.   WriteLn('fmpq_poly_fprint_pretty(stdout, @rseries, "x")');
  140.   WriteLn;
  141.   Write('--> ');
  142.   //fmpq_poly_fprint_pretty(@stdout, @rseries, 'x');
  143.   sp := fmpq_poly_get_str_pretty(@rseries, 'x');
  144.   WriteLn('rseries--> ', sp);
  145.   WriteLn;
  146.   WriteLn('==========================================');
  147.   WriteLn;
  148.  
  149.   { ---- sine series via loop ---- }
  150.   WriteLn('populate series with the sine series');
  151.   WriteLn('x - x^3/3! + x^5/5! ... using a program block');
  152.   WriteLn('and then reverse the series');
  153.   WriteLn;
  154.   Write('--> ');
  155.  
  156.   sone := 1;
  157.   i    := 1;
  158.   while i < size do
  159.   begin
  160.     fmpz_set_si(@mpz_num, sone);
  161.     fmpz_fac_ui(@mpz_den, mp_limb_t(i));
  162.     fmpq_set_fmpz_frac(@term, @mpz_num, @mpz_den);
  163.     fmpq_poly_set_coeff_fmpq(@series, i, @term);
  164.     Inc(i);
  165.     fmpq_set_si(@term, 0, 1);   { even power terms are 0 }
  166.     fmpq_poly_set_coeff_fmpq(@series, i, @term);
  167.     Inc(i);
  168.     sone := -sone;
  169.   end;
  170.  
  171.   fmpq_poly_revert_series_newton(@rseries, @series, size);
  172.   sp := fmpq_poly_get_str_pretty(@series, 'x');
  173.   WriteLn('rseries--> ', sp);
  174.   WriteLn;
  175.   WriteLn;
  176.  
  177.   { ---- arctan series via loop ---- }
  178.   WriteLn('do the same with the arctan series');
  179.   WriteLn('x - x^3/3 + x^5/5 ...');
  180.   WriteLn;
  181.   Write('--> ');
  182.  
  183.   sone := 1;
  184.   i    := 1;
  185.   while i < size do
  186.   begin
  187.     fmpq_set_si(@term, sone, mp_limb_t(i));
  188.     fmpq_poly_set_coeff_fmpq(@series, i, @term);
  189.     Inc(i);
  190.     fmpq_set_si(@term, 0, 1);   { even power terms are 0 }
  191.     fmpq_poly_set_coeff_fmpq(@series, i, @term);
  192.     Inc(i);
  193.     sone := -sone;
  194.   end;
  195.  
  196.   fmpq_poly_revert_series_newton(@rseries, @series, size);
  197.   sp := fmpq_poly_get_str_pretty(@rseries, 'x');
  198.   WriteLn('rseries--> ', sp);
  199.   WriteLn;
  200.  
  201.   { ---- cleanup ---- }
  202.   fmpq_clear(@term);
  203.   fmpq_poly_clear(@series);
  204.   fmpq_poly_clear(@rseries);
  205.   fmpz_clear(@mpz_den);
  206.   fmpz_clear(@mpz_num);
  207. end.
  208.  

output
Quote
starting
assign the arctan series to series
fmpq_poly_set_str(@series, "10  0 1 0 -1/3 0 1/5 0 -1/7 0 1/9")

now get the series back into a string
sp=fmpq_poly_get_str(@series)

--> 10  0 1 0 -1/3 0 1/5 0 -1/7 0 1/9

get a pretty reprentation into string
sp=fmpq_poly_get_str_pretty(@series, "x")

--> 1/9*x^9 - 1/7*x^7 + 1/5*x^5 - 1/3*x^3 + 1*x

now reverse the series to get the tangent series
fmpq_poly_revert_series_newton(@rseries, @series , 10)

print the series
fmpq_poly_fprint_pretty(stdout, @rseries, "x")

--> rseries--> 62/2835*x^9 + 17/315*x^7 + 2/15*x^5 + 1/3*x^3 + 1*x

==========================================

populate series with the sine series
x - x^3/3! + x^5/5! ... using a program block
and then reverse the series

--> rseries--> -1/10888869450418352160768000000*x^27 + 1/15511210043330985984000000*x^25 - 1/25852016738884976640000*x^23 + 1/51090942171709440000*x^21 - 1/121645100408832000*x^19 + 1/355687428096000*x^17 - 1/1307674368000*x^15 + 1/6227020800*x^13 - 1/39916800*x^11 + 1/362880*x^9 - 1/5040*x^7 + 1/120*x^5 - 1/6*x^3 + 1*x


do the same with the arctan series
x - x^3/3 + x^5/5 ...

--> rseries--> 8374643517010684/1298054391195577640625*x^27 + 58870668456604/3698160658676859375*x^25 + 113927491862/2900518163668125*x^23 + 18888466084/194896477400625*x^21 + 443861162/1856156927625*x^19 + 6404582/10854718875*x^17 + 929569/638512875*x^15 + 21844/6081075*x^13 + 1382/155925*x^11 + 62/2835*x^9 + 17/315*x^7 + 2/15*x^5 + 1/3*x^3 + 1*x

 

TinyPortal © 2005-2018