I get different results when running same algorithm on different
platforms. What went so terribly wrong here?
linux debian v5.10
result:
270288240945436551019305908463402626133344424635003137400602091930237777421360473851797325724922730460543802875678603168471867361078540080081808473288115110060581000433119131429793939577717447508918397345219433071259594386723113371250852722071254367796680880783950950640887679135140232038177361100800
win32 strawberry v5.10
result:
270288240945436550000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
==================
use bignum;
sub f {
my ($n) = @_;
my $f = 1;
while ($n > 1) {
$f *= $n;
$n -= 1;
}
return $f;
}
sub p {
my ($n,$k) = @_;
if ($n == $k or $k == $0) { return 1 } ^
error
elsif ($n < $k) { return 0 }
else {
my $brojnik = f($n);
my $nazivnik = f($k)*f($n-$k);
# print "$brojnik\n$nazivnik\n";
return $brojnik / $nazivnik;
}
}
# my ($x,$y) = @ARGV;
my ($x,$y) = (1000, 500);
printf ("%.f", p($x,$y) );
^^^^^^
What does print p($x,$y),"\n"; give you?
I get this with 5.8.8 Perl (but this is just a float
to string conversion):
$inv1 = ($nazivnik / $brojnik);
$inv2 = ($brojnik / $nazivnik);
$ratio = $inv1 * $inv2;
print "\nratio = $ratio\n\n";
ratio = 1.0000000000000000000000000000000000000001449960144929226517452630725078430893586
^
40 decimal places
I'm wondering if 40 decimal places is enough precision for you.
I think taking PI as an example of precision is pretty good.
In analytic physical science's, math, probability/statics,
where pi is used extensively, if you can't do the inverse, ie; solve pi to 40
decimal places, then you don't need 40 decimal places. I'm not talking number
series/period used specifically to roll out the next digit of pi.
Because the physical reality terms are not accurate to do the inverse,
you can use PI to 1 trillion digits in physical/math equations using
intermediate results, itterating for hundreds of thousands of years, but this:
1.0000000000000000000000000000000000000001449960144929226517452630725078430893586
is really 1, always was, always will be.
Read the first and last paragraphs below.
sln
-------------------------------------------
PI:
While the value of pi has been computed to more than a trillion (10 ** 12) digits,
elementary applications, such as calculating the circumference of a circle,
will rarely require more than a dozen decimal places.
For example, a value truncated to 11 decimal places is accurate enough to
calculate the circumference of the earth with a precision of a millimeter,
and one truncated to 39 decimal places is sufficient to compute the circumference
of any circle that fits in the observable universe to a precision comparable
to the size of a hydrogen atom.
Calculating p:
p can be empirically estimated by drawing a large circle, then measuring its
diameter and circumference and dividing the circumference by the diameter.
Another geometry-based approach, due to Archimedes, is to calculate the perimeter, Pn,
of a regular polygon with n sides circumscribed around a circle with diameter d. Then
p = lim(n->infinity) of Pn/d
That is, the more sides the polygon has, the closer the approximation approaches p.
Archimedes determined the accuracy of this approach by comparing the perimeter of the
circumscribed polygon with the perimeter of a regular polygon with the same number of
sides inscribed inside the circle. Using a polygon with 96 sides, he computed the
fractional range:
3-10/17 < pi < 3-1/7
p can also be calculated using purely mathematical methods. Most formulas used
for calculating the value of p have desirable mathematical properties, but are
difficult to understand without a background in trigonometry and calculus.
However, some are quite simple, such as this form of the Gregory-Leibniz series: ...
While that series is easy to write and calculate, it is not immediately obvious why it yields p.
In addition, this series converges so slowly that 300 terms are not sufficient to calculate p
correctly to 2 decimal places. However, by computing this series in a somewhat more
clever way by taking the midpoints of partial sums, it can be made to converge much faster.
....
And on and on with simulated series/periods, supercomputers, years of calculations.
Now for the good stuff. What uses PI?
---------------------------------------------
Use in mathematics and science:
Mathematics ..
pi is ubiquitous in mathematics, appearing even in places that lack an obvious
connection to the circles of Euclidean geometry.
Higher analysis and number theory
The frequent appearance of pi in complex analysis can be related to the behavior
of the exponential function of a complex variable, described by Euler's formula.
[snip] .. A consequence is that the gamma function of a half-integer is a rational multiple of vp.
Physics ..
Although not a physical constant, pi appears routinely in equations describing
fundamental principles of the Universe, due in no small part to its relationship
to the nature of the circle and, correspondingly, spherical coordinate systems.
- The cosmological constant.
- Heisenberg's uncertainty principle, which shows that the uncertainty in the
measurement of a particle's position (?x) and momentum (?p) can not both be
arbitrarily small at the same time.
- Einstein's field equation of general relativity.
- Coulomb's law for the electric force, describing the force between two electric
charges (q1 and q2) separated by distance r.
- Magnetic permeability of free space.
- Kepler's third law constant, relating the orbital period (P) and the semimajor
axis (a) to the masses (M and m) of two co-orbiting bodies.
Probability and statistics ..
In probability and statistics, there are many distributions whose formulas contain pi, including:
- The probability density function for the normal distribution with mean µ and standard deviation s,
due to the Gaussian integral.
- The probability density function for the (standard) Cauchy distribution.
Note that since for any probability density function f(x), the above formulas can be used
to produce other integral formulas for pi.
Buffon's needle problem is sometimes quoted as a empirical approximation of pi in
"popular mathematics" works. Consider dropping a needle of length L repeatedly on
a surface containing parallel lines drawn S units apart (with S > L). If the needle
is dropped n times and x of those times it comes to rest crossing a line (x > 0),
then one may approximate pi using the Monte Carlo method
Though this result is mathematically impeccable, it cannot be used to determine more than
very few digits of pi by experiment. Reliably getting just three digits (including the initial "3")
right requires millions of throws, and the number of throws grows exponentially with the number
of digits desired. Furthermore, any error in the measurement of the lengths L and S will transfer
directly to an error in the approximated pi. For example, a difference of a single atom in the
length of a 10-centimeter needle would show up around the 9th digit of the result.
In practice, uncertainties in determining whether the needle actually crosses a line when
it appears to exactly touch it will limit the attainable accuracy to much less than 9 digits
^^^^^^^^^^^^^^