Login  Register

RE: Null<double> and arithmetics

Posted by Vadim Ogranovich-3 on May 09, 2002; 11:21am
URL: http://quantlib.414.s1.nabble.com/Null-double-and-arithmetics-tp2054p2055.html

I did some research / experiments with g++3.0.4 on Linux. They have isnan(),
et. al. defined in <cmath>. Below is the code that generates a NaN and
confirms that the NaN behaves as expected.

//foo.cpp
#include <iostream>
#include <cmath>

using namespace std;

int main()
{
  double x = 1.0/0.0;

  cout << "NaN=" << isnan(x) << " Inf=" << isinf(x) << '\n';

  x = 0.0/0.0;

  cout << "NaN=" << isnan(x) << " Inf=" << isinf(x) << '\n';

  x *= 0.1;
  cout << "NaN=" << isnan(x) << " Inf=" << isinf(x) << '\n';

  return 0;
}

$ ./foo
NaN=0 Inf=1
NaN=1 Inf=0
NaN=1 Inf=0

I'd wrap Null<double> around this.


Another comment. I find the b) proposal overly restrictive for doubles at
least. I do want to be able store NaN in a double to indicate that a value
is either not initialized or can not possibly have a reasonable value, e.g.
implied volatility of an option traded below intrinsic value, or whatever.

I have no opinion what to do about Null<int>. I can see a situation where
Null<int> can be involved in arithmetics, for example a class can represent
the number of stock shares by int, set it to Null<int> when the value is not
known, and then compute price*shares and expect it to come out Null<double>.
However I am not sure that all this is worth the trouble given that there is
no compiler support for this.

Thanks, Vadim

-----Original Message-----
From: Luigi Ballabio [mailto:[hidden email]]
Sent: Thursday, May 09, 2002 7:27 AM
...

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


_______________________________________________________________

Have big pipes? SourceForge.net is looking for download mirrors. We supply
the hardware. You get the recognition. Email Us: [hidden email]
_______________________________________________
Quantlib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users

--------------------------------------------------
DISCLAIMER
This e-mail, and any attachments thereto, is intended only for use by the
addressee(s) named herein and may contain legally privileged and/or
confidential information.  If you are not the intended recipient of this
e-mail, you are hereby notified that any dissemination, distribution or
copying of this e-mail, and any attachments thereto, is strictly prohibited.
If you have received this e-mail in error, please immediately notify me and
permanently delete the original and any copy of any e-mail and any printout
thereof.

E-mail transmission cannot be guaranteed to be secure or error-free.  The
sender therefore does not accept liability for any errors or omissions in
the contents of this message which arise as a result of e-mail transmission.

NOTICE REGARDING PRIVACY AND CONFIDENTIALITY

Knight Trading Group may, at its discretion, monitor and review the content
of all e-mail communications.