[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [bug-gawk] Bug? - print int(100*32.98)
From: |
Robert Brandtner |
Subject: |
Re: [bug-gawk] Bug? - print int(100*32.98) |
Date: |
Mon, 3 Jun 2019 21:02:54 +0200 |
Hello Andrew, hello Arnold
Thanks a lot for your fast response and the clear explanation.
So it's in the end the difference between our human decimal representation
of numbers and the binary representation in a computer.
It works as designed and is as it is.- No bug. I was simply fooled by my
expectation and the ignorance of the technical background.
Thanks
Kind regards
Robert
Am Mo., 3. Juni 2019 um 15:20 Uhr schrieb Andrew J. Schorr <
address@hidden>:
> 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
>