[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: How are numbers printed in gawk?
From: |
Wolfgang Laun |
Subject: |
Re: How are numbers printed in gawk? |
Date: |
Wed, 18 Mar 2020 12:08:10 +0100 |
The question how gawk (or any other language) determines whether a floating
point number represents an integer value would have been much more
interesting. I've found that the (Java) code snippet
boolean isInteger( double x ) {
if( x < Long.MIN_VALUE || Long.MAX_VALUE < x ) return false; // or
Integer.M*_VALUE
return x == Math.floor( x );
}
provides a terse answer. As for the implementation of floor, read the GNU C
library source code.
-W
On Wed, 18 Mar 2020 at 09:37, <address@hidden> wrote:
> Peng Yu <address@hidden> wrote:
>
> > Hi,
> >
> > I'd like to write some python code that produces the same results as
> > gawk for numbers. As far as I understand there is no integer type in
> > gawk internal. But if I use float in python for integer, python will
> > print something like 1.0. Therefore, I'd like to know gawk is
> > implemented regarding numbers. Could anybody show the best approach to
> > implement the same method used in awk dealing with numbers in python?
> > Thanks.
> >
> > $ awk 'BEGIN { print .0 + 1 }'
> > 1
> > $ python -c 'print(.0 + 1)'
> > 1.0
> >
> > --
> > Regards,
> > Peng
>
> $ python -c "print('%.0f' % 1.0)"
> 1
>
> Please take any further python questions elsewhere.
>
> Thanks,
>
> Arnold
>
>