[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [bug-gawk] Bug? - print int(100*32.98)
From: |
arnold |
Subject: |
Re: [bug-gawk] Bug? - print int(100*32.98) |
Date: |
Tue, 04 Jun 2019 00:37:13 -0600 |
User-agent: |
Heirloom mailx 12.5 7/5/10 |
Glad to have helped.
Take it easy,
Arnold
Robert Brandtner <address@hidden> wrote:
> 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
> >