[ncl-talk] unexpected results from very simple code

danghy at gmail.com danghy at gmail.com
Mon Jul 20 23:48:23 MDT 2015


Thanks, Dennis!

Actually the "degrees" is a mixture of integers and floats, that's why I wanted to use "sprintf" to read them all through one command:
 
degrees = (/ \
210200051,121,36,43,38,58,26,\
210200052,121,33,54,38,57,04,\
210200053,121,33,50,38,53,06,\
210200054,121,37,59,38,54,43,\
210200055,121,37,30,38,51,22,\
210200056,121,31,05,38,51,22,\
210200057,121,15,10,38,48,22.3,\
210200058,121,44,53,39,07,52,\
210200059,121,46,37,39,03,04,\
210200060,121,58,37,39,03,47,\
21021001,121,59,21,39,37,30,\
21021002,122,00,41,39,37,01,\
21021003,121,58,55,39,24,07,\
21021004,121,58,41,39,24,18,\
21021005,122,57,29,39,41,54,\
21021006,122,58,00,39,40,40,\
21021007,122,35,00,39,16,11,\
21021008,121,28,17,39,37,48,\
21021009,121,24,35,39,26,15,\
21021010,121,35,06.11,38,54,04.52,\
21021011,121,33,24.13,38,53,56.08,\
21021012,121,34,29.99,38,55,36.14,\
21021013,121,35,44.81,38,56,58.08,\
21021014,121,33,52.06,39,01,19.05/)

Only the first column "station IDs" are influenced.  The rest integers return the correct value even though I used "sprintf".  Since the dataset is not long, I can pick out those station IDs and save them in another array.

Best,
Hongyan



danghy at gmail.com
 
From: Dennis Shea
Date: 2015-07-21 11:33
To: danghy
CC: ncl-talk
Subject: Re: [ncl-talk] unexpected results from very simple code
ncl 0> degrees = (/210200051, 121, 36, 43,38,58,26/)
ncl 1> print(typeof(degrees))   ; integer
 
Why did you use 'sprintf' to print an integer?
         print(sprintf("%9.0f",degrees(0)))
Please note the documentation.
---
http://www.ncl.ucar.edu/Document/Functions/Built-in/sprintf.shtml
Converts floats or doubles into formatted strings.
---
http://www.ncl.ucar.edu/Document/Functions/Built-in/sprinti.shtml
Converts integers into formatted strings.
---
 
ncl 2> print(sprinti("%10.0i",degrees(0)))
 
(0)      210200051
 
On Mon, Jul 20, 2015 at 9:19 PM, Rick Brownrigg <brownrig at ucar.edu> wrote:
> Hi Hongyan,
>
> As Arne indicated, with 32-bit floating point, you can't expect more than 7
> significant digits (14 for double), and in your case, you are asking for
> precision 2 orders of magnitude beyond that.
>
> For the truly curious, all the gory reality about computing and
> precision/accuracy is detailed in this doc:
>
> http://www.cse.msu.edu/~cse320/Documents/FloatingPoint.pdf
>
> HTH...
> Rick
>
> On Mon, Jul 20, 2015 at 7:26 PM, danghy at gmail.com <danghy at gmail.com> wrote:
>>
>> Hi Arne,
>>
>> Thanks a lot for the information!  I will probably use "integer" to solve
>> the problem.  But I was really curious why the floating point number returns
>> "210200048", not "210200049"... the perfect case would be that it returns
>> "210200051".
>>
>> Thanks again.
>>
>> Hongyan
>> ________________________________
>> danghy at gmail.com
>>
>>
>> From: Arne Melsom
>> Date: 2015-07-20 17:27
>> To: danghy
>> CC: ncl-talk
>> Subject: Re: [ncl-talk] unexpected results from very simple code
>> Hi Danghy,
>>
>> I think that you can't expect (4 byte) floating point numbers to deliver
>> the accuracy (no. digits) that you request. In your case, I suggest using
>> double precision or integer representation. Here's a part of your example
>> using the original representation and the two alternatives:
>>
>> begin
>>
>> a=210200051.
>> print("output / a :")
>> print(tostring(a))
>> print(sprintf("%9.0f",a))
>>
>> ad=210200051.d
>> print("output / ad:")
>> print(tostring(ad))
>> print(sprintf("%9.0f",ad))
>>
>> ai=210200051
>> print("output / ai:")
>> print(tostring(ai))
>> print(sprinti("%9i",ai))
>>
>> end
>>
>>
>> ...which gives the following output:
>>
>> (0) output / a :
>> (0) 210200048.000000
>> (0) 210200048
>> (0) output / ad:
>> (0) 210200051.000000
>> (0) 210200051
>> (0) output / ai:
>> (0) 210200051
>> (0) 210200051
>>
>> Hope this helps!
>> Arne M.
>>
>>
>>
>> ----- Original Message -----
>> > Dear all,
>> >
>> > I met a problem when reading "station ID" - I've simplified the code as
>> > follows:
>> >
>> > 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"
>> > begin
>> >
>> > degrees = (/210200051, 121, 36, 43,38,58,26/)
>> >
>> > print(sprintf("%9.0f",degrees(0)))
>> > print(tostring(degrees(0)))
>> >
>> > a=210200051.
>> > print(tostring(a))
>> > print(sprintf("%9.0f",a))
>> >
>> > end
>> >
>> > The NCL output on screen is:
>> >
>> > ncl test.ncl
>> > Copyright (C) 1995-2015 - All Rights Reserved
>> > University Corporation for Atmospheric Research
>> > NCAR Command Language Version 6.3.0
>> > The use of this software is governed by a License Agreement.
>> > See http://www.ncl.ucar.edu/ for more details.
>> > (0) 210200048
>> > (0) 210200051
>> > (0) 210200048.000000
>> > (0) 210200048
>> >
>> > Only the second method "tostring" gives the correct station ID. Could
>> > you
>> > please give me a clue why is this happening?
>> >
>> > Appreciate!
>> >
>> >
>> > danghy at gmail.com
>> >
>> > _______________________________________________
>> > ncl-talk mailing list
>> > ncl-talk at ucar.edu
>> > List instructions, subscriber options, unsubscribe:
>> > http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>> >
>>
>>
>> _______________________________________________
>> ncl-talk mailing list
>> ncl-talk at ucar.edu
>> List instructions, subscriber options, unsubscribe:
>> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>>
>
>
> _______________________________________________
> ncl-talk mailing list
> ncl-talk at ucar.edu
> List instructions, subscriber options, unsubscribe:
> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20150721/8e9f2348/attachment.html 


More information about the ncl-talk mailing list