I'm working on a program computing some "strange" calculations using doubles.
If I divide a number by 0, I get 1.#INF which is the same value as numeric_limits<double>::infinity().
When I make some calculations with indefinite result (eg: 0/0, ∞-∞) I get as output -1.#IND.
What should I do if I want to get that value without calculations?
(I can't find the correct numeric_limits member)
You mean in my calculations?
I'm writing a calculator so I would have to check for all the indefinite cases
( 0/0, ∞-∞, ∞/∞, 0∙∞, 1∞, 00, ∞0)
and that would increase lines on my operation solver functions...
First of all, you can't use infinity as a parameter. Unless you're writing something that solves calculus problems. But you can't infinity directly then, anyway. You have to emulate it through logic.
Second, only x/0 and a couple of other operations, like tan(pi/2), have undefined results. 0/x is perfectly valid.
Third, 0^0==1. You can try it out.
Well, when I studied Limits at school my professor told me that the cases I posted are all indefinite so I think what he told me was right...
And if I try
pow(numeric_limits<double>::infinity(),6);
it returns me an infinite value (as expected).
and if I give this input to my calculator:
INF+1
(INF returns infinity) I get infinity as output even if I didn't tell my calculator how to cope with it
But I don't have problems with my calculations: I just want to know whether a double is holding an indefinite value, is any function avaiable to know this?
All operations I've tried using INF worked properly (with +-*/) just using doubles and numeric_limits<double>::infinity()
(EDIT) As I said before, I don't have problem in calculations, I'm just asking if is possible to know whether a variable has an indefinite value
(and if it is of course I'd like to know how...)
I assume you use IEEE 754. You could always check it via std::numeric_limits<>::is_iec559
In IEEE 754, there are several representations for nan values, so when performing different operations you may get different results (the exponent consists of ones, the significant isn't zero). You can check for it via isnan() in <math.h>, but it isn't part of the C++ standard (doubles aren't required to be IEEE 754 or to have a nan value.) So best thing might be to avoid them (on the other hand, chances are that the target processor has IEEE arithmetic - if not, you would know about it as it would be highly exotic).
As for the usage of *infinite* values: when doing IEEE arithmetic, they behave as expected, e.g. +inf*0 results in nan, +inf*x, x>0 results in +inf etc. Any operation including a nan operand results in a nan value.
I suspect the function is in global namespace. These names are reserved for the compiler vendor, so this function is compiler (-version) specific. Perhaps you should exploit another property of nan values: they evaluate as unordered, even when compared to themselves. A recommendation from the appendix (7) of the IEEE 754 standard is to check x != x, which will evaluate true iff x is a nan value.
(Sorry that I tell this only now, I had to look it up myself... I usually try avoid nans)