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

Thread Previous | Thread Next
From:
Yitzchak Scott-Thoennes
Date:
October 7, 2003 12:21
Subject:
Re: [perl #24091] Not OK: perl v5.8.1 on sgi6-irix-ld 6.5
Message ID:
20031007192017.GA992@efn.org
On Tue, Oct 07, 2003 at 08:53:06PM +0200, Tels <perl_dummy@bloodgate.com> wrote:
> 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. 

sprintf is supposed to round, not truncate (though "in an
implementation-defined manner").

> 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

If you want to truncate, but allow slight wiggle-room for error in the
lowest 1-3 bits, try something like:

$result = "$result" if $result !~ /./;

or use an explicit ratio-to-nearest-integer check:

$result = floor($result+.5) if abs($result/floor($result+.5)) < 1e-14

Thread Previous | Thread Next


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