New: BigDecimals

Posted by on in Blogs
After BigIntegers, the next logical step was the implementation of BigDecimals. I implemented them using BigIntegers, because a BigDecimal is not much more than a BigInteger divided (or multiplied) by - scaled by - a power of 10.

Unlike BigIntegers, my implementation of BigDecimals does not use any assembler, so it should be usable anywhere BigIntegers can be used.

The BigDecimals are mostly modelled after the interface of the BigDecimal class in Java, but, like BigIntegers, made more Delphi-like. Java BigDecimals do not support operator overloading, while my BigDecimals do. This makes the handling of rounding and precision a little different too.

Anyway, I think that every Delphi should not only have BigIntegers, but BigDecimals too. Have fun with them. They can be found in the same .zip file as BigIntegers. Just read more about them on my website.

The next step is BigDecimal math, like trigonometric, exponential functions etc. One other step will be to host this on GitHub. I am also thinking of writing prime, factorization and other mathematically useful functions for BigIntegers.


Comments

  • Walter Chester
    Walter Chester Tuesday, 13 December 2016

    I love Bigintegers and BigDecimals!
    I can't get BigDecimal.Sqrt to work: always get divide by zero error. Here is my humble attempt:
    A := '123.456';
    writeln('>> 1 ',A.ToPlainString);
    B := BigDecimal.Sqrt(A);
    writeln('>> 2');
    --- Of course A and B are type BigDecimal. I must be doing something wrong. The same code for BigInteger works fine. My email is [email protected]

  • Walter Chester
    Walter Chester Wednesday, 14 December 2016

    After a little digging:
    I changed Veltius.BigDecimals.pas, function BigDecimal.Sqrt(Precision: Integer): BigDecimal;
    from:
    while (Result * Result - Self).Abs >= Epsilon do
    to:
    while ((Result * Result - Self).Abs >= Epsilon) and (Result

  • Walter Chester
    Walter Chester Wednesday, 14 December 2016

    It got cut off...
    to:
    while ((Result * Result - Self).Abs >= Epsilon) and (Result

  • Walter Chester
    Walter Chester Wednesday, 14 December 2016

    OK, so it's an HTML thing...
    while ((Result * Result - Self).Abs >= Epsilon) and (Result LessThan 0.0)

  • Please login first in order for you to submit comments
  • Page :
  • 1

Check out more tips and tricks in this development video: