Code Comments
Programming Forum and web based access to our favorite programming groups.Hi
I have a list of registers like this:
4.13295489416043E-04,18.837
8.34706990538692E-02,19.016
0.25013649151908,19.341
0.333469745379554,19.449
I want to substract 24 to the first field of each register. For this
purpose i have used the following sentence:
awk -F, '{$1=$1-24; print $0}' archive
This is the result:
-20 18.837
-16 19.016
-24 19.341
-24 19.449
The results are wrong because awk interprets double precision numbers
like integers. My question is: how to do floating point operations
correctly?
Thanks
Post Follow-up to this messageNot an expert here, but I think you could try
awk -F, '{$1=strtod($1)-24; print $0}' archive
instead ...
Gives
-23.9996 18.837
-23.9165 19.016
-23.7499 19.341
-23.6665 19.449
on my machine which looks better than your result ;)
Post Follow-up to this messageNot an expert here, but I think you could try
awk -F, '{$1=strtonum($1)-24; print $0}' archive
instead ...
Gives
-23.9996 18.837
-23.9165 19.016
-23.7499 19.341
-23.6665 19.449
on my machine which looks better than your result ;)
Post Follow-up to this message
Itaca wrote:
> Hi
>
> I have a list of registers like this:
>
> 4.13295489416043E-04,18.837
> 8.34706990538692E-02,19.016
> 0.25013649151908,19.341
> 0.333469745379554,19.449
>
> I want to substract 24 to the first field of each register. For this
> purpose i have used the following sentence:
>
> awk -F, '{$1=$1-24; print $0}' archive
>
> This is the result:
>
> -20 18.837
> -16 19.016
> -24 19.341
> -24 19.449
>
> The results are wrong because awk interprets double precision numbers
> like integers. My question is: how to do floating point operations
> correctly?
Try this:
awk -F, '{$1=$1-24; printf "%.20e, %.10e, %f %s\n", $1,$1,$1,$2}'
archive
and pick the $1 output format you like.
Ed.
> Thanks
Post Follow-up to this messageIn article <4c59890.0504050212.92e425f@posting.google.com>,
Itaca <mazacar@hotmail.com> wrote:
[...]
% I want to substract 24 to the first field of each register. For this
% purpose i have used the following sentence:
%
% awk -F, '{$1=$1-24; print $0}' archive
This should give a floating-point result for the first field.
What awk are you using?
--
Patrick TJ McPhee
North York Canada
ptjm@interlog.com
Post Follow-up to this messageptjm@interlog.com (Patrick TJ McPhee) wrote in message news:<3bh617F6j93fbU1@uni-berlin.de>
..
> In article <4c59890.0504050212.92e425f@posting.google.com>,
> Itaca <mazacar@hotmail.com> wrote:
>
> [...]
>
> % I want to substract 24 to the first field of each register. For this
> % purpose i have used the following sentence:
> %
> % awk -F, '{$1=$1-24; print $0}' archive
>
> This should give a floating-point result for the first field.
>
> What awk are you using?
Exactly! It depends on the awk version !
With GNU Awk 3.1.3 the results are wrong. I have installed GNU Awk
3.1.1 on my laptop and it works fine! Newer version works worse? Is it
a bug?
Post Follow-up to this messageItaca wrote: > ptjm@interlog.com (Patrick TJ McPhee) wrote in message news:<3bh617F6j93fb U1@uni-berlin.de>... > > > > Exactly! It depends on the awk version ! > > With GNU Awk 3.1.3 the results are wrong. I have installed GNU Awk > 3.1.1 on my laptop and it works fine! Newer version works worse? Is it > a bug? Is it possible that this is a locale issue? Maybe your locale is set to something unexpected and gawk 3.1.1 was ignoring it but gawk 3.1.3 is using it correctly. Just a guess. Take a look at http://www.gnu.org/software/gawk/ma...wk.html#Locales for some more info on locales. Ed.
Post Follow-up to this messageOn Wed, 06 Apr 2005 08:45:27 -0500 in comp.lang.awk, Ed Morton <morton@lsupcaemnt.com> wrote: >Itaca wrote: > >Is it possible that this is a locale issue? Maybe your locale is set to >something unexpected and gawk 3.1.1 was ignoring it but gawk 3.1.3 is >using it correctly. Just a guess. Take a look at >http://www.gnu.org/software/gawk/ma...wk.html#Locales for some more >info on locales. Looks like you made a good catch: the OP's awk seems to bebetween period and comma, maybe because comma is the locale decimal point, as in most non-English locales, where semicolon fills the role in lists which use comma in English locales. -- Thanks. Take care, Brian Inglis Calgary, Alberta, Canada Brian.Inglis@CSi.com (Brian[dot]Inglis{at}SystematicSW[dot]a b[dot]ca) fake address use address above to reply
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.