Recent

Author Topic: StrMath.pas a String Number Calculator Unit  (Read 1161 times)

JgQDev

  • New member
  • *
  • Posts: 9
StrMath.pas a String Number Calculator Unit
« on: February 17, 2026, 07:30:58 am »
Hello everyone

For anyone who is looking for a String number calculator, pure String.
I have created a Free Pascal Unit for just that

https://github.com/JgQDev/StrMath

valdir.marcos

  • Hero Member
  • *****
  • Posts: 1178
Re: StrMath.pas a String Number Calculator Unit
« Reply #1 on: February 17, 2026, 10:39:09 am »
Very nice.

Bart

  • Hero Member
  • *****
  • Posts: 5706
    • Bart en Mariska's Webstek
Re: StrMath.pas a String Number Calculator Unit
« Reply #2 on: February 17, 2026, 12:22:29 pm »
I have some (silly) implementation of integer math with strings myself.
I used advanced records to "hide"the internal implementation.
As a result I was able to overload operators (+, -, *, **, div,  mod, :=, =, <, >, <>) so I can simply do something like:
Code: Pascal  [Select][+][-]
  1. var
  2.   A, B,  C: TBigInt;
  3. begin
  4.   ...
  5.   C := A + B;
  6.   if C < Googol then ;

Bart
 

BeniBela

  • Hero Member
  • *****
  • Posts: 958
    • homepage
Re: StrMath.pas a String Number Calculator Unit
« Reply #3 on: February 17, 2026, 02:58:32 pm »
On the one hand, it's impressive how many functions this unit offers, but on the other hand, it's completely pointless because strings are probably the worst way to store numbers

JgQDev

  • New member
  • *
  • Posts: 9
Re: StrMath.pas a String Number Calculator Unit
« Reply #4 on: February 17, 2026, 03:27:23 pm »
On the one hand, it's impressive how many functions this unit offers, but on the other hand, it's completely pointless because strings are probably the worst way to store numbers

Actually, Its not just Strings, you can also use Array of Byte
to store data as Binary. And the length of the Array of Byte depends on the
number.

The Array of Byte could be a Integer or Real

Bart

  • Hero Member
  • *****
  • Posts: 5706
    • Bart en Mariska's Webstek
Re: StrMath.pas a String Number Calculator Unit
« Reply #5 on: February 17, 2026, 10:56:34 pm »
On the one hand, it's impressive how many functions this unit offers, but on the other hand, it's completely pointless because strings are probably the worst way to store numbers
Same for my unit, but it was good fun implementing it (using school arithmetic algorithms like longdivision etc.).
In theory it supports up to appr. 10^2000000000 digits (my lib is integer only).
Would not advice to try that though, it might take more time then there is to get to the heath death of the universe...

Bart

srvaldez

  • Full Member
  • ***
  • Posts: 192
Re: StrMath.pas a String Number Calculator Unit
« Reply #6 on: February 18, 2026, 03:03:50 am »
Hi JgQDev
what made you decide to use strings?
doing math with strings is quite slow, I also toyed with arbitrary precision math but I used a record of integers and tried to emulate floating point
considering that I use basic school arithmetic algorithms it's performance is decent
« Last Edit: February 18, 2026, 03:05:21 am by srvaldez »

JgQDev

  • New member
  • *
  • Posts: 9
Re: StrMath.pas a String Number Calculator Unit
« Reply #7 on: February 18, 2026, 05:56:44 am »
Hi JgQDev
what made you decide to use strings?
doing math with strings is quite slow, I also toyed with arbitrary precision math but I used a record of integers and tried to emulate floating point
considering that I use basic school arithmetic algorithms it's performance is decent

I decided to Create the Unit in case I need accuracy in my calculation. Half, Single and Doubles has a problem with accuracy, and I know Strings and Real Integer Binary Calculations are slow when trying to calculate a ton of numbers, but I will take it for accurate results

creaothceann

  • Sr. Member
  • ****
  • Posts: 278
Re: StrMath.pas a String Number Calculator Unit
« Reply #8 on: February 18, 2026, 07:07:43 am »
Half, Single and Doubles has a problem with accuracy
There's also the Fractions unit, which already may be all you need, depending on your use case.

LV

  • Sr. Member
  • ****
  • Posts: 427
Re: StrMath.pas a String Number Calculator Unit
« Reply #9 on: February 18, 2026, 11:48:43 am »
Good job. On the other hand, for these kinds of tasks, you could create a binding to, for example, the powerful and free Maxima system.

Let's say we need to calculate an integral:

     +∞  —x²
     ∫  e    dx
    —∞

Result:
      ___
     √ π

And then get the numerical value with 100 significant digits.


Code: Pascal  [Select][+][-]
  1. program MaximaGaussianIntegral;
  2.  
  3. uses
  4.   SysUtils,
  5.   Classes,
  6.   Process;
  7.  
  8.   function RunMaximaOnce(const Expr: string): string;
  9.   var
  10.     P: TProcess;
  11.     Output: TStringList;
  12.   begin
  13.     P := TProcess.Create(nil);
  14.     Output := TStringList.Create;
  15.     try
  16.       P.Executable := 'C:\maxima-5.46.0\bin\maxima.bat';
  17.       P.Parameters.Add('--very-quiet');
  18.       P.Parameters.Add('-r');
  19.       P.Parameters.Add(Expr + '; quit();');
  20.       P.Options := [poWaitOnExit, poUsePipes];
  21.       P.Execute;
  22.       Output.LoadFromStream(P.Output);
  23.       Result := Trim(Output.Text);
  24.     finally
  25.       Output.Free;
  26.       P.Free;
  27.     end;
  28.   end;
  29.  
  30. begin
  31.   Writeln('Analytic integral of exp(-x^2) from -inf to +inf:');
  32.   Writeln(
  33.     RunMaximaOnce('integrate(exp(-x^2), x, minf, inf)')
  34.     );
  35.   Writeln;
  36.  
  37.   Writeln('Numeric value sqrt(pi) with 100 digits:');
  38.   Writeln(
  39.     RunMaximaOnce('linel:110$ fpprec:100$ bfloat(sqrt(%pi))')
  40.     );
  41.  
  42.   Readln;
  43. end.
  44.  

Code: Text  [Select][+][-]
  1. Analytic integral of exp(-x^2) from -inf to +inf:
  2. integrate(exp(-x^2),x,minf,inf)
  3.                                    sqrt(%pi)
  4. quit()
  5.  
  6. Numeric value sqrt(pi) with 100 digits:
  7. linel:110
  8. fpprec:100
  9. bfloat(sqrt(%pi))
  10.    1.772453850905516027298167483341145182797549456122387128213807789852911284591032181374950656738544665b0
  11. quit()
  12.  

 

TinyPortal © 2005-2018