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

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.