Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 04 Mar 2004 03:45:44 +1030
From:      Wayne Sierke <ws+freebsd-questions@au.dyndns.ws>
To:        Dan Nelson <dnelson@allantgroup.com>
Cc:        freebsd-questions@freebsd.org
Subject:   Re: Size of variables in awk
Message-ID:  <1078334144.666.179.camel@ovirt.dyndns.ws>
In-Reply-To: <20040303161840.GD79860@dan.emsphone.com>
References:  <1078286126.666.11.camel@ovirt.dyndns.ws> <20040303080359.GB79860@dan.emsphone.com> <1078304560.666.157.camel@ovirt.dyndns.ws> <20040303161840.GD79860@dan.emsphone.com>

next in thread | previous in thread | raw e-mail | index | archive | help
On Thu, 2004-03-04 at 02:48, Dan Nelson wrote:
> In the last episode (Mar 03), Wayne Sierke said:
> > On Wed, 2004-03-03 at 18:34, Dan Nelson wrote:
> > > In the last episode (Mar 03), Wayne Sierke said:
> > > > It seems I've run into the 32-bit signed number wall in awk
> > > > (5.2-RELEASE).
> > > > 
> > > > My totals are maxing out at 2147483648.
> > > > 
> > > > Would anyone happen to know whether that's really the case (that awk is
> > > > only implemented with 32-bit number capability - unfortunately I don't
> > > > have any other awks nearby to verify nor can I find any reference info
> > > > that indicates) and/or can suggest a way around it?
> > > 
> > > Seems to works fine on -current:
> > > 
> > > $ jot 8 30 | awk '{ print 2^$1 }'
> > 
> > Ah, ok. Same for me on 5.2-RELEASE. More info:
> > 
> > I'm using the printf function in awk but something ain't right:
> > 
> > # jot 4 30 | awk '{ printf("%u\n", 2^$1-1) }'
> > 2147483648
> > 
> > # jot 4 30 | awk '{ printf("%lu\n", 2^$1-1) }'
> > 2147483648
> > 
> > # jot 4 30 | awk '{ printf("%llu\n", 2^$1-1) }'
> > 35186519572480
> 
> I see nothing wrong here.  %u is an unsigned int, and on x86 systems,
> an int is 32 bits.  %llu is a long long unsigned int, and they are 64
> bits.  Since there is no way for C to print a number larger than 64
> bits, you won't be able to use the numeric specifiers to print large
> numbers.  You can use %s though.  See
> /usr/src/contrib/one-true-awk/run.c, the format() function.

When you say "they are 64 bits" you're referring to a long
(signed/unsigned) int (not long long unsigned int)?

In which case aren't there two problems with the results shown?

1 - %u should print values up to 2^32-1

2 - %lu should print values up to 2^64-1

whereas they're both hitting a limit at 2^31.



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?1078334144.666.179.camel>