develooper Front page | perl.perl5.porters | Postings from October 2003

Re: [perl #24091] Not OK: perl v5.8.1 on sgi6-irix-ld 6.5

From:
Ed Allen Smith
Date:
October 7, 2003 13:04
Subject:
Re: [perl #24091] Not OK: perl v5.8.1 on sgi6-irix-ld 6.5
Message ID:
mid+200310072004.h97K4UU71131236@dogberry.rutgers.edu
In message <200310072046.47703@bloodgate.com> (on 7 October 2003 20:53:06
+0200), perl_dummy@bloodgate.com (Tels) wrote:
>I am now between a rock and a hard place:

Sigh... yeah.

>POSIX::floor () converts the 81 as result to 80, as does int(). 
>sprinf("%.0f"), OTOH, converts this fine to 81, but it
>fails for things like 9.91234 - this should be truncated to 9, but sprintf
>rounds it to 10. 
>
>Here is a case where the new sprintf("%.0f") fails, but the former int()
>works:
>
>	# perl -Mbigint -le 'print 9999->broot()'
>	99
>	# perl -Ilib -Mbigint -le 'print 9999->broot()'
>	100
>
>And here is one where the old one fails, but the new one works:
>
>	# perl -Mbigint -le 'print +(81 ** 3)->broot(3)'
>	80
>	# perl -Ilib -Mbigint -le 'print +(81 ** 3)->broot(3)'
>	81
>
>:-(

Yes.

>Hm, Paul Johnson <paul@pjcj.net> wrote:
>
>>Because (81 ** 3) ** (1/3) != 81
>>
>>$ perl -le 'print ((81 ** 3) ** (1/3) != 81)'
>>1
>>
>>$ perl -MPOSIX -le '$a = (81 ** 3) ** (1/3); print join(" ", $a, int ($a),
>>POSIX::floor($a), sprintf("%f",$a), sprintf("%.20f",$a), sprintf
>>("%.0f",$a))'
>>81 80 80 81.000000 80.99999999999998578915 81
>
>Argl.

Quite.

>So, if I first round the result to 8 digits after the ".", I get rid of the
>small variances, and can the truncate the result properly? Or should I just 
>add 0.5?

I wouldn't add a full 0.5, if one wants consistency with previous results
when said previous results were correct or when said previous results were
for cases where using algebra wouldn't give you integers anyway. Some
smaller epsilon seems preferable.

>Decisions....
>
>      $x->[0] = int ( sprintf ("%.8f", $x->[0] ** (1 / $n->[0]) ) );
>
>This seems to work for both cases on my system.....

Although there are problems with sprintf rounding with long doubles on some
systems (irix and others; there's a check for this in the irix hints file
that turns on a workaround via -DHAS_LDBL_SPRINTF_BUG, but it's not a 100%
fix by any means). I'm thinking the epsilon addition I mentioned in my email
to you, or the method that Yitzchak Scott-Thoennes mentioned, would be
better.

    -Allen

-- 
Allen Smith                       http://cesario.rutgers.edu/easmith/
February 1, 2003                               Space Shuttle Columbia
Ad Astra Per Aspera                     To The Stars Through Asperity



nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About