Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Problem with double precision numbers
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

Report this thread to moderator Post Follow-up to this message
Old Post
Itaca
04-10-05 08:55 AM


Re: Problem with double precision numbers
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 ;)


Report this thread to moderator Post Follow-up to this message
Old Post
tink
04-11-05 08:56 PM


Re: Problem with double precision numbers
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 ;)


Report this thread to moderator Post Follow-up to this message
Old Post
tink
04-11-05 08:56 PM


Re: Problem with double precision numbers

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

Report this thread to moderator Post Follow-up to this message
Old Post
Ed Morton
04-11-05 08:56 PM


Re: Problem with double precision numbers
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

Report this thread to moderator Post Follow-up to this message
Old Post
Patrick TJ McPhee
04-11-05 08:56 PM


Re: Problem with double precision numbers
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?

Report this thread to moderator Post Follow-up to this message
Old Post
Itaca
04-11-05 08:56 PM


Re: Problem with double precision numbers

Itaca 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.

Report this thread to moderator Post Follow-up to this message
Old Post
Ed Morton
04-12-05 01:55 AM


Re: Problem with double precision numbers
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

Report this thread to moderator Post Follow-up to this message
Old Post
Brian Inglis
04-12-05 08:56 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

AWK archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 07:08 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.