in reply to Re: Calculation discrepancy between Perl versions
in thread Calculation discrepancy between Perl versions

Thanks, all, for the very useful responses (I'm responding to BrowserUk's reply specifically because it's so detailed and helpful in so many aspects) - this was very useful for both confirmation and more direction in deciding where to look for the error. I'm not all that familiar with the guts of Perl, but it's looking likely that there's a major compilation difference responsible here: either single-precision lookup tables, or perhaps a radically different math lib. Annoying that something like that could affect a Perl program... but on the other hand, good to learn that it can.

Again, thank you all very much.


--
"Language shapes the way we think, and determines what we can think about."
-- B. L. Whorf
  • Comment on Re^2: Calculation discrepancy between Perl versions

Replies are listed 'Best First'.
Re^3: Calculation discrepancy between Perl versions
by BrowserUk (Patriarch) on Oct 05, 2010 at 17:17 UTC
    either single-precision lookup tables, or perhaps a radically different math lib. Annoying that something like that could affect a Perl program...

    I'm very unsure of my ground here as I'm not familiar with the other platforms, but it might not be software--Perl or the libs--but the floating point hardware. If their FPU's are only single precision, that might account for the results.

    That said, my best efforts to perform the calculation in single precision doesn't get close to the inaccuracies you;re seeing:

    #include <stdio.h> float sqrtf( float n ) { float g = n / (float)2.0; while( ( ( g*g ) - n ) > 0.000001 ) { g = ( g + n/g ) / (float)2.0; } return g; } void main( void ) { float a = (float)0.01125, b = (float)-43.22, c = (float)41173.3950 +0009608; float two = 2.0, four = 4.0; float b2 = b * b; float ac4 = four * a * c; float sqrtbit = sqrtf( b2 - ac4 ); float a2 = two * a; float r1 = ( -b + sqrtbit ) / a2; float r2 = ( -b - sqrtbit ) / a2; printf( "b2:%f ac4:%f sqrt:%f a2:%f\n", b2, ac4, sqrtbit, a2 ); printf( "roots: %f , %f\n", r1, r2 ); }

    Produces:

    C:\test>quads b2:1867.968506 ac4:1852.802856 sqrt:3.894310 a2:0.022500 roots: 2093.969238 , 1747.808472

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.