bug-gnu-utils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

awk float problems ( decimal dot or comma separator and unexpected resul


From: Ramírez Sánchez-Escobar José Julio
Subject: awk float problems ( decimal dot or comma separator and unexpected results whether print or printf)
Date: Wed, 2 Aug 2006 14:52:42 +0200

I'm dealing with awk trying to write a very simple script,
and I get unexpected results whether print or printf 
related to awk float.
The version of awk is:
 1# gawk --version
GNU Awk 3.1.3
And for the operating system and its revision I did: 
uname -s -r -v -m -p -i -o (is it correct for this purpose?):
 2# uname -s -r -v -m -p -i -o
Linux 2.6.11-1.27_FC3smp #1 SMP Tue May 17 20:43:11 EDT 2005 i686 i686 i386 
GNU/Linux

I would like to ask for help:
I want to add floats in a unix C (tcsh) shell. 
I though using awk as a way to do.
I want to use awk to change the value of a variable, 
increasing 0.5 each step as follows:
0.5
1.0
1.5
....
To do this first I though that:
 124# cat > awkscript2
set i = 0.5 ; echo $i
echo $i | awk '{print $1 + 0.5}'
 125# tcsh -vx awkscript2
set i = 0.5 ; echo $i
set i = 0.5
echo 0.5
0.5
echo $i | awk '{print $1 + 0.5}'
echo 0.5
awk {print $1 + 0.5}
0,5
Same result for awk '{print ( $1 + 0.5 )}'
I expect the value of 1.0 instead of 0,5, but if I write:
 131# cat > awkscript3
set i = 0.5 ; echo $i
echo $i | awk '{print $1 + 1}'
 132# tcsh -vx awkscript3
set i = 0.5 ; echo $i
set i = 0.5
echo 0.5
0.5
echo $i | awk '{print $1 + 1}'
echo 0.5
awk {print $1 + 1}
1
Then if I use printf:
 143# cat > awkscript4
set i = 0.5 ; echo $i
echo $i | awk '{printf ("%4.1f\n", ( $1 + 0.5 ))}'
 144# tcsh -vx awkscript4
set i = 0.5 ; echo $i
set i = 0.5
echo 0.5
0.5
echo $i | awk '{printf ("%4.1f\n", ( $1 + 0.5 ))}'
echo 0.5
awk {printf ("%4.1f\n", ( $1 + 0.5 ))}
 0,5
Then I thought in summing two fields of a file:
 147# cat > awkscript5
set i = 0.5 ; echo $i
echo "$i 0.5" > i ; awk '{print ( $1 + $2 )}' i
 148# tcsh -vx awkscript5
set i = 0.5 ; echo $i
set i = 0.5
echo 0.5
0.5
echo "$i 0.5" > i ; awk '{print ( $1 + $2 )}' i
echo 0.5 0.5
awk {print ( $1 + $2 )} i
0
And:
 149# cat > awkscript6
set i = 0.5 ; echo $i
echo "$i 0.5" > i ; awk '{printf ("%4.1f\n", ( $1 +$2 ))}' i
 150# tcsh -vx awkscript6
set i = 0.5 ; echo $i
set i = 0.5
echo 0.5
0.5
echo "$i 0.5" > i ; awk '{printf ("%4.1f\n", ( $1 +$2 ))}' i
echo 0.5 0.5
awk {printf ("%4.1f\n", ( $1 +$2 ))} i
 0,0
And same if I subtitute the decimal dot separator by comma:
 156# cat awkscript6
set i = 0,5 ; echo $i
echo "$i 0,5" > i ; awk '{printf ("%4.1f\n", ( $1 +$2 ))}' i
 157# tcsh -vx awkscript6
set i = 0,5 ; echo $i
set i = 0,5
echo 0,5
0,5
echo "$i 0,5" > i ; awk '{printf ("%4.1f\n", ( $1 +$2 ))}' i
echo 0,5 0,5
awk {printf ("%4.1f\n", ( $1 +$2 ))} i
 0,0

Summing up, How can I increase the value 0.5 in steps of 0.5 each, 
and why I'm getting as decimal separator a comma instead of a dot?.
There is anyway to specify the kind of awk separator for floats as 
an others prebuilt awk variables? 
Could this be the problem to get the right value?, Is this a bug?
I'm completely lost with this, and I would appreciate very much 
any suggestion.

Thank you very much, 
JJ






reply via email to

[Prev in Thread] Current Thread [Next in Thread]