The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
Google UNIX.COM


UNIX for Advanced & Expert Users Advanced UNIX and Linux questions go here. Expert-to-Expert.

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 07-17-2008
Registered User
 

Join Date: Feb 2007
Posts: 110
awk help

Hi

In my file there are 4 columns having amount fields,but I am getting different amounts using the below one's



awk -F "|" '{a+=$1+$2+$3+$4} END {printf ("%f\n",a)}' file

1123337200682.226562

awk 'BEGIN{FS=OFS="|"} {for(i=1;i<=NF;i++) {a+=$i;n++ }} END {printf ("%f\n",a)}' file

1123337200684.453613



Thanks,
MR
Reply With Quote
Forum Sponsor
  #2  
Old 07-17-2008
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 4,298
Welcome to the wonderful world of floating point arithmetic.

First off, awk uses double precision floating point for internal representation of numbers.
The usual limit of double presision is 15 digits. You are printing 19 - do you need all of them? You can check your system, it may provide more than 15.

Next, the way awk is performing those fp adds is possibly the source of the problem.
For $1 + $2..... it probably is storing temporary values and getting intermediate sums.
The other loop probably does a straightforward addition, with only one temp storage variable. Slop in the representation of numbers increases as you step thru intermediate values and sums. The more intermediate values, the more possible error.

The other issue is that awk has to call atof for each number it reads in. The actual number stored in memory may not be exactly representable in fp, and since you are working near the limit of double precision, a few thousand of the small errors represents a change on the order of a whole number.

It is not the fault of awk, it has to do with the way fp numbers work.

You can try writing a bc script, it generally does better on big numbers. You can also try a simple C app with long double datatypes.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 02:17 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0