Re: bignum with floating point



On 24 Apr., 12:04, Richard Heathfield <r...@xxxxxxxxxxxxxxx> wrote:
Logan Shaw said:

Richard Heathfield wrote:
If you have bignums, you can use rationals instead. A rational is
basically two integers. So to store 1/3, you wouldn't bother with the
hopelessly inaccurate "0.333333333333" - instead, you store the
precisely correct value { "1", "3" }

This seems useful in a lot of situations, but there may be situations
where floating point would be better.

There may be situations where you are doing a very long series of
computations and you wish to have an upper bound on the storage
requirements.

On looking back, I see that I over-egged the precision part. Clearly you
would have to drop some precision eventually!

For example, suppose you are computing a million terms of the series

4/1 - 4/3 + 4/5 - 4/7 + 4/9 ...

This is not a good formula to compute PI. Even with millions of terms
you just get few digits. There is a better formula from Bailey,
Borwein
and Plouffe which is used in the following program:

$ include "seed7_05.s7i";
include "bigint.s7i";
include "bigrat.s7i";

# In 1997, David H. Bailey, Peter Borwein and Simon Plouffe published
a
# paper (Bailey, 1997) on a new formula for PI as an infinite series:

# PI = sum_n_from_0_to_inf(16 ** (-n) *
# (4 / (8 * n + 1) - 2 / (8 * n + 4) - 1 / (8 * n + 5) - 1 / (8 *
n + 6)));

const func bigRational: compute_pi is func
result
var bigRational: sum is 0_ / 1_;
local
var integer: n is 0;
var bigInteger: k is 0_;
begin
for n range 0 to 825 do
k := bigInteger conv n;
sum +:= 1_ / 16_ ** n *
(4_ / (8_ * k + 1_) - 2_ / (8_ * k + 4_) -
1_ / (8_ * k + 5_) - 1_ / (8_ * k + 6_));
end for;
end func;

const proc: main is func
begin
writeln(compute_pi digits 1000);
end func;
-------- End of example program --------

You can see that PI is computed as big rational number with
up to 1000 decimal digits.

Greetings Thomas Mertes

Homepage: http://seed7.sourceforge.net
Project page: http://sourceforge.net/projects/seed7

.