Login  Register

Re: Null<double> and arithmetics

Posted by Luigi Ballabio-4 on May 09, 2002; 7:08am
URL: http://quantlib.414.s1.nabble.com/Null-double-and-arithmetics-tp2054p2056.html

At 02:17 PM 5/7/02 -0500, Vadim Ogranovich wrote:
>Life may be more simple than I thought, it seems like comipilers have
>support for NaN and Inf numbers. Below is a VC++ help page that makes me
>think this is the case

I've been looking at the C++ standard and it defines a <limits> header with
a template class numeric_limits<Type>. However, support for it is kind of
erratic, namely:

gcc 2.95.x : doesn't implement <limits>

gcc 3.0.x  : std::numeric_limits<int>::has_quiet_NaN = false
              std::numeric_limits<double>::has_quiet_NaN = false

Borland C++: std::numeric_limits<int>::has_quiet_NaN = false
              std::numeric_limits<double>::has_quiet_NaN = true
              std::numeric_limits<double>::quiet_NaN() / 100.0  -> crashes

Visual C++ : std::numeric_limits<int>::has_quiet_NaN = true
              std::numeric_limits<double>::has_quiet_NaN = true
              std::numeric_limits<double>::quiet_NaN() / 100.0  -> gives a NaN

so in this case, surprisingly enough, VC++ is the compiler with the best
support of the standard...

I think what we can do is:

a) we can #define Null<T> in a compiler-dependent way, so that we get to
use NaNs for VC++ and stick to some ql-defined value for the other compilers;

b) or we can redefine Null<T> to throw an error when one tries to cast it
to an actual int or double or whatever T is. Albeit rudely, this would
notify the user, should he ever try to do Null<double>()/100.0, instead of
letting wrong values enter the computations.

c) both of the above (NaN for VC++, throw for the others)

Thoughts?

Bye,
                 Luigi