The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
.
google unix.com



UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD.

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-17-2008
mohan705 mohan705 is offline
Registered User
  
 

Join Date: Feb 2007
Posts: 134
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
  #2 (permalink)  
Old 07-17-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,748
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.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 04:26 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0