On Tue, 2003-07-22 at 16:36, Erik Price wrote: > Cole Tuininga wrote: > > Got a perl question for y'all. I rarely have to do anything with perl, > > and I'm sure perl has a good reason for behaving like the following, but > > heck if I can figure it out. > > > > The perl cookbook suggestions using sprintf for rounding floats. This > > seemingly works fine: > > > > [EMAIL PROTECTED]:~$ perl -e 'print sprintf( "%.2f\n", 0.562 )' > > 0.56 > > [EMAIL PROTECTED]: perl -e 'print sprintf( "%.2f\n", 0.567 )' > > 0.57 > > > > However, I'm extremely confused by the following: > > > > [EMAIL PROTECTED]:~$ perl -e 'print sprintf( "%.2f\n", 0.565 )' > > 0.56 > > I don't have an answer, Cole, but the same happens in Python and I'd bet > in C as well -- must be a convention of the printf routines: > > Python 2.2.2 (#1, Mar 9 2003, 08:18:26) > [GCC 3.2 20020927 (prerelease)] on cygwin > Type "help", "copyright", "credits" or "license" for more information. > >>> print "%.2f\n" % 0.562 > 0.56 > > >>> print "%.2f\n" % 0.567 > 0.57 > > >>> print "%.2f\n" % 0.565 > 0.56
Aha! Google for "round to even" -- it's an IEEE floating point computation rule. _______________________________________________ gnhlug-discuss mailing list [EMAIL PROTECTED] http://mail.gnhlug.org/mailman/listinfo/gnhlug-discuss