Recent

Author Topic: Unit decfloat  (Read 1201 times)

srvaldez

  • Full Member
  • ***
  • Posts: 190
Unit decfloat
« on: January 16, 2026, 11:36:13 pm »
I adapted my decfloat routines into a unit using ADVANCEDRECORDS
I had very strange errors while debugging, try as I might the overloaded trig function were not visible to the test program
I shuffled code around and recompiled, the sqrt was visible but not the trigs, then I decided to post the code here for help
I copied just the unit source and the test program to a new folder then tried to compile, now the operators were not visible because it seemed that the forward declarations were not needed before, here's what I think was happening, changing the code in the unit and then re-compiling did not replace the previous object files, from now on I will manually delete the object files before re-compiling

the unit is hardwired for 112 digits, you can change the constant num_dwords from 13 to something else if you like but the trig functions are tailored to about 100 digits

updated
« Last Edit: January 20, 2026, 05:05:35 pm by srvaldez »

jamie

  • Hero Member
  • *****
  • Posts: 7516
Re: Unit decfloat
« Reply #1 on: January 17, 2026, 12:09:12 am »
use the BUILD instead if just compile when you suspect something like this.

BUILD ALL deletes the object files and rebuilds them.

Jamie
The only true wisdom is knowing you know nothing

speter

  • Sr. Member
  • ****
  • Posts: 487
Re: Unit decfloat
« Reply #2 on: January 17, 2026, 02:46:47 am »
For whatever it is worth, I was able to compile the trig unit, and test it using your test code.

The output was:
Code: [Select]
777777777777777777777777777777777777777777777777777777777777777777777777777777777777777778
 1.414213562373095048801688724209698078569671875376948073176679737990732478462107038850387534
 .841470984807896506652502321630298999622563060798371065672751709991910404391239668948639744
 .540302305868139717400936607442976603732310420617922227670097255381100394774471764517951856
 1.557407724654902230506974807458360173087250772381520038383946605698861397151727289555099965

