win32 vs linux (bignum)

M

Matija Papec

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 }
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) );
 
S

sisyphus

I get different results when running same algorithm on different
platforms. What went so terribly wrong here? :)

You used bignum - that's what "went wrong" :)

Actually - I haven't yet worked out exactly what *did* go wrong. It's
not just Strawberry Perl, but any Win32 perl (including ActivePerl)
produces the same "approximate" result - despite the fact that it's
the same version of both bignum and Math::BigInt on both Win32 and
linux.

Afaict, both $brojnik and $nazivnik (is that a political symbol ?) are
calculated to the same value on both platforms, but it's the division
($brojnik/$nazivnik) that creates the difference.

Taking a Devel::peek::Dump() of the returned value ($brojnik/
$nazivnik) reveals some different field settings between linux and
win32 - which might be worth investigating.
It should also be possible to create a simpler demo of the problem -
but I failed in that quest, too :)

This is definitely worthy of a bug report - though a simpler demo for
that bug report would be preferable.

Cheers,
Rob
 
T

turkish

sisyphus said:
You used bignum - that's what "went wrong" :)
:)

Afaict, both $brojnik and $nazivnik (is that a political symbol ?) are

No, these are another terms for numerator and denominator. :)
calculated to the same value on both platforms, but it's the division
($brojnik/$nazivnik) that creates the difference.

Yes, and it seems that problem goes beyond linux vs windows.
Dividing same large numbers using python/java gives correct result while
perl/ruby/php are wrong regardless of the OS.
Taking a Devel::peek::Dump() of the returned value ($brojnik/
$nazivnik) reveals some different field settings between linux and
win32 - which might be worth investigating.
It should also be possible to create a simpler demo of the problem -
but I failed in that quest, too :)

This is definitely worthy of a bug report - though a simpler demo for
that bug report would be preferable.

use bignum;
my ($n, $d) = <DATA>;
chomp($n, $d);
print $n/$d;

__DATA__
40238726007709377354370243392300398571937486421071463254379991042993851239862902059204420848696940480047998861019719605863166687299480855890132382966994459099742450408707375991882362772718873251977950595099527612087497546249704360141827809464649629105639388743788648733711918104582578364784997701247663288983595573543251318532395846307555740911426241747434934755342864657661166779739666882029120737914385371958824980812686783837455973174613608537953452422158659320192809087829730843139284440328123155861103697680135730421616874760967587134831202547858932076716913244842623613141250878020800026168315102734182797770478463586817016436502415369139828126481021309276124489635992870511496497541990934222156683257208082133318611681155361583654698404670897560290095053761647584772842188967964624494516076535340819890138544248798495995331910172335555660213945039973628075013783761530712776192684903435262520001588853514733161170210396817592151090778801939317811419454525722386554146106289218796022
383897147608850627686296714667469756291123408243920816015378088989396451826324367161676217916890977991190375403127462228998800519544441428201218736174599264295658174662830295557029902432415318161721046583203678690611726015878352075151628422554026517048330422614397428693306169089796848259012545832716822645806652676995865268227280707578139185817888965220816434834482599326604336766017699961283186078838615027946595513115655203609398818061213855860030143569452722420634463179746059468257310379008402443243846565724501440282188525247093519062092902313649327349756551395872055965422874977401141334696271542284586237738753823048386568897646192738381490014076731044664025989949022222176590433990188601856652648506179970235619389701786004081188972991831102117122984590164192106888438712185564612496079872290851929681937238864261483965738229112312502418664935314397013742853192664987533721894069428143411852015801412334482801505139969429015348307764456909907315243327828826986460278986432113908350
6217095002597389863554277196742822248757586765752344220207573630569498825087968928162753848863396909959826280956121450994871701244516461260379029309120889086942028510640182154399457156805941872748998094254742173582401063677404595741785160829230135358081840096996372524230560855903700624271243416909004153690105933983835777939410970027753472000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
14887338741396604108836218987068397819515734169330307832552614964463631525147056089659539353020968301885437350481455158516435403120762983029708143341331125565290146112787030472070558647314210247036525976597784518092962753931708751743427749813657515530486192136342088200123427578504060322268353183572626247684288099604317729196932385458475870787960678089474805708323607303725130632023014879405880088044425973858266587929771882570002658468366509416402778571870881449528235149699570523837807193374172499257253080329249482755483689253643954520965116467976006881581562502101250730317449718557752552479483242397797858641026386246065305342383763338299798804251426068321399476365271733915649901592715774415472458296798952361296871772675924137165979347968382886768388509048400469844428499523227069062942721623939098937588340238777157515568891409171768985250095842525138027339737717540062795966278680090918275933243483396153221137907390132418600198174540107671636662142790982001042896637128055202939
683626448277265970382094413684209458162026605677549391645057402598469869530573463082258684211076188260727348872289588911568206112878444837409973091425945918369740745707209513812281716958724110851326071095151377636895662101661415015659862946356501206391326713122836788609529711241312058219893887204378865996340580885186675061657006875734884482244425305903813716473827156698957576512329604792763641150051463059212316087778601478651563197581218398997460441030535693709023282770248260738731169939793856526818637224852114356256743074517095933572917787115242437958826232003607322617412997607082434068594882266482291534379837634370964375348040273367085219398753790481822034810177719602236505565210811303560690642065746922411180102378946048721520239612733413047730989068621588697781037337647167385696944081157256721257664363581037586646971667003754609168413277462354155210048777002439363830584897240493890798948838108607229018130353379262326268822714886836557184096243603337010655180334599435813695
05321722720193665102822583486757396388249600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000


Hm.. perhaps using some example with external text file? :)
 
J

Jürgen Exner

Matija Papec said:
I get different results when running same algorithm on different
platforms. What went so terribly wrong here? :)
return $brojnik / $nazivnik;

Your problem is that you are using floating point arithmetic in the line
above.
As you know from computer numerics floating point arithmetic is
inherently problematic and very much system dependent.

jue
 
S

sln

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

^^^^^^^^^^^^^^
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,144
Latest member
KetoBaseReviews
Top