On Tue, Jul 22, 2003 at 04:19:17PM -0400, Cole Tuininga <[EMAIL PROTECTED]> wrote:
> However, I'm extremely confused by the following:
> 
> [EMAIL PROTECTED]:~$ perl -e 'print sprintf( "%.2f\n", 0.565 )'
> 0.56
> 
> Can anybody explain why this is not rounding up?  Thanks in advance...

First of all, I don't know why the code isn't simply:
    $ perl -e 'printf("%.2f\n", 0.565)'
    0.56

or why perl necessarily needs to be part of the solution:
    $ printf "%.2f\n" 0.565
    0.56
[ The printf command is part of the Single Unix Spec V2 (aka UNIX98) ]

I dug around in the C99 standard, and couldnt't find this referenced
anywhere.  Additionally, I tried playing with more precision, and
turned up the following:
    $ perl -e 'printf "%.19f -> %.2f\n", $_, $_ for qw/0.005 0.015 0.025 0.035 0.045/'
    0.0050000000000000001 -> 0.01
    0.0149999999999999990 -> 0.01
    0.0250000000000000010 -> 0.03
    0.0350000000000000030 -> 0.04
    0.0449999999999999980 -> 0.04

So I think it's all about the precision of the number as stored as in
its binary representation.

-- 
Bob Bell <[EMAIL PROTECTED]>
-------------------------------------------------------------------------
 "Testing shows the presence, not the absence, of bugs."
   -- Edsger W. Dijkstra, University of Texas
_______________________________________________
gnhlug-discuss mailing list
[EMAIL PROTECTED]
http://mail.gnhlug.org/mailman/listinfo/gnhlug-discuss

Reply via email to