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-14Thread Previous | Thread Next