awk if statement / equals operator


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers awk if statement / equals operator
# 1  
Old 12-17-2012
awk if statement / equals operator

Hi,
I was hoping someone could explain this please Smilie

I'm using bash, scientific linux... and I don't know what else you need to know. With

Code:
awk '{ if( 0.3 == 0.1*3) print $1}' file.dat

nothing will be printed since apparently the two numbers do not equate. (Using 0.3 != 0.1*3 is seen as true however)

0.2==0.1*2 is seen as true
0.5==0.1*5 is true
0.7==0.1*7 is false...

Many thanks
# 2  
Old 12-17-2012
One way would be to avoid decimal numbers. For me, this works

Code:
awk '{ if( 10*0.3 == 10*0.1*3) print $1}' file.dat
awk '{ if( 10*0.7 == 10*0.1*7) print $1}' file.dat

A different approach would be to avoid equal compares with decimal numbers. Instead you could write
Code:
awk '{ if( abs(0.3 - 0.1*3) < 0.00001) print $1}' file.dat

But since awk does not have a builtin abs function, you would have to provide one in your awk-script:
Code:
function abs(x) {
    return ((x < 0.0) ? -x : x)
}

# 3  
Old 12-17-2012
Thanks -

avoiding using decimals works for the case I'm doing now but could you tell me WHY this is even a problem for awk?
And why are there certain decimal numbers (0.3, 0.7) that pose a problem while the vast majority do not?
# 4  
Old 12-17-2012
Floating point numbers are a problem for all languages, because many non-integral numbers can not be represented precisely. The fact, that a certain number can be represented with a finite number of fractional digits in the decimal system does not mean, that they also have a finite number of fractional digits in a certain binary floating point representation. That's why you never should check for equality with floating point numbers, no matter what language you are using, because of the internal representation.

Commercial software uses fixed-point representations for this purpose or even plain integer variables.
This User Gave Thanks to hergp For This Post:
# 5  
Old 12-17-2012
FWIW I tried this in ksh93 and it seemed to work there:
Code:
$ printf "%.20f %.20f\n" 0.3 $((0.1*3))
0.30000000000000000000 0.30000000000000000000
$ printf "%.20f %.20f\n" 0.3 0.1*3 
0.30000000000000000000 0.30000000000000000000

Various awks:
gawk / mawk / bwk:
Code:
$ gawk 'BEGIN{printf "%.20f %.20f\n",0.3,0.1*3}'
0.29999999999999998890 0.30000000000000004441

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Like operator in IF statement

how can i use like operator in IF statement. Below is correct format, please guide if ; then CT_ACT_FILE_NAME=`echo FINACLE' else CT_ACT_FILE_NAME=`echo not listed' fi ---------- Post updated at 04:58 PM ---------- Previous update was at 04:56 PM ---------- Please use CODE... (6 Replies)
Discussion started by: rizwan.shaukat
6 Replies

2. Shell Programming and Scripting

Using like operator in awk if condition

Hello All, I have developed a script which selects a particular filed from a file ,trims it,searches for a particular pattern and then mail it when found. cat test_file.txt |sed -n '5,$p'|sed -e 's/ //g'|awk -F'|' '{if ($4 !="Alive") print $1,$2,$3,$4}' >> proc_not_alive.txt It is... (4 Replies)
Discussion started by: karthik adiga
4 Replies

3. Shell Programming and Scripting

Convert Update statement into Insert statement in UNIX using awk, sed....

Hi folks, I have a scenario to convert the update statements into insert statements using shell script (awk, sed...) or in database using regex. I have a bunch of update statements with all columns in a file which I need to convert into insert statements. UPDATE TABLE_A SET COL1=1 WHERE... (0 Replies)
Discussion started by: dev123
0 Replies

4. Shell Programming and Scripting

Plz help me out use OFMT operator in awk programming

Hi While calculating the sum of 6 and 7 fileds it is calculate the wrong value because of floating point like 7898778.10 awk ' BEGIN { FS = OFS = "|" } NR == 1 { next } { UX = $1 OFS $3 OFS $4 OFS $5 p1 +=$6 p2 +=$7 } END { for(i in UX) ... (8 Replies)
Discussion started by: jagu
8 Replies

5. Shell Programming and Scripting

Getting syntax error with awk ternary operator

split($7,a," "); date = a; time = a split(date,d,"/"); month = sprintf("%02d",d); day = sprintf("%02d",d); year = 2000 + d % 100 split(time,t,":"); hour=t; min=t hour >= 12? { hour=hour-12; amPm=" PM" } : amPM=" AM" hour == 0? hour=12 time=sprintf("%02d",hour)":"sprintf("%02d",min)amPm ... (4 Replies)
Discussion started by: Michael Stora
4 Replies

6. Shell Programming and Scripting

awk Help - Comparison Operator problem

Hi, I've tried searching through the forum but I've drawn a blank so i'm going to post here. I'm developing a number of checks on a CSV file, trying to find if any are greater than a max limit. I'm testing it by running it from a command line. The file I'm testing has 8 records. When I... (3 Replies)
Discussion started by: Tmart
3 Replies

7. Shell Programming and Scripting

Awk with or and not operator

awk -F '/' '{ if (($2!="30") print }' f.dat > date_mismatch.dat The above command is giving the desired results But I would want use Or in the above but it is returning the complete file awk -F '/' '{ if ($2!="30"||$2!="31") print }' f.dat > date_mismatch.dat Have tried the... (1 Reply)
Discussion started by: infernalhell
1 Replies

8. Shell Programming and Scripting

How to write If statement using && and operator in Unix

Hi What is the syntax for if statement using && and || operator? if && ] || here its giving me an error to this if statement any suggestion?? (2 Replies)
Discussion started by: Avi
2 Replies

9. Shell Programming and Scripting

cannot properly employ "or" operator in an if statement (bash)

Hi, I have a variable, $sername, and I would like to display this variable only if it *does not* contain either of these two tags: *DTI*FA* or *DIFF*FA*. I think the syntax for my 'or' operator is off. The variable $sername is continuously changing in an outer loop (not shown), but at the... (4 Replies)
Discussion started by: goodbenito
4 Replies

10. Shell Programming and Scripting

Diamond operator in Until Statement (perl)

Hello: I have the following perl script which is giving me trouble inside the second elsif statement. The purpose of the script is to go through a file and print out only those lines which contain pertinent information. The tricky part came when I realized that certain items actually spanned... (5 Replies)
Discussion started by: erichpowell
5 Replies
Login or Register to Ask a Question