Dominique Blas wrote:
> Sorry,
> 
> I've found the following bug in mod_perl 1.25 and Perl 5.6.1.
> Maybe was it already released  but in case not here it is :
> 
> perl -e '
> $a=0.57;
> print sprintf("%03d", $a * 100)'
> 
> prints "056" instead of "057"
> 
> Same behaviour with $a=0,58 that prints "057".
> 
> Of course it works for other values and also if you write 
> 
> $a=0,57*100;
> print sprintf("%03d", $a);
> 
> 
> Bug in sprintf() ?

No, this is not a bug. %d is the same as int(). Consider:

% perl -le '$a=0.57; print int ($a*100)'
56

Now read the manpage:

        int EXPR
        int     Returns the integer portion of EXPR.  If EXPR is
                omitted, uses "$_".  You should not use this func­
                tion for rounding: one because it truncates
                towards "0", and two because machine representa­
                tions of floating point numbers can sometimes pro­
                duce counterintuitive results.  For example,
                "int(-6.725/0.025)" produces -268 rather than the
                correct -269; that's because it's really more like
                -268.99999999999994315658 instead.  Usually, the
                "sprintf", "printf", or the "POSIX::floor" and
                "POSIX::ceil" functions will serve you better than
                will int().

this explains the problem.

__________________________________________________________________
Stas Bekman            JAm_pH ------> Just Another mod_perl Hacker
http://stason.org/     mod_perl Guide ---> http://perl.apache.org
mailto:[EMAIL PROTECTED] http://use.perl.org http://apacheweek.com
http://modperlbook.org http://apache.org   http://ticketmaster.com

Reply via email to