Re: NCL bug

From: Dave Allured <dave.allured_at_nyahnyahspammersnyahnyah>
Date: Mon Sep 12 2011 - 13:02:07 MDT

Gus,

This is a classic example of roundoff error. The number 0.9 does
not have an exact representation in standard floating point binary.
  Your code has computed 0.9 as an NCL "float" by two different methods:

(1) x = ispan(0,100,1)*.01, then take x(90)
(2) Entered directly as an NCL constant "0.9"

Now, the NCL print statement does *not* display all significant
digits by default. If you show all significant digits with the
sprintf function, the issues will become clearer:

mac56:~ 3> ncl
  NCAR Command Language Version 5.2.1
ncl 1> x = ispan(0,100,1)*.01
ncl 2> y = 0.9
ncl 3> print (sprintf ("%15.9f", x(90)))
(0) 0.899999976
ncl 4> print (sprintf ("%15.9f", y))
(0) 0.899999976

On my system, the two methods get exactly the same floating point
number. But floating point roundoff can vary from system to system.
  Do you get two different numbers by the above method?

--Dave

On 9/12/2011 10:49 AM, Ghassan J. Alaka, Jr. wrote:
> Hi,
>
> I have found a bug in NCL version 5.1.1, although a colleague of
> mine confirmed that the same error in NCL version 6.0.0.
>
> If I create an array (called "x") that contains increments from 0 to
> 1 by 0.01, I run into problems when x should equal 0.9. The print()
> function says that x(90) = 0.9, yet x(90) > 0.9 after some
> investigation. This only seems to be occurring at x = 0.9. Why is
> this the case?
>
> I have attached a simple script to view this error.
>
> Thank you,
> Gus Alaka
>
>
> nclbug.ncl
>
>
>
>
> ;================================================================
> ; Load scripts
> ;================================================================
>
> load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"
> load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl"
> load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"
> load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/shea_util.ncl"
>
>
> begin
>
> ;x = fspan(0.,1.0,101)
> x = ispan(0,100,1)*.01
>
> val1 = ind(x.eq.0.1)
> val2 = ind(x.eq.0.5)
> val3 = ind(x.eq.0.9)
>
> print(""+val1)
> print(""+val2)
> print(""+val3)
> print(""+x(90))
>
> if(x(90).eq.0.9)then
> print("!Equal!")
> end if
>
> if(x(90).lt.0.9)then
> print("!LessThan!")
> end if
>
> if(x(90).gt.0.9)then
> print("!GreaterThan!")
> end if
>
> ; if x(90) = 0.9 (as seen from print()), why doesn't x(90) = 0.9?
> ; This problem does not seem to exist at 0.8, 0.7, 0.6, etc.
>
> end
>
>
>
> _______________________________________________
> ncl-talk mailing list
> List instructions, subscriber options, unsubscribe:
> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
_______________________________________________
ncl-talk mailing list
List instructions, subscriber options, unsubscribe:
http://mailman.ucar.edu/mailman/listinfo/ncl-talk
Received on Mon Sep 12 13:02:00 2011

This archive was generated by hypermail 2.1.8 : Mon Sep 12 2011 - 13:37:01 MDT