For Programmers: Free Programming Magazines  


Home > Archive > AWK > April 2005 > Problem with double precision numbers









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Problem with double precision numbers
Itaca

2005-04-10, 3:55 am

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
tink

2005-04-11, 3:56 pm

Not 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 ;)

tink

2005-04-11, 3:56 pm

Not 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 ;)

Ed Morton

2005-04-11, 3:56 pm



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

Patrick TJ McPhee

2005-04-11, 3:56 pm

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?
--

Patrick TJ McPhee
North York Canada
ptjm@interlog.com
Itaca

2005-04-11, 3:56 pm

ptjm@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?
Ed Morton

2005-04-11, 8:55 pm



Itaca wrote:
> ptjm@interlog.com (Patrick TJ McPhee) wrote in message news:<3bh617F6j93fbU1@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.
Brian Inglis

2005-04-12, 3:56 am

On 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 be
between 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
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com