BUT, there were lots of hints and warnings (which may help you):
Code: [Select]
Hint: (11030) Start of reading config file C:\lazarus\fpc\3.2.2\bin\x86_64-win64\fpc.cfg
Hint: (11031) End of reading config file C:\lazarus\fpc\3.2.2\bin\x86_64-win64\fpc.cfg
Note: (11046) DWARF debug information cannot be used with smart linking on this target, switching to static linking
Free Pascal Compiler version 3.2.2 [2025/11/08] for x86_64
Copyright (c) 1993-2021 by Florian Klaempfl and others
(1002) Target OS: Win64 for x64
(3104) Compiling project1.lpr
(3104) Compiling unit1.pas
(3104) Compiling unitDecfloat.pas
C:\Users\spete\Documents\programming\lazarus\from_others\trig_func\Unit-decfloat\2026-01-17\unitDecfloat.pas(1943,50) Hint: (5057) Local variable "tmb" does not seem to be initialized
C:\Users\spete\Documents\programming\lazarus\from_others\trig_func\Unit-decfloat\2026-01-17\unitDecfloat.pas(1942,40) Hint: (5057) Local variable "pam" does not seem to be initialized
C:\Users\spete\Documents\programming\lazarus\from_others\trig_func\Unit-decfloat\2026-01-17\unitDecfloat.pas(1942,50) Hint: (5057) Local variable "tam" does not seem to be initialized
C:\Users\spete\Documents\programming\lazarus\from_others\trig_func\Unit-decfloat\2026-01-17\unitDecfloat.pas(1943,45) Hint: (5057) Local variable "qmb" does not seem to be initialized
C:\Users\spete\Documents\programming\lazarus\from_others\trig_func\Unit-decfloat\2026-01-17\unitDecfloat.pas(1942,45) Hint: (5057) Local variable "qam" does not seem to be initialized
C:\Users\spete\Documents\programming\lazarus\from_others\trig_func\Unit-decfloat\2026-01-17\unitDecfloat.pas(1943,40) Hint: (5057) Local variable "pmb" does not seem to be initialized
C:\Users\spete\Documents\programming\lazarus\from_others\trig_func\Unit-decfloat\2026-01-17\unitDecfloat.pas(1964,43) Hint: (5057) Local variable "t" does not seem to be initialized
C:\Users\spete\Documents\programming\lazarus\from_others\trig_func\Unit-decfloat\2026-01-17\unitDecfloat.pas(1964,40) Hint: (5057) Local variable "q" does not seem to be initialized
C:\Users\spete\Documents\programming\lazarus\from_others\trig_func\Unit-decfloat\2026-01-17\unitDecfloat.pas(1964,37) Hint: (5057) Local variable "p" does not seem to be initialized
C:\Users\spete\Documents\programming\lazarus\from_others\trig_func\Unit-decfloat\2026-01-17\unitDecfloat.pas(1991,13) Hint: (5057) Local variable "one" does not seem to be initialized
C:\Users\spete\Documents\programming\lazarus\from_others\trig_func\Unit-decfloat\2026-01-17\unitDecfloat.pas(1984,11) Hint: (5057) Local variable "y" does not seem to be initialized
C:\Users\spete\Documents\programming\lazarus\from_others\trig_func\Unit-decfloat\2026-01-17\unitDecfloat.pas(2039,3) Warning: (2005) Comment level 2 found
C:\Users\spete\Documents\programming\lazarus\from_others\trig_func\Unit-decfloat\2026-01-17\unitDecfloat.pas(2045,3) Warning: (2005) Comment level 2 found
C:\Users\spete\Documents\programming\lazarus\from_others\trig_func\Unit-decfloat\2026-01-17\unitDecfloat.pas(2047,3) Warning: (2005) Comment level 2 found
C:\Users\spete\Documents\programming\lazarus\from_others\trig_func\Unit-decfloat\2026-01-17\unitDecfloat.pas(2049,3) Warning: (2005) Comment level 2 found
C:\Users\spete\Documents\programming\lazarus\from_others\trig_func\Unit-decfloat\2026-01-17\unitDecfloat.pas(2114,21) Hint: (5057) Local variable "n" does not seem to be initialized
C:\Users\spete\Documents\programming\lazarus\from_others\trig_func\Unit-decfloat\2026-01-17\unitDecfloat.pas(2114,18) Hint: (5057) Local variable "y" does not seem to be initialized
C:\Users\spete\Documents\programming\lazarus\from_others\trig_func\Unit-decfloat\2026-01-17\unitDecfloat.pas(2843,17) Hint: (5058) Variable "$result" does not seem to be initialized
C:\Users\spete\Documents\programming\lazarus\from_others\trig_func\Unit-decfloat\2026-01-17\unitDecfloat.pas(2850,11) Hint: (5057) Local variable "p" does not seem to be initialized
C:\Users\spete\Documents\programming\lazarus\from_others\trig_func\Unit-decfloat\2026-01-17\unitDecfloat.pas(2899,101) Hint: (5024) Parameter "dwords_in" not used
C:\Users\spete\Documents\programming\lazarus\from_others\trig_func\Unit-decfloat\2026-01-17\unit1.pas(19,28) Hint: (5024) Parameter "Sender" not used
C:\Users\spete\Documents\programming\lazarus\from_others\trig_func\Unit-decfloat\2026-01-17\unit1.pas(9,3) Hint: (5023) Unit "Math" not used in Unit1
(9022) Compiling resource C:\Users\spete\Documents\programming\lazarus\from_others\trig_func\Unit-decfloat\2026-01-17\lib\x86_64-win64\project1.obj
(9015) Linking C:\Users\spete\Documents\programming\lazarus\from_others\trig_func\Unit-decfloat\2026-01-17\project1.exe
(1008) 3047 lines compiled, 0.6 sec, 1672320 bytes code, 126436 bytes data
(1021) 4 warning(s) issued
(1022) 20 hint(s) issued
(1023) 1 note(s) issued

I am attaching my test project.

cheers
S.
I climbed mighty mountains, and saw that they were actually tiny foothills. :)

srvaldez

  • Full Member
  • ***
  • Posts: 190
Re: Unit decfloat
« Reply #3 on: January 17, 2026, 06:58:26 am »
thanks Jamie and speter
how can I extend write and writeln for the decfloat type without breaking it?
the following enables to use writeln with decfloat
Code: Pascal  [Select][+][-]
  1. procedure writeln(param : decfloat); inline;
  2. begin
  3.     System.writeln(fp2str(param)); // Call the built-in writeln with a converted type
  4. end;
  5.  

but there's a problem, writeln(x) works with decfloat but with nothing else, how can I add support for decfloat ?

