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 floating point arithmetic
Hi all. Major newbie here. I'm learning awk trying the examples in chapter
4 of Kernighan and Pike's classic text "The Unix Programming Environment"
but I'm experiencing some problems with small float values. In the book
there's an example to calculate totals by name. Using the input file:

Susie   400
John    100
Mary    200
Mary    300
John    100
Susie   100
Mary    100

The program:

{ sum[$1] += $2 }
END     { for (name in sum) print name, sum[name] }

calculates the totals:

Mary 600
John 200
Susie 500

The problem arises when I try this with floating point values:

Susie   1,2
John    0,1
Mary    1
Mary    0,4
John    0,33
Susie   0,48
Mary    1,1

The output of the programa now is:

Mary 2,1
John 0
Susie 1,2

If I try dots instead of commas:

Susie   1.2
John    0.1
Mary    1
Mary    0.4
John    0.33
Susie   0.48
Mary    1.1

I get equally wrong results:

Mary 2
John 0
Susie 1

What's wrong? I'm using GAWK (the version that's shipped with Mandrake
10.1)

Report this thread to moderator Post Follow-up to this message
Old Post
ELG
12-07-04 08:59 AM


Re: Problem with floating point arithmetic
ELG wrote:
>
> 	{ sum[$1] += $2 }
> END     { for (name in sum) print name, sum[name] }
>
> The problem arises when I try this with floating point values:
>
> If I try dots instead of commas:
>
> Susie   1.2
> John    0.1
> Mary    1
> Mary    0.4
> John    0.33
> Susie   0.48
> Mary    1.1
>
> I get equally wrong results:
>
> Mary 2
> John 0
> Susie 1

That's an effect of the configured locale, I suppose.
(I get these values when I use commas instead of dots.)

> What's wrong? I'm using GAWK (the version that's shipped with Mandrake
> 10.1)

Susie   1.2
John    0.1
Mary    1
Mary    0.4
John    0.33
Susie   0.48
Mary    1.1

Mary 2.5
John 0.43
Susie 1.68

Works for me.

% awk --version
GNU Awk 3.1.1

Janis

Report this thread to moderator Post Follow-up to this message
Old Post
Janis Papanagnou
12-07-04 08:59 AM


Re: Problem with floating point arithmetic
In article <pan.2004.12.02.23.43.57.731305@nospam.net>,
ELG <ELG@nospam.net> wrote:

> Hi all. Major newbie here. I'm learning awk trying the examples in chapter
> 4 of Kernighan and Pike's classic text "The Unix Programming Environment"
> but I'm experiencing some problems with small float values. In the book
> there's an example to calculate totals by name. Using the input file:
>
> Susie   400
> John    100
> Mary    200
> Mary    300
> John    100
> Susie   100
> Mary    100
>
> The program:
>
> 	{ sum[$1] += $2 }
> END     { for (name in sum) print name, sum[name] }
>
> calculates the totals:
>
> Mary 600
> John 200
> Susie 500
>
> The problem arises when I try this with floating point values:
>
> Susie   1,2
> John    0,1
> Mary    1
> Mary    0,4
> John    0,33
> Susie   0,48
> Mary    1,1
>
> The output of the programa now is:
>
> Mary 2,1
> John 0
> Susie 1,2
>
> If I try dots instead of commas:
>
> Susie   1.2
> John    0.1
> Mary    1
> Mary    0.4
> John    0.33
> Susie   0.48
> Mary    1.1
>
> I get equally wrong results:
>
> Mary 2
> John 0
> Susie 1
>
> What's wrong? I'm using GAWK (the version that's shipped with Mandrake
> 10.1)

what is the value of OFMT ?  This is the output format specifier for
numeric values

awk 'BEGIN{print OFMT}'

The general default is %.6g

If it anything else, then that might be the problem.

Bob Harris

Report this thread to moderator Post Follow-up to this message
Old Post
Bob Harris
12-07-04 08:59 AM


Re: Problem with floating point arithmetic
Perhaps the default format for floats is wrong.
Add this to your program:

BEGIN { print OFMT; OFMT="%.6g" }

