Re: print() precision

From: Wei Huang <huangwei_at_nyahnyahspammersnyahnyah>
Date: Wed Jul 31 2013 - 14:39:36 MDT

Fan,

NCL uses "%g" in its C program to print float or double.
This will give 6 digit when print, or convert to string.

The program below can clearly demonstrate that.

If you need more accurate print, please use the sprintf function
(also shown in program below).

Wei

---------------
 a = 1.0d
 b = 11.11d

 do n = 0, 20
    print("No. " + n + ": a(%g) = " + a + ", a(%32.16f) = " + sprintf("%32.16f", a))
 
    a = a * b
 end do

 a = 1.0d
 b = 0.1111d

 do n = 0, 20
    print("No. " + n + ": a(%g) = " + a + ", a(%32.16f) = " + sprintf("%32.16f", a))

    a = a * b
 end do

huangwei@ucar.edu
VETS/CISL
National Center for Atmospheric Research
P.O. Box 3000 (1850 Table Mesa Dr.)
Boulder, CO 80307-3000 USA
(303) 497-8924

On Jul 31, 2013, at 10:14 AM, Fan Fang <Fan.Fang@nasa.gov> wrote:

> Thanks Wei and Dennis.
>
> It works. But if the variable is pre-assigned without 'd' at the end,
> would 'todouble()' somehow do it? I have
>
> begin
> a = 0.4071155678
> b = todouble(a)
> print(b)
> end
>
> and I got something not quite precise:
>
> Copyright (C) 1995-2010 - All Rights Reserved
> University Corporation for Atmospheric Research
> NCAR Command Language Version 5.2.1
> The use of this software is governed by a License Agreement.
> See http://www.ncl.ucar.edu/ for more details.
>
>
> Variable: b
> Type: double
> Total Size: 8 bytes
> 1 values
> Number of Dimensions: 1
> Dimensions and sizes: [1]
> Coordinates:
> (0) 0.4071155786514282
>
>
>
>
> On 07/31/2013 12:07 PM, Dennis Shea wrote:
>> xf = 0.4071155678
>> xd = 0.4071155678d
>>
>> print("xf="+xf +" : "+sprintf("%13.6f", xf))
>> print("xd="+xd +" : "+sprintf("%13.10f", xd))
>>
>>
>> ==============
>>
>> (0) xf=0.407116 : 0.407116
>> (0) xd=0.407116 : 0.4071155678
>>
>>
>> On 7/31/13 9:57 AM, Wei Huang wrote:
>>> Fan,
>>>
>>> Try
>>>
>>> print(0.407115567890123456789d)
>>>
>>> Wei
>>>
>>> huangwei@ucar.edu
>>> VETS/CISL
>>> National Center for Atmospheric Research
>>> P.O. Box 3000 (1850 Table Mesa Dr.)
>>> Boulder, CO 80307-3000 USA
>>> (303) 497-8924
>>>
>>>
>>>
>>>
>>>
>>>
>>> On Jul 31, 2013, at 9:41 AM, Fan Fang <Fan.Fang@nasa.gov> wrote:
>>>
>>>> It appear print() only does floats. IS there an easy way to print out
>>>> just what is fed?
>>>>
>>>> -Fan
>>>>
>>>> begin
>>>> print(0.4071155678)
>>>> end
>>>>
>>>> Copyright (C) 1995-2009 - All Rights Reserved
>>>> University Corporation for Atmospheric Research
>>>> NCAR Command Language Version 5.1.1
>>>> The use of this software is governed by a License Agreement.
>>>> See http://www.ncl.ucar.edu/ for more details.
>>>> (0) 0.4071156
>>>> _______________________________________________
>>>> 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
>>>
>
> _______________________________________________
> 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 Wed Jul 31 14:39:54 2013

This archive was generated by hypermail 2.1.8 : Thu Aug 01 2013 - 15:55:03 MDT