Forum > General
[Solved] gmp unit problem (arbitrary precision arithmetic)
HappyLarry:
I have been using the gmp unit and have a reasonable understanding of it.
I have used the floating point 'sqrt' function and it has worked correctly up until now. However when I try to find root of
1247257156019619631959228850474915324796444794921068388944443347008749568000000000000000
I get the answer
35316528085580830944581390125742259962343219.93...
The correct answer is
35316528085580830944581390125749276564383499.14....
I have set the precision as 4*(number of digits in the original number). I have noticed that the error occurs on the 32nd digit and 32*4 = 128 which may be important, but I don't know why nor how to get around it.
Any ideas would be welcome.
HappyLarry:
Here is the procedure which might help).
procedure sqrt2(var strAnswer:string; var Expon:integer; strNum1: string; NumPlaces:integer);
var
a,b: MPFloat;
n:integer;
p:mp_exp_t;
begin
f_init (a);
f_init (b);
f_set_str (a, '1', 10);
f_set_str (b, strNum1, 10);
p:=4*NumPlaces;
f_set_prec (a, p);
f_sqrt (a,b);
strAnswer:=f_get_str(n, 10, p, a);
Expon:=n;
f_clear (a);
f_clear (b);
end;
marcov:
Precision maybe binary based (number of bits) instead of decimal ?
HappyLarry:
It is binary-based but I have assumed that the number of bits needed to represent a number in decimal will be 4*(number of decimal digits).
e.g
any 1 digit number can be represented in 4 bits since 24 = 16
any 2 digit number can be represented in 8 bits since 28 = 256
any 3 digit number can be represented in 12 bits since 212 = 4096
any 4 digit number can be represented in 16 bits since 216 = 65536
etc.
That explains why I have set the precision as 4*(number of digits).
marcov:
--- Quote from: HappyLarry on January 18, 2015, 06:13:50 pm ---It is binary-based but I have assumed that the number of bits needed to represent a number in decimal will be 4*(number of decimal digits).
--- End quote ---
Ok. Then I don't know :-)
(it is 3.322 btw, log 10 / log 2, which comes down to slightly over 106 bits for 32 digits.
Navigation
[0] Message Index
[#] Next page