Report this thread to moderator Post Follow-up to this message
Old Post
William James
12-07-04 08:59 AM


Re: Problem with floating point arithmetic
Prawdziwa Blondynka napisał(a):
> Hi,
> I have exactly the same problem, my OFMT is right. Do you have any
> other ideas about what could be wrong?
> I'm working on Solaris...

I have the same problem but my awk bad interpreted :

"0.45" + 1 => 1"

----------------
Jak masz ustawiony polski język to wtedy kropką jest przecinek :-(.
Co prawda nic to nie dało, bo źle interpretował część ułamkową :-(

Ja napisałem sobie króciutką funkcję :
...

function s_tonum ( sliczba,   znak,c,u ) {
# Funkcja poprawnie konweryje strind na liczbę rzeczywistą
#   np.  " 0.23" (ang.) lub " 0,23" (polski jezyk)
#
gsub( / /, "", sliczba);
znak = index( sliczba, ".");
if (znak == 0) index( sliczba, ",");
if (znak == 0) return strtonum( sliczba);
c = strtonum( substr( sliczba, 1, znak -1 ));
u = substr( sliczba, znak + 1);
return ( c  + ( u/(10**length(u)) ));
};

....
number = s_tonum( " 0.456");
....

Koriolan

Report this thread to moderator Post Follow-up to this message
Old Post
Koriolan
12-07-04 08:59 AM


Re: Problem with floating point arithmetic
Janis Papanagnou wrote:

>
> That's an effect of the configured locale, I suppose.
> (I get these values when I use commas instead of dots.)
>

Thank you for your reply. Can you please post the output of the locale
command in your system so I'm able to properly configure mine?



Report this thread to moderator Post Follow-up to this message
Old Post
ELG
12-07-04 08:59 AM


Re: Problem with floating point arithmetic
w_a_x_man@yahoo.com (William James) wrote in message news:<f8860640.0412022225.c11089d@post
ing.google.com>...
> Perhaps the default format for floats is wrong.
> Add this to your program:
>
> BEGIN { print OFMT; OFMT="%.6g" }


Hi,
I have exactly the same problem, my OFMT is right. Do you have any
other ideas about what could be wrong?
I'm working on Solaris...

Thanks,
Blondie

Report this thread to moderator Post Follow-up to this message
Old Post
Prawdziwa Blondynka
12-07-04 08:59 AM


Re: Problem with floating point arithmetic
In article <harris-2D29F0.20510004122004@cacnews.cac.cpqcorp.net>,
Bob Harris  <harris@zk3.dec.com> wrote:

% Isn't there 2 awk's available on sun?  One called awk, and one called
% nawk ???

On the most recent sun machines, there are generally four awks available --
one called awk, another called awk, another called nawk and one called gawk.

% If I recall previous posts to this news group, the default 'awk' version
% is very old and moldy.  The 'nawk' version is the more capable version
% of awk shipped by sun.

/usr/bin/awk is the old and moldy version. If you put /usr/xpg4/bin first
in your path, then the default awk is supposed to be posix-compliant (ditto
for the other utilities).

--

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
12-07-04 08:59 AM


Re: Problem with floating point arithmetic
Change
if (znak == 0) index( sliczba, ",");
to
if (znak == 0) znak = index( sliczba, ",")


Even better:

-------------------------------------------

function s_tonum( s )
{
gsub( /,/, ".", s )
return s + 0
}

BEGIN {
number = s_tonum( "   0,456  " ) + 1
print number
}


Report this thread to moderator Post Follow-up to this message
Old Post
William James
12-07-04 01:55 PM


Re: Problem with floating point arithmetic
Janis Papanagnou wrote:

>
> That's an effect of the configured locale, I suppose.
> (I get these values when I use commas instead of dots.)
>

Thank you for your reply. Can you please post the output of the locale
command in your system so I'm able to properly configure mine?



Report this thread to moderator Post Follow-up to this message
Old Post
ELG
12-09-04 01:55 PM


Sponsored Links




Last Thread Next Thread Next
Pages (2): [1] 2 »
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:21 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.