The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Floating point error in C Hara High Level Programming 2 06-18-2008 06:43 AM
Rounding off the value of Floating point value damansingh Shell Programming and Scripting 7 05-21-2008 10:46 AM
How to Compare Floating point / real numbers padarthy Shell Programming and Scripting 13 09-24-2007 09:03 PM
floating point problem vijlak High Level Programming 4 03-08-2007 04:18 AM
Floating Point Division gsatch Shell Programming and Scripting 1 07-25-2002 05:03 PM

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 06-24-2005
kanagias kanagias is offline
Registered User
  
 

Join Date: Jun 2005
Posts: 4
problem with floating point numbers in awk

hi all, i have the following problem using awk in a script

i want to read the values from a column with real numbers and calculate the mean.the problem is that when i use a statement such as this

num = $4

i cant find a way to convert the variable from string to floating point to perform any operations.i tried the following

sum = num + 0.0

but sum is equal to 0 after this statement, num is not treated as floating point number but as string

any help would be appreciated

edit : i am using awk 3.1.4 on Suse 9.2
  #2 (permalink)  
Old 06-24-2005
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,131
Quote:
Originally Posted by kanagias
hi all, i have the following problem using awk in a script

i want to read the values from a column with real numbers and calculate the mean.the problem is that when i use a statement such as this

num = $4

i cant find a way to convert the variable from string to floating point to perform any operations.i tried the following

sum = num + 0.0

but sum is equal to 0 after this statement, num is not treated as floating point number but as string

any help would be appreciated

edit : i am using awk 3.1.4 on Suse 9.2

what "operation" are you trying to perform?
for example:
echo '1.2' | nawk '{num=$1; print num, num+3.5}'
  #3 (permalink)  
Old 06-24-2005
kanagias kanagias is offline
Registered User
  
 

Join Date: Jun 2005
Posts: 4
i just want to do simple mathematical operations.to be more clear here is a test awk script i used

BEGIN{}

{

num = $1
sum += num

}

END {
print sum
}

where column 1 contains the floating point numbers
print sum produces 0...

i tried to add 0.0 to num to force to be treated as number and not as string but that didnt work either
  #4 (permalink)  
Old 06-24-2005
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,131
produces a fine floating point number for me - Solaris's nawk:

Code:
nawk '{sum +=$1} END{print sum}' <<!
1.2
1.3
1.4
!

  #5 (permalink)  
Old 06-24-2005
kanagias kanagias is offline
Registered User
  
 

Join Date: Jun 2005
Posts: 4
neither awk or gawk works for me...
anyway thanks for replying
  #6 (permalink)  
Old 06-24-2005
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,131
Quote:
Originally Posted by kanagias
neither awk or gawk works for me...
anyway thanks for replying
works under gawk for me....
there must be something strange with your 'data source'
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 05:08 PM.


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