<html><head><meta http-equiv="content-type" content="text/html; charset=UTF-8"><style>body { line-height: 1.5; }blockquote { margin-top: 0px; margin-bottom: 0px; margin-left: 0.5em; }body { font-size: 10.5pt; font-family: 微软雅黑; color: rgb(0, 0, 0); line-height: 1.5; }</style></head><body>
<div><span></span>Thanks, Dennis!</div><div><br></div><div>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:</div><div><span style="background-color: rgba(0, 0, 0, 0); font-size: 10.5pt; line-height: 1.5;">&nbsp;</span></div><div><span style="color: rgb(0, 0, 0); background-color: rgba(0, 0, 0, 0);">degrees&nbsp;=&nbsp;(/&nbsp;\<br>210200051,121,36,43,38,58,26,\<br>210200052,121,33,54,38,57,04,\<br>210200053,121,33,50,38,53,06,\<br>210200054,121,37,59,38,54,43,\<br>210200055,121,37,30,38,51,22,\<br>210200056,121,31,05,38,51,22,\<br>210200057,121,15,10,38,48,22.3,\<br>210200058,121,44,53,39,07,52,\<br>210200059,121,46,37,39,03,04,\<br>210200060,121,58,37,39,03,47,\<br>21021001,121,59,21,39,37,30,\<br>21021002,122,00,41,39,37,01,\<br>21021003,121,58,55,39,24,07,\<br>21021004,121,58,41,39,24,18,\<br>21021005,122,57,29,39,41,54,\<br>21021006,122,58,00,39,40,40,\<br>21021007,122,35,00,39,16,11,\<br>21021008,121,28,17,39,37,48,\<br>21021009,121,24,35,39,26,15,\<br>21021010,121,35,06.11,38,54,04.52,\<br>21021011,121,33,24.13,38,53,56.08,\<br>21021012,121,34,</span><span style="background-color: rgba(0, 0, 0, 0);"><font color="#3366ff">29.99</font></span><span style="color: rgb(0, 0, 0); background-color: rgba(0, 0, 0, 0);">,38,55,36.14,\<br>21021013,121,35,</span><span style="background-color: rgba(0, 0, 0, 0);"><font color="#0000ff">44.81</font></span><span style="color: rgb(0, 0, 0); background-color: rgba(0, 0, 0, 0);">,38,56,58.08,\<br>21021014,121,33,52.06,39,01,19.05/)<br><br></span></div>
<div>Only the first column "station IDs" are influenced. &nbsp;The rest integers return the correct value even though I used "sprintf". &nbsp;Since the dataset is not long, I can pick out those station IDs and save them in another array.</div><div><br></div><div>Best,</div><div>Hongyan</div><div><br></div><hr style="width: 210px; height: 1px;" color="#b5c4df" size="1" align="left">
<div><span><div style="MARGIN: 10px; FONT-FAMILY: verdana; FONT-SIZE: 10pt"><div>danghy@gmail.com</div></div></span></div>
<blockquote style="margin-top: 0px; margin-bottom: 0px; margin-left: 0.5em;"><div>&nbsp;</div><div style="border:none;border-top:solid #B5C4DF 1.0pt;padding:3.0pt 0cm 0cm 0cm"><div style="PADDING-RIGHT: 8px; PADDING-LEFT: 8px; FONT-SIZE: 12px;FONT-FAMILY:tahoma;COLOR:#000000; BACKGROUND: #efefef; PADDING-BOTTOM: 8px; PADDING-TOP: 8px"><div><b>From:</b>&nbsp;<a href="mailto:shea@ucar.edu">Dennis Shea</a></div><div><b>Date:</b>&nbsp;2015-07-21&nbsp;11:33</div><div><b>To:</b>&nbsp;<a href="mailto:danghy@gmail.com">danghy</a></div><div><b>CC:</b>&nbsp;<a href="mailto:ncl-talk@ucar.edu">ncl-talk</a></div><div><b>Subject:</b>&nbsp;Re: [ncl-talk] unexpected results from very simple code</div></div></div><div><div>ncl 0&gt; degrees = (/210200051, 121, 36, 43,38,58,26/)</div>
<div>ncl 1&gt; print(typeof(degrees))&nbsp;&nbsp; ; integer</div>
<div>&nbsp;</div>
<div>Why did you use 'sprintf' to print an integer?</div>
<div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print(sprintf("%9.0f",degrees(0)))</div>
<div>Please note the documentation.</div>
<div>---</div>
<div>http://www.ncl.ucar.edu/Document/Functions/Built-in/sprintf.shtml</div>
<div>Converts floats or doubles into formatted strings.</div>
<div>---</div>
<div>http://www.ncl.ucar.edu/Document/Functions/Built-in/sprinti.shtml</div>
<div>Converts integers into formatted strings.</div>
<div>---</div>
<div>&nbsp;</div>
<div>ncl 2&gt; print(sprinti("%10.0i",degrees(0)))</div>
<div>&nbsp;</div>
<div>(0)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 210200051</div>
<div>&nbsp;</div>
<div>On Mon, Jul 20, 2015 at 9:19 PM, Rick Brownrigg &lt;brownrig@ucar.edu&gt; wrote:</div>
<div>&gt; Hi Hongyan,</div>
<div>&gt;</div>
<div>&gt; As Arne indicated, with 32-bit floating point, you can't expect more than 7</div>
<div>&gt; significant digits (14 for double), and in your case, you are asking for</div>
<div>&gt; precision 2 orders of magnitude beyond that.</div>
<div>&gt;</div>
<div>&gt; For the truly curious, all the gory reality about computing and</div>
<div>&gt; precision/accuracy is detailed in this doc:</div>
<div>&gt;</div>
<div>&gt; http://www.cse.msu.edu/~cse320/Documents/FloatingPoint.pdf</div>
<div>&gt;</div>
<div>&gt; HTH...</div>
<div>&gt; Rick</div>
<div>&gt;</div>
<div>&gt; On Mon, Jul 20, 2015 at 7:26 PM, danghy@gmail.com &lt;danghy@gmail.com&gt; wrote:</div>
<div>&gt;&gt;</div>
<div>&gt;&gt; Hi Arne,</div>
<div>&gt;&gt;</div>
<div>&gt;&gt; Thanks a lot for the information!&nbsp; I will probably use "integer" to solve</div>
<div>&gt;&gt; the problem.&nbsp; But I was really curious why the floating point number returns</div>
<div>&gt;&gt; "210200048", not "210200049"... the perfect case would be that it returns</div>
<div>&gt;&gt; "210200051".</div>
<div>&gt;&gt;</div>
<div>&gt;&gt; Thanks again.</div>
<div>&gt;&gt;</div>
<div>&gt;&gt; Hongyan</div>
<div>&gt;&gt; ________________________________</div>
<div>&gt;&gt; danghy@gmail.com</div>
<div>&gt;&gt;</div>
<div>&gt;&gt;</div>
<div>&gt;&gt; From: Arne Melsom</div>
<div>&gt;&gt; Date: 2015-07-20 17:27</div>
<div>&gt;&gt; To: danghy</div>
<div>&gt;&gt; CC: ncl-talk</div>
<div>&gt;&gt; Subject: Re: [ncl-talk] unexpected results from very simple code</div>
<div>&gt;&gt; Hi Danghy,</div>
<div>&gt;&gt;</div>
<div>&gt;&gt; I think that you can't expect (4 byte) floating point numbers to deliver</div>
<div>&gt;&gt; the accuracy (no. digits) that you request. In your case, I suggest using</div>
<div>&gt;&gt; double precision or integer representation. Here's a part of your example</div>
<div>&gt;&gt; using the original representation and the two alternatives:</div>
<div>&gt;&gt;</div>
<div>&gt;&gt; begin</div>
<div>&gt;&gt;</div>
<div>&gt;&gt; a=210200051.</div>
<div>&gt;&gt; print("output / a :")</div>
<div>&gt;&gt; print(tostring(a))</div>
<div>&gt;&gt; print(sprintf("%9.0f",a))</div>
<div>&gt;&gt;</div>
<div>&gt;&gt; ad=210200051.d</div>
<div>&gt;&gt; print("output / ad:")</div>
<div>&gt;&gt; print(tostring(ad))</div>
<div>&gt;&gt; print(sprintf("%9.0f",ad))</div>
<div>&gt;&gt;</div>
<div>&gt;&gt; ai=210200051</div>
<div>&gt;&gt; print("output / ai:")</div>
<div>&gt;&gt; print(tostring(ai))</div>
<div>&gt;&gt; print(sprinti("%9i",ai))</div>
<div>&gt;&gt;</div>
<div>&gt;&gt; end</div>
<div>&gt;&gt;</div>
<div>&gt;&gt;</div>
<div>&gt;&gt; ...which gives the following output:</div>
<div>&gt;&gt;</div>
<div>&gt;&gt; (0) output / a :</div>
<div>&gt;&gt; (0) 210200048.000000</div>
<div>&gt;&gt; (0) 210200048</div>
<div>&gt;&gt; (0) output / ad:</div>
<div>&gt;&gt; (0) 210200051.000000</div>
<div>&gt;&gt; (0) 210200051</div>
<div>&gt;&gt; (0) output / ai:</div>
<div>&gt;&gt; (0) 210200051</div>
<div>&gt;&gt; (0) 210200051</div>
<div>&gt;&gt;</div>
<div>&gt;&gt; Hope this helps!</div>
<div>&gt;&gt; Arne M.</div>
<div>&gt;&gt;</div>
<div>&gt;&gt;</div>
<div>&gt;&gt;</div>
<div>&gt;&gt; ----- Original Message -----</div>
<div>&gt;&gt; &gt; Dear all,</div>
<div>&gt;&gt; &gt;</div>
<div>&gt;&gt; &gt; I met a problem when reading "station ID" - I've simplified the code as</div>
<div>&gt;&gt; &gt; follows:</div>
<div>&gt;&gt; &gt;</div>
<div>&gt;&gt; &gt; load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"</div>
<div>&gt;&gt; &gt; load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl"</div>
<div>&gt;&gt; &gt; load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"</div>
<div>&gt;&gt; &gt; begin</div>
<div>&gt;&gt; &gt;</div>
<div>&gt;&gt; &gt; degrees = (/210200051, 121, 36, 43,38,58,26/)</div>
<div>&gt;&gt; &gt;</div>
<div>&gt;&gt; &gt; print(sprintf("%9.0f",degrees(0)))</div>
<div>&gt;&gt; &gt; print(tostring(degrees(0)))</div>
<div>&gt;&gt; &gt;</div>
<div>&gt;&gt; &gt; a=210200051.</div>
<div>&gt;&gt; &gt; print(tostring(a))</div>
<div>&gt;&gt; &gt; print(sprintf("%9.0f",a))</div>
<div>&gt;&gt; &gt;</div>
<div>&gt;&gt; &gt; end</div>
<div>&gt;&gt; &gt;</div>
<div>&gt;&gt; &gt; The NCL output on screen is:</div>
<div>&gt;&gt; &gt;</div>
<div>&gt;&gt; &gt; ncl test.ncl</div>
<div>&gt;&gt; &gt; Copyright (C) 1995-2015 - All Rights Reserved</div>
<div>&gt;&gt; &gt; University Corporation for Atmospheric Research</div>
<div>&gt;&gt; &gt; NCAR Command Language Version 6.3.0</div>
<div>&gt;&gt; &gt; The use of this software is governed by a License Agreement.</div>
<div>&gt;&gt; &gt; See http://www.ncl.ucar.edu/ for more details.</div>
<div>&gt;&gt; &gt; (0) 210200048</div>
<div>&gt;&gt; &gt; (0) 210200051</div>
<div>&gt;&gt; &gt; (0) 210200048.000000</div>
<div>&gt;&gt; &gt; (0) 210200048</div>
<div>&gt;&gt; &gt;</div>
<div>&gt;&gt; &gt; Only the second method "tostring" gives the correct station ID. Could</div>
<div>&gt;&gt; &gt; you</div>
<div>&gt;&gt; &gt; please give me a clue why is this happening?</div>
<div>&gt;&gt; &gt;</div>
<div>&gt;&gt; &gt; Appreciate!</div>
<div>&gt;&gt; &gt;</div>
<div>&gt;&gt; &gt;</div>
<div>&gt;&gt; &gt; danghy@gmail.com</div>
<div>&gt;&gt; &gt;</div>
<div>&gt;&gt; &gt; _______________________________________________</div>
<div>&gt;&gt; &gt; ncl-talk mailing list</div>
<div>&gt;&gt; &gt; ncl-talk@ucar.edu</div>
<div>&gt;&gt; &gt; List instructions, subscriber options, unsubscribe:</div>
<div>&gt;&gt; &gt; http://mailman.ucar.edu/mailman/listinfo/ncl-talk</div>
<div>&gt;&gt; &gt;</div>
<div>&gt;&gt;</div>
<div>&gt;&gt;</div>
<div>&gt;&gt; _______________________________________________</div>
<div>&gt;&gt; ncl-talk mailing list</div>
<div>&gt;&gt; ncl-talk@ucar.edu</div>
<div>&gt;&gt; List instructions, subscriber options, unsubscribe:</div>
<div>&gt;&gt; http://mailman.ucar.edu/mailman/listinfo/ncl-talk</div>
<div>&gt;&gt;</div>
<div>&gt;</div>
<div>&gt;</div>
<div>&gt; _______________________________________________</div>
<div>&gt; ncl-talk mailing list</div>
<div>&gt; ncl-talk@ucar.edu</div>
<div>&gt; List instructions, subscriber options, unsubscribe:</div>
<div>&gt; http://mailman.ucar.edu/mailman/listinfo/ncl-talk</div>
<div>&gt;</div>
</div></blockquote>
</body></html>