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

gawk string to number (bug?)

0 views
Skip to first unread message

Valerio, Danilo

unread,
Dec 9, 2009, 5:59:06 PM12/9/09
to bug-gn...@gnu.org
Dear all,

can someone help me to solve this?

:~$ echo "3671014434790262159" | awk '{print $1}'
3671014434790262159

:~$ echo "3671014434790262159" | awk '{print $1+0}'
3671014434790262272

Best Regards,
Danilo


John Cowan

unread,
Dec 10, 2009, 1:18:41 PM12/10/09
to Valerio, Danilo, bug-gn...@gnu.org
Valerio, Danilo scripsit:

> Dear all,
>
> can someone help me to solve this?
>
> :~$ echo "3671014434790262159" | awk '{print $1}'
> 3671014434790262159

The value of $1 is a string, so you are just asking print to output
that string.

> :~$ echo "3671014434790262159" | awk '{print $1+0}'
> 3671014434790262272

In this case, you are forcing the value of $1 to be a number.
Unfortunately, the number you asked for is too big to fit into an IEEE
double value, so you get an approximation. The range of integers that
awk can handle correctly (on stock hardware) is plus or minus 2^53,
or 9007199254740992.

If you need to manipulate numbers larger than that, you can use bc.

--
Long-short-short, long-short-short / Dactyls in dimeter, John Cowan
Verse form with choriambs / (Masculine rhyme): co...@ccil.org
One sentence (two stanzas) / Hexasyllabically
Challenges poets who / Don't have the time. --robison who's at texas dot net


Valerio, Danilo

unread,
Dec 10, 2009, 5:34:13 PM12/10/09
to John Cowan, bug-gn...@gnu.org
Thanks John,

The problem is that I have several gigabytes of a txt-file containing numbers
like that. So I wanted to avoid bc.

For example the simplest thing I would need is an AND between that number and
0xffff.
It sounds stupid, but I am not able to find a workaround for getting the right
result.
Of course
> echo "3671014434790262159" | awk '{print and($1,0xffff}'
gives the wrong result, due to the approximation you just explained.

And of course bc does the job good, but I never thought at it for processing
very large streams.

Several people suggested me to switch to perl, but... I want to try everything
I can with gawk before starting learning perl. :-)

Dan

John Cowan

unread,
Dec 10, 2009, 9:38:22 PM12/10/09
to Valerio, Danilo, John Cowan, bug-gn...@gnu.org
Valerio, Danilo scripsit:

> Several people suggested me to switch to perl, but... I want to try
> everything I can with gawk before starting learning perl. :-)

You really do need a language with bignum (arbitrary-precision integer)
support, such as Java, Common Lisp, Scheme (in most dialects), or Perl.
Of these, Perl is the closest to awk, and there's even a translator, a2p.
Be sure to read the caveats in the a2p man page, though. Then you can
add "use bignum;" to the top of the generated Perl, and away you go.

--
My confusion is rapidly waxing John Cowan
For XML Schema's too taxing: co...@ccil.org
I'd use DTDs http://www.ccil.org/~cowan
If they had local trees --
I think I best switch to RELAX NG.


0 new messages