rvelthuis / DelphiBigNumbers

BigInteger and BigDecimal for Delphi
http://rvelthuis.de/programs/bigintegers.html
BSD 2-Clause "Simplified" License
119 stars 62 forks source link

BigDecimal.Sqrt always gets divide by zero error. #2

Closed chesterw closed 7 years ago

chesterw commented 7 years ago

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.

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 < 0.0)

This seems to work for me.

rvelthuis commented 7 years ago

I added your suggestion (well, by checking for Result.IsZero) and found out that FHalf and FOneTenth were not initialized. Sqrt() uses Half. This used to be a local variable, but I decided to make it a property. Just forgot to initialize it and OneTenth. Added those and now it seems to work. Thanks for the heads-up!

chesterw commented 7 years ago

Thank you so much! It works fine now. I have used BigIntegers and BigDecimals units for some time now: Some programs: prime numbers and sums of factors databases out to hundreds of millions with remarkable speed. Again, thank you.

On Fri, Apr 14, 2017 at 2:23 PM, Rudy Velthuis notifications@github.com wrote:

I added your suggestion (well, by checking for Result.IsZero) and found out that FHalf and FOneTenth were not initialized. Sqrt() uses Half. This used to be a local variable, but I decided to make ti a property. Just forgot to initialize it and OneTenth. Added those and now it seems to work. Thanks for the heads-up!

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/rvelthuis/BigNumbers/issues/2#issuecomment-294207775, or mute the thread https://github.com/notifications/unsubscribe-auth/AZhxSdXczHx3UbRTGDuWRIcYCnVfjMxeks5rv7mGgaJpZM4MzFPN .