[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [bug-gawk] Bug? - print int(100*32.98)
From: |
Andrew J. Schorr |
Subject: |
Re: [bug-gawk] Bug? - print int(100*32.98) |
Date: |
Mon, 3 Jun 2019 09:20:03 -0400 |
User-agent: |
Mutt/1.5.21 (2010-09-15) |
On Mon, Jun 03, 2019 at 07:09:09AM -0600, address@hidden wrote:
> Hi.
>
> Thanks for your note.
>
> Robert Brandtner <address@hidden> wrote:
>
> > Hello,
> >
> > Is the following result a bug or a feature?
> >
> > gawk 'BEGIN{print int(100*32.98)}'
> > => 3297
> >
> > Kind regards
> > Robert Brandtner
>
> It's just the way things are. Gawk uses floating point math. 100 * 32.98
> isn't quite 3298:
>
> $ gawk 'BEGIN{printf("%.17g\n", 100*32.98)}'
> 3297.9999999999995
>
> When that result is handed to int(), it gets rounded down to 3297.
You could also consider using MPFR arithmetic with a higher precision.
For example:
bash-4.2$ gawk -M -v PREC=60 'BEGIN{print int(100*32.98)}'
3298
Regards,
Andy