<edit> just for your info, you can use toString
Code: Pascal  [Select][+][-]
  1.         writeln(y.toString); //full precision
  2.         writeln(y.toString(20)); //user specified precision
  3.  
similarly you can use toStringExp for exponential format but the exponential format doesn't round up
« Last Edit: January 17, 2026, 07:07:53 am by srvaldez »

cdbc

  • Hero Member
  • *****
  • Posts: 2600
    • http://www.cdbc.dk
Re: Unit decfloat
« Reply #4 on: January 17, 2026, 09:19:10 am »
Hi
Quote
how can I extend write and writeln for the decfloat type without breaking it?
Change the compiler internally!
'Cause 'Write' & 'Writeln' are compiler-intrinsics ~ Magic
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

MathMan

  • Sr. Member
  • ****
  • Posts: 472
Re: Unit decfloat
« Reply #5 on: January 17, 2026, 07:09:24 pm »
Hello srvaldez,

I think your questions have been answered. Don't know where you are heading with you decfloat unit, but the following link (and associated sources on GitLab) may be of interest ...

https://arxiv.org/abs/2011.11524

Cheers,
MathMan

srvaldez

  • Full Member
  • ***
  • Posts: 190
Re: Unit decfloat
« Reply #6 on: January 17, 2026, 07:13:15 pm »
thanks for the link MathMan :)
but I would still like to automatically cast to string when using write[ln] with the decfloat type, perhaps overload WriteStr ?
« Last Edit: January 17, 2026, 07:20:44 pm by srvaldez »

MathMan

  • Sr. Member
  • ****
  • Posts: 472
Re: Unit decfloat
« Reply #7 on: January 17, 2026, 08:29:41 pm »
thanks for the link MathMan :)
but I would still like to automatically cast to string when using write[ln] with the decfloat type, perhaps overload WriteStr ?

Might be possible, haven't checked. It definitely is not possible for Write or WriteLn - you would have to change the intrinsics in the compiler for that. Which would render this unusable for users of the standard compiler.

But what would be the advantage of overloading WriteStr - in terms of source clarity there is no difference between WriteStr( FpDec) and Write( ToString( FpDec ) ), imho.

srvaldez

  • Full Member
  • ***
  • Posts: 190
Re: Unit decfloat
« Reply #8 on: January 20, 2026, 12:14:43 am »
I updated the unit, fixed a bug in the conversion to string and replaced the clunky exp function, added some things and hopefully a more interesting example, new upload in opening post
using code tags to hopefully preserve the formatting
Code: [Select]
   Gauss-Legendre 10 Quuadrature rule
                    Xi                                             Wi
   -.9739065285171717200779640120844520534283      .06667134430868813759356880989333179285786
   -.8650633666889845107320966884234930485275      .1494513491505805931457763396576973324026
   -.6794095682990244062343273651148735757693      .2190863625159820439955349342281631924588
   -.4333953941292471907992659431657841622001      .2692667193099963550912269215694693528598
   -.1488743389816312108848260011297199846176      .295524224714752870173892994651338329421
    .1488743389816312108848260011297199846176      .295524224714752870173892994651338329421
    .4333953941292471907992659431657841622001      .2692667193099963550912269215694693528598
    .6794095682990244062343273651148735757693      .2190863625159820439955349342281631924588
    .8650633666889845107320966884234930485275      .1494513491505805931457763396576973324026
    .9739065285171717200779640120844520534283      .06667134430868813759356880989333179285786

