Unix Shell - nawk: division by zero Problem

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > September 2007 > nawk: division by zero Problem





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 nawk: division by zero Problem
wxkevin@yahoo.com

2007-09-17, 1:29 pm

I have an nawk script that is dividing by a very small number (2.4 X
10^-8). nawk is giving me a divide by zero. Does anybody know of a
workaround?

Ed Morton

2007-09-17, 1:29 pm

wxkevin@yahoo.com wrote:
> I have an nawk script that is dividing by a very small number (2.4 X
> 10^-8). nawk is giving me a divide by zero. Does anybody know of a
> workaround?
>


Use a different awk, e.g.:

$ gawk 'BEGIN{print 2.4 * (10^-8)}'
2.4e-08

Ed.
Janis Papanagnou

2007-09-17, 1:29 pm

Ed Morton wrote:
> wxkevin@yahoo.com wrote:
>
>
> Use a different awk, e.g.:
>
> $ gawk 'BEGIN{print 2.4 * (10^-8)}'
> 2.4e-08


Commonly written just as

$ awk 'BEGIN{print 2.4e-8}'
2.4e-08

And gawk has a much larger scale in divisions of small numbers, as the
OP asked for...

$ awk 'BEGIN{print 1.0/2.4e-300}'
4.16667e+299
$ awk 'BEGIN{print 1.0/2.4e-323}'
inf
$ awk 'BEGIN{print 1.0/2.4e-324}'
awk: fatal: division by zero attempted


Janis ;-)

>
> Ed.

wxkevin@yahoo.com

2007-09-17, 7:22 pm

On Sep 17, 10:54 am, Ed Morton <mor...@lsupcaemnt.com> wrote:
> wxke...@yahoo.com wrote:
>
> Use a different awk, e.g.:
>
> $ gawk 'BEGIN{print 2.4 * (10^-8)}'
> 2.4e-08
>
> Ed.


Unfortunately using another awk is not possible.

Michael Tosch

2007-09-17, 7:22 pm

wxkevin@yahoo.com wrote:
> On Sep 17, 10:54 am, Ed Morton <mor...@lsupcaemnt.com> wrote:
>
> Unfortunately using another awk is not possible.
>


Which OS?

Most OS have awk linked with the system math library "libm".
Probably you can install an OS patch for /usr/lib/libm.*

Otherwise you can try this workaround:

nawk 'BEGIN{tiny=1.0e-7; a=2.4e-8; print 1.0 / (a > tiny ? a : tiny)}'

--
Michael Tosch @ hp : com
Kenan Kalajdzic

2007-09-18, 1:26 am

wxkevin@yahoo.com wrote:
> On Sep 17, 10:54 am, Ed Morton <mor...@lsupcaemnt.com> wrote:
>
> Unfortunately using another awk is not possible.


Can you use bc instead?

--
Kenan Kalajdzic
Bill Marcum

2007-09-18, 1:27 pm

On Mon, 17 Sep 2007 07:28:59 -0700, wxkevin@yahoo.com
<wxkevin@yahoo.com> wrote:
>
>
> I have an nawk script that is dividing by a very small number (2.4 X
> 10^-8). nawk is giving me a divide by zero. Does anybody know of a
> workaround?
>

Multiply by the reciprocal, 4.16667e7


--
The grand leap of the whale up the Fall of Niagara is esteemed, by all
who have seen it, as one of the finest spectacles in nature.
-- Benjamin Franklin.
Ed Morton

2007-09-18, 1:27 pm

Janis Papanagnou wrote:
> Ed Morton wrote:
>
>
>
> Commonly written just as
>
> $ awk 'BEGIN{print 2.4e-8}'
> 2.4e-08


I wasn't paying attention. I thought his problem was with 10^-8 being a
small number, and 2.4 was an example of some data he was operating on
but why I didn't notice the lack of a division there when he clearly
stated that, is beyond me. I even read your response a couple of times
and couldn't figure out why you'd bothered to post it before it
eventually dawned on me what I'd done.

Thanks for putting the OP and I on a better track!

Ed.
Janis Papanagnou

2007-09-18, 1:27 pm

Ed Morton wrote:
>
> I wasn't paying attention. I thought his problem was with 10^-8 being a
> small number, and 2.4 was an example of some data he was operating on
> but why I didn't notice the lack of a division there when he clearly
> stated that, is beyond me. I even read your response a couple of times
> and couldn't figure out why you'd bothered to post it before it
> eventually dawned on me what I'd done.


It may soothe you to hear that, because of your consistently high
quality postings, I've also read your posting a couple of times to
find out where *I* had a misconception, before I posted mine. :-)

Janis
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2009 webservertalk.com