# [ncl-talk] dividing by zero

Dave Allured - NOAA Affiliate dave.allured at noaa.gov
Mon Nov 7 12:50:55 MST 2016

```Laura,

Quoting from the documentation for "where":

"Caveat: NCL evaluates the entire expression inside the where function,
which will cause problems if it's an expression NCL would normally fail on."

There is more than one way to fix this.  This is my preferred method
because it is self documenting.

rain at _FillValue = default_fillvalue(typeof(rain))
; do this only if rain does not already have a good _FillValue

divisor = where (rain .ne. 0, rain, rain at _FillValue)
ratio_rainc = where (rain .gt. 5., rainc / divisor, rain at _FillValue)

That was a general solution.  You have a special case where the two
conditionals overlap, so one of them is unnecessary.  You could merge
conditionals like this:

divisor = where (rain .gt. 5, rain, rain at _FillValue)
ratio_rainc = rainc / divisor

--Dave

On Mon, Nov 7, 2016 at 11:59 AM, Laura Fowler <laura at ucar.edu> wrote:

> Hello:
>
> I am trying to get around the error message:
>
> fatal:divide: Division by 0, Can't continue
> fatal:Div: operator failed, can't continue
>
> when trying to calculate the ratio between the convective rain and
> total rain on a global scale. To avoid dividing by zero, I defined the
> missing value:
>
> rain at _FillValue = default_fillvalue(typeof(rain))
>
> prior to the line:
>
> ratio_rainc = where(rain.gt.5.,rainc/rain,rain at _FillValue)
>
> I thought that this would solve the issue of dividing by zero, but it
> did not. I am not sure what to try next?
>
> Thanks,
> Laura
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20161107/1f671f0a/attachment.html
```