[Trilinos-Users] Trilinos-Users Digest, Vol 84, Issue 10

Hoemmen, Mark mhoemme at sandia.gov
Fri Aug 17 12:14:20 MDT 2012


Thanks, Markus, for pointing this out.  It should be easy for us to fix this by having the specialization of toString for ArrayView<T> simply use the specialization of toString for T.  Right now, it uses operator<< directly in objects of type T, instead of deferring to T's toString specialization.  I will file a Teuchos bug for this; if you are interested in tracking progress, please e-mail me directly.

Thanks!
mfh


On Aug 17, 2012, at 12:00 PM, "trilinos-users-request at software.sandia.gov" <trilinos-users-request at software.sandia.gov> wrote:
> Message: 1
> Date: Thu, 16 Aug 2012 12:56:09 -0600
> From: "Markus Berndt" <berndt at lanl.gov>
> Subject: Re: [Trilinos-Users] precision double in for Teuchos
>    XMLObject ostream
> To: "Hoemmen, Mark" <mhoemme at sandia.gov>
> Cc: "trilinos-users at software.sandia.gov"
>    <trilinos-users at software.sandia.gov>
> Message-ID: <1345143369.3423.10.camel at handel.lanl.gov>
> Content-Type: text/plain; charset=utf-8
> 
> It looks like this fix described by Mark (see below in the quoted email)
> was included in 10.12. However, for Array<double> and Array<float> the
> problem persists. 
> 
> I believe that the function
> 
> std::string ArrayView<T>::toString() const
> 
> needs to be updated/specialized such that for T=double and T=float high
> enough precision is used to represent a number accurately in the XML
> file.
> 
> - Markus
> 
> 
> On Mon, 2012-03-05 at 04:51 +0000, Hoemmen, Mark wrote:
>> On Sat 03 Mar 2012 at 16:12, George Pau <gpau at lbl.gov> wrote:
>>> I have a code that read in an XML file, save it as a ParameterList
>>> and then write it to a new XML file again.  What I notice is that
>>> the precision of parameter of type double drops in the new XML file.
>>> For example, even though I specified 7 decimal points in the
>>> original XML file, my new XML file shows the same value to only 5
>>> decimal points.  Is there any way that I can maintain the precision
>>> in my new XML file?
>> 
>> I took a brief look at the conversion code, and it seems like the
>> issue is with Teuchos::ToStringTraits.  This traits class implements
>> conversion to strings (via Teuchos::toString()) for different types T.
>> The default implementation of ToStringTraits creates an
>> std::ostringstream, writes the object of type T to the ostringstream,
>> and returns the std::string version of the ostringstream.  Teuchos
>> does not override ToStringTraits for floating-point types, so
>> toString() uses the default implementation in that case.
>> 
>> This would explain why setting the precision of floating-point data in
>> the output file, as you do, has no effect.  The XML output code first
>> converts the floating-point value to a string (via toString()), then
>> writes the string.  This bypasses the precision you set for the output
>> file.
>> 
>> This is really a bug that we should fix, so thank you for pointing
>> this out!  If you need a workaround, you could simply specialize
>> Teuchos::ToStringTraits for T = double, using an excessive precision.
>> I like to use round(52 * log10(2)) + 1 = 17 for double-precision real
>> values (please feel free to correct me if I've gotten this wrong).
>> 
>> namespace Teuchos {
>> template<>
>> class ToStringTraits<double> {
>> public:
>>  static std::string toString (const double& t) {
>>    std::ostringstream os;
>>    os.setf (std::ios::scientific);
>>    os.precision (17);
>>    os << t;
>>    return os.str();
>>  }
>> };
>> 
>> There is, in fact, an algorithm, due to Guy Steele (yes, Java's Guy
>> Steele) et al., for idempotent printing of finite-length
>> floating-point values.  One of these days it would be nice for us to
>> take the time to implement that algorithm and test it carefully.  For
>> now, cranking up the precision works fine.
>> 
>> Best,
>> mfh
>> 
>> _______________________________________________
>> Trilinos-Users mailing list
>> Trilinos-Users at software.sandia.gov
>> http://software.sandia.gov/mailman/listinfo/trilinos-users
>> 
> 
> -- 
> Markus Berndt -- LANL CCS-2 -- (505) 665-4711
> 
> 
> 
> 
> ------------------------------
> 
> _______________________________________________
> Trilinos-Users mailing list
> Trilinos-Users at software.sandia.gov
> http://software.sandia.gov/mailman/listinfo/trilinos-users
> 
> 
> End of Trilinos-Users Digest, Vol 84, Issue 10
> **********************************************



More information about the Trilinos-Users mailing list