[ncl-talk] formatted print, select subscription

Dave Allured - NOAA Affiliate dave.allured at noaa.gov
Fri Dec 20 13:16:52 MST 2019


I am only answering question #1.  That list of numbers is a very special
case.  Every number is exactly half way between two integers.  Because of
this, you get into the fine details of how different compilers handle
rounding.  There is a difference in the tie-breaking rule for numbers that
are exactly half way between the two closest representable values.

Your versions of NCL and gfortran use a rounding mode called "Round half to
even".  But your PGI compiler uses a different rounding mode called "Round
half away from zero".  This explains the differences between the lists that
you showed.  See the wikipedia page "Rounding" for more details.

There are other names in use.  In particular, the IEEE 754 standard for
floating point arithmetic uses different and more precise names for these
two rounding modes.

In NCL, if you want "Round half away from zero" like you showed, then yes,
you should use the "round" function, instead of depending on printf
rounding.  Unfortunately, the exact rounding mode of NCL's "round" function
is not documented and not guaranteed.  This behavior may change between NCL
versions, install configurations, and platforms.  I hope the current
behavior is sufficient for your immediate purposes.


On Fri, Dec 20, 2019 at 4:26 AM Ehsan Taghizadeh via ncl-talk <
ncl-talk at ucar.edu> wrote:

> Hi,
> I hope you're doing great.
> I have some difficulties and I'll be thankful if I could have your help.
>
> 1- Why -0.5 is written 0 (not -1) while -1.5 is written -2, with "%4.0f"
> in the following lines? It seems reals round to even numbers.
>
> a := (/-0.5D0, 0.5D0, -1.5D0, 1.5D0, -2.5D0, 2.5D0, -3.5D0, 3.5D0/)
>  print(sprintf("%4.0f", a))
>
> output:
> (0)       -0
> (1)        0
> (2)       -2
> (3)        2
> (4)       -2
> (5)        2
> (6)       -4
> (7)        4
> However, this is same for gfortran.
> fortran code:
>
> implicit none
> real*8 :: a(8)
> a = (/-0.5D0, 0.5D0, -1.5D0, 1.5D0, -2.5D0, 2.5D0, -3.5D0, 3.5D0/)
> print"(f4.0)", a
> end
>
> output:
>  -0.
>   0.
>  -2.
>   2.
>  -2.
>   2.
>  -4.
>   4.
>
> But for PGI compiler I see the other (expected) result:
> output:
>  -1.
>   1.
>  -2.
>   2.
>  -3.
>   3.
>  -4.
>   4.
>
> I confused what happened? Back to NCL, should I use round function, as
> below, to write output, always?
>
> b = round(a,0)
> print(sprintf("%4.0f", b))
>
> output:
> (0)       -1
> (1)        1
> (2)       -2
> (3)        2
> (4)       -3
> (5)        3
> (6)       -4
> (7)        4
>
> 2- How could I select (or delete) unordered subscription from a
> multidimensional array? For example, if there is an array (a) with
> following dimensions:
>
> [lev | 34] x [lat | 361] x [lon | 720]
>
> and I want to make other array like a, exclude a(0,:,:), a(7,:,:) and
> a(10,:,:), what way is better than the following line?
>  b :=
> a((/1,2,3,4,5,6,8,9,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33/),:,:)
>
> 3- How could I compute difference between two successive levels, instead
> of using following loop:
> FL = u at lev
> ws(0,:,:) = 0.0
> do i = 1, dimsizes(FL)-1
>     ws(i,:,:) = sqrt((u(i,:,:)-u(i-1,:,:))^2+(v(i,:,:)-v(i-1,:,:))^2)
> end do
> As you know above lines compute wind shear.
>
> Pardon me to ask different questions in one email.
> I'll be thankful for any help.
>
> *Sincerely*
> *Ehsan*
> *PhD student of Meteorology,*
> *Institute of Geophysics, University of Tehran, Tehran, Iran*
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20191220/259fcb59/attachment.html>


More information about the ncl-talk mailing list