Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

awk math results differ from other languages

6 views
Skip to first unread message

rick

unread,
Nov 28, 2007, 1:26:22 PM11/28/07
to
Does anyone know why awk doesn't come up with the same result as other
languages for this equation? Bash, zsh, python, perl, etc. all give
the same answer. Awk gives a different answer.

$ echo "111111111 * 111111111" | bc -l
12345678987654321

$ perl -le 'print 111111111 * 111111111'
12345678987654321

$ python -c "print 111111111 * 111111111"
12345678987654321

(bash/zsh - same results)
$ echo $((111111111 * 111111111))
12345678987654321

$ awk 'BEGIN {printf "%.0f", '"111111111 * 111111111"'}'
12345678987654320

Ed Morton

unread,
Nov 28, 2007, 1:50:39 PM11/28/07
to

Awk uses double-precision floating-point numbers to represent all numeric values
so not all numeric values can be represented exactly. See
http://www.gnu.org/software/gawk/manual/gawk.html#Floating-Point-Issues.

Ed.

gerryt

unread,
Nov 29, 2007, 9:55:48 AM11/29/07
to
On Nov 28, 10:50 am, Ed Morton <mor...@lsupcaemnt.com> wrote:
> On 11/28/2007 12:26 PM, rick wrote:
> > Does anyone know why awk doesn't come up with the same result as other
> > languages for this equation? Bash, zsh, python, perl, etc. all give
> > the same answer. Awk gives a different answer.
Because you asked a different question of awk???

> > $ echo "111111111 * 111111111" | bc -l
> > 12345678987654321
> > $ perl -le 'print 111111111 * 111111111'
> > 12345678987654321
> > $ python -c "print 111111111 * 111111111"
> > 12345678987654321
> > (bash/zsh - same results)
> > $ echo $((111111111 * 111111111))
> > 12345678987654321
> > $ awk 'BEGIN {printf "%.0f", '"111111111 * 111111111"'}'
> > 12345678987654320
> Awk uses double-precision floating-point numbers to represent all numeric values
> so not all numeric values can be represented exactly. Seehttp://www.gnu.org/software/gawk/manual/gawk.html#Floating-Point-Issues.
> Ed.

I think what gets me about these so called examples is the OP
ASKED for a floating point result but only with awk : <
perl -le 'printf "%.0f", 111111111 * 111111111'
gives the same result as awk does..
And in this particular case
awk 'BEGIN {print (111111111*111111111)}'
gives the "right" answer.

Edward Morton

unread,
Nov 29, 2007, 10:10:32 AM11/29/07
to

That depends on which awk, which OS, etc.:

on cygwin on Windows XP:

$ gawk 'BEGIN {print (111111111*111111111); exit}'
12345678987654320

on Solaris:

$ /usr/bin/awk 'BEGIN {print (111111111*111111111); exit}'
12345678987654320
$ nawk 'BEGIN {print (111111111*111111111); exit}'
12345678987654320
$ /usr/xpg4/bin/awk 'BEGIN {print (111111111*111111111); exit}'
12345678987654321
$ gawk 'BEGIN {print (111111111*111111111); exit}'
1.23457e+16
$ gawk 'BEGIN {printf "%.0f\n", (111111111*111111111); exit}'
12345678987654320

Regards,

Ed.

gerryt

unread,
Nov 29, 2007, 11:42:16 AM11/29/07
to
> Ed.

Indeed and thanks - I put the word right in quotes for a reason : >
My test was on Solaris too where I alias awk to /usr/xpg4/bin/awk

0 new messages