Integrating Exp(x) over [-3, 3]:  20.03574985481979
Actual value:  20.035749854819804
Relative error: -6.950990959e-16

 Bernoulli(  0) =  1
 Bernoulli(  1) = -.5
 Bernoulli(  2) =  .1666666666666666666666666666666666666667
 Bernoulli(  4) = -.03333333333333333333333333333333333333333
 Bernoulli(  6) =  .02380952380952380952380952380952380952381
 Bernoulli(  8) = -.03333333333333333333333333333333333333333
 Bernoulli( 10) =  .07575757575757575757575757575757575757576
 Bernoulli( 12) = -.2531135531135531135531135531135531135531
 Bernoulli( 14) =  1.1666666666666666666666666666666666666667
 Bernoulli( 16) = -7.0921568627450980392156862745098039215686
 Bernoulli( 18) =  54.971177944862155388471177944862155388471
 Bernoulli( 20) = -529.12424242424242424242424242424242424242
 Bernoulli( 22) =  6192.1231884057971014492753623188405797101
 Bernoulli( 24) = -86580.253113553113553113553113553113553114
 Bernoulli( 26) =  1425517.1666666666666666666666666666666667
 Bernoulli( 28) = -27298231.067816091954022988505747126436782
 Bernoulli( 30) =  601580873.9006423683843038681748359167714

 Euler numbers via Bernoulli numbers
 https://math.stackexchange.com/questions/4489269/euler-numbers-and-bernoulli-numbers
         n
       -----
        \       n!                          B(k+1)
 E(n) =  )  --------- (2^(k+1) - 2^(2*k+2) -------
        /    k!(b-k)!                        k+1
       -----
        k=0

 Euler(  0) =  1
 Euler(  2) = -1
 Euler(  4) =  5
 Euler(  6) = -61
 Euler(  8) =  1385
 Euler( 10) = -50521
 Euler( 12) =  2702765
 Euler( 14) = -199360981
 Euler( 16) =  19391512145
 Euler( 18) = -2404879675441
 Euler( 20) =  370371188237525
 Euler( 22) = -69348874393137901
 Euler( 24) =  15514534163557086905
 Euler( 26) = -4087072509293123892361
 Euler( 28) =  1252259641403629865468285
 Euler( 30) = -441543893249023104553682821

srvaldez

  • Full Member
  • ***
  • Posts: 190
Re: Unit decfloat
« Reply #9 on: January 27, 2026, 12:32:11 am »
implemented complex trig, log, exp, gamma and zeta functions
the Riemann zeta function is evaluated using the Euler-Maclaurin summation formula and the gamma function is approximated using the Stirling's formula, all functions give a bit over 100 digits accuracy for the most part, huge arguments will affect accuracy especially for the trig functions
I think I will wait a little before posting the code so that I may test it a bit more

srvaldez

  • Full Member
  • ***
  • Posts: 190
Re: Unit decfloat
« Reply #10 on: January 28, 2026, 02:29:47 pm »
//===================================== complex =========================================
// the code is a translation of the C++ code published on the web with some tweaks to make the results agree with Mathematica
// I also added the complex ArcTan2, complex Zeta and complex gamma functions
// the file has since vanished from the web but his revised work is available at https://github.com/RobTillaart/Complex/tree/master
//
//     FILE: Complex.cpp
//   AUTHOR: Rob Tillaart
//  VERSION: 0.1.09
//  PURPOSE: library for Complex math for Arduino
//      URL: http://arduino.cc/playground/Main/ComplexMath
//
//  Released to the public domain

I first translated the code to Free Basic on Sep 17, 2017 and presently to Free Pascal from the FB translation
functions and operators

// the following are overloaded for the non-complex decfloat type and therefore not possible for decfloatc
//   operator := (r : Int16) z : decfloatc;
//   operator := (r : Int32) z : decfloatc;
//   operator := (r : int64) z : decfloatc;
//   operator := (r : double) z : decfloatc;

// the following are implemented for decfloac , the complex type

   operator := (r : decfloatc) z : ansistring;
   Operator + (x, y : decfloatc) z : decfloatc;
   Operator + (x : decfloatc; y : decfloat) z : decfloatc;
   Operator + (x : decfloat; y : decfloatc) z : decfloatc;
   Operator - (y : decfloatc) z : decfloatc; //negate
   Operator - (x, y : decfloatc) z : decfloatc;
   Operator - (x : decfloatc; y : Double) z : decfloatc;
   Operator - (x : Double; y : decfloatc) z : decfloatc;
   Operator * (x, y : decfloatc) z : decfloatc;
   Operator * (x : decfloatc; y : decfloat) z : decfloatc;
   Operator * (x : decfloat; y : decfloatc) z : decfloatc;
   Operator / (x, y : decfloatc) z : decfloatc;
   Operator / (x : decfloatc; y : Double) z : decfloatc;
   Operator / (x : Double; y : decfloatc) z : decfloatc;
   Operator ** (const x : decfloatc; const y : decfloatc) : decfloatc;
   Operator ** (const x : Double; const y : decfloatc) : decfloatc;
   Operator ** (const x : Integer; const y : decfloatc) : decfloatc;

   function Str2c(const s: ansistring) : decfloatc;
   Function csquare(x : decfloatc) : decfloatc;
   Function reciprocal(x :decfloatc) : decfloatc;
   Function cabs(x : decfloatc) : decfloatc;
   Function csqrt(x : decfloatc) : decfloatc;
   Function cexp(x : decfloatc) : decfloatc;
   Function clog(x : decfloatc) : decfloatc;
   Function cpow(Const x, c : decfloatc) : decfloatc;
   Function clogn(Const x, c : decfloatc) : decfloatc;
   Function clog10(Const x : decfloatc) : decfloatc;
   Function csin(const z : decfloatc) : decfloatc;
   Function ccos(const z : decfloatc) : decfloatc;
   Function ctan(const z : decfloatc) : decfloatc;
   Function cArcSin(const x : decfloatc) : decfloatc;
   Function cArcCos(const x : decfloatc) : decfloatc;
   Function cArcTan(const x : decfloatc) : decfloatc;
   Function ccsc(const x : decfloatc) : decfloatc;
   Function csec(const x : decfloatc) : decfloatc;
   Function ccot(const x : decfloatc) : decfloatc;
   Function cArcCsc(const x : decfloatc) : decfloatc;
   Function cArcSec(const x : decfloatc) : decfloatc;
   Function cArcCot(const x : decfloatc) : decfloatc;

   //HYPERBOLICUS I
   Function csinh(const x : decfloatc) : decfloatc;
   Function ccosh(const x : decfloatc) : decfloatc;
   Function ctanh(const x : decfloatc) : decfloatc;
   Function cArcSinh(const x : decfloatc) : decfloatc;
   Function cArcCosh(const x : decfloatc) : decfloatc;
   Function cArcTanh(const x : decfloatc) : decfloatc;

   //HYPERBOLICUS II
   Function ccsch(const x : decfloatc) : decfloatc;
   Function csech(const x : decfloatc) : decfloatc;
   Function ccoth(const x : decfloatc) : decfloatc;
   Function cArcCsch(const x : decfloatc) : decfloatc;
   Function cArcSech(const x : decfloatc) : decfloatc;
   Function cArcCoth(const x : decfloatc) : decfloatc;
   Function fpsignc(const x : decfloatc) : Int32;

   function toDecfloatc(const x : decfloat; const y : decfloat) : decfloatc; overload;
   function toDecfloatc(const x : double; const y : decfloat) : decfloatc; overload;
   function toDecfloatc(const x : decfloat; const y : double) : decfloatc; overload;
   function toDecfloatc(const x : double; const y : double) : decfloatc; overload;
   function toDecfloatc(const x : Int32; const y : decfloat) : decfloatc; overload;
   function toDecfloatc(const x : decfloat; const y : Int32) : decfloatc; overload;
   function toDecfloatc(const x : Int32; const y : Int32) : decfloatc; overload;
   function cArctan2(y, x: decfloatc): decfloatc; overload;
« Last Edit: January 30, 2026, 03:16:57 pm by srvaldez »

srvaldez

  • Full Member
  • ***
  • Posts: 190
Re: Unit decfloat
« Reply #11 on: January 28, 2026, 02:30:30 pm »
out of curiosity I overloaded the := operator for complex to string, this is probably not recommended but it works, you may wish to remove it
Code: Pascal  [Select][+][-]
  1.   operator := (r : decfloatc) z : ansistring;
  2.  
  3.   begin
  4.     z:=fp2str(r.re, 105);
  5.     if r.im <0 then
  6.       z:=z+' -'+fp2str(abs(r.im), 105)+'*I'
  7.     else
  8.       z:=z+' +'+fp2str(r.im, 105)+'*I';
  9.   end;
  10.  
so you can do crazy things like the following
Code: Pascal  [Select][+][-]
  1.     var xc, yc, zc:decfloatc;
  2.         s:ansistring;
  3.     begin
  4.         xc.re:=123;
  5.         xc.im:=456;
  6.         s:=xc;  // complex number is converted to string
  7.         writeln(s);
  8.     end.
  9.  

output
 123 + 456*I
aside from assigning values to the re and im members you can use Str2c
Code: Pascal  [Select][+][-]
  1. xc:=Str2c('1.5 - 1.5*I');
  2.  

 

TinyPortal © 2005-2018