Perl help
Stephen Ryan
stephen.p.ryan at dartmouth.edu
Tue Jul 22 16:52:28 EDT 2003
On Tue, 2003-07-22 at 16:41, Stephen Ryan wrote:
> 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:
> > >
> > > egon at trogdor:~$ perl -e 'print sprintf( "%.2f\n", 0.562 )'
> > > 0.56
> > > egon at trogdor: perl -e 'print sprintf( "%.2f\n", 0.567 )'
> > > 0.57
> > >
> > > However, I'm extremely confused by the following:
> > >
> > > egon at trogdor:~$ 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.
Errm. I take it back.
$ perl -e 'print sprintf( "%.2f\n", 0.575 )'
0.57
Dunno what's up with that.
More information about the gnhlug-discuss
mailing list