[CppAD] Own Data Type

Robert P. Goddard robert_goddard at apl.washington.edu
Tue Mar 1 18:03:30 EST 2011


This thread is troublesome in several respects.

First, Dominik presents a class he calls MyType, which contains a single 
member "d" of type qd_real, and defines a whole lot of standard 
operations on it -- all of which simply delegate the operation to d.  
Why does MyType exist at all? If you are going to use it with CppAD, why 
not CppAD<qd_real> instead of CppAD<MyType>?

Second, Dominik's second message asks about the effect of the expression

Y[0]=X[0]-234.123456789123456789123456789123456789;

The C++ standard (old and new) tell us unambiguously what that means: 
The long constant on the right is a double, pure and simple, and it 
retains only the precision native to a double -- silently. The C++ 2010 
Draft of 8/21/2010 says in part 2.14.4 "Floating Literals:

> The type of a floating literal is double unless explicitly specified 
> by a suffix. The suffixes f and F specify float, the suffixes l and L 
> specify long double.

If you want the compiler to recognize the extra digits in your constant, 
you MUST use the "L" or "l" suffix -- which works if and only if your 
compiler recognizes the "long double" type defined in the new 
proto-standard C++. And then it works only to the extent that the 
built-in type has enough precision to represent a qd_real, AND qd_real 
has a "-" operation that recognizes "long double", or a non-"explicit" 
conversion constructor with argument of type "long double". Which this 
one doesn't have. So it's a double.

Take-away message: Literals are defined by the language, and must be 
supported by the compiler. If you really want your mega-precision number 
to be initialized to full precision, the obvious way to do it is define 
a constructor that takes a string argument. In that case, the expression 
above would take the form

Y[0]=X[0]-MyType("234.123456789123456789123456789123456789");

All of this is independent of CppAD. It's just C++.

Bob Goddard

-------------- next part --------------
A non-text attachment was scrubbed...
Name: robert_goddard.vcf
Type: text/x-vcard
Size: 384 bytes
Desc: not available
Url : http://list.coin-or.org/pipermail/cppad/attachments/20110301/0c815edf/attachment.vcf 


More information about the CppAD mailing list