bug-gnu-utils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: gawk string to number (bug?)


From: Valerio, Danilo
Subject: Re: gawk string to number (bug?)
Date: Thu, 10 Dec 2009 23:34:13 +0100
User-agent: KMail/1.12.4 (Linux/2.6.31-16-generic; KDE/4.3.4; x86_64; ; )

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

On Thursday 10 December 2009 07:18:41 pm you wrote:
> 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.
> 




reply via email to

[Prev in Thread] Current Thread [Next in Thread]