Getting indefinite value «SOLVED»✓

Dec 10, 2008 at 1:34pm
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)
Last edited on Dec 10, 2008 at 10:22pm
Dec 10, 2008 at 2:30pm
Why do you want to get the value in the first place?
Dec 10, 2008 at 2:54pm
What I whant is something like this:
1
2
3
4
5
6
double d = //Some Calculations
if (d==numeric_limits<double>::infinity())
    cout << "infinity";
else if ( d==/*indefinite value*/ )
    cout << "indefinite";
else cout << d;

So now it is basically just for making my own output but in future I'll need that for some other calculations


Of course I can get that value in some ways:
1
2
3
4
numeric_limits<double>::infinity()-numeric_limits<double>::infinity();
//or
double z=0.0;
0.0/z;//If I type 0.0/0.0 I get a compiler error 
But I would like to have something shorter
Dec 10, 2008 at 3:40pm
Wouldn't it be easier to check the parameters?
Dec 10, 2008 at 4:36pm
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...

PS: I've noticed that
numeric_limits<double>::infinity()-numeric_limits<double>::infinity() != 0/0
.

Edit: I need to know how to make a double to be indefinite for cases ∞0 and 1 because the math.h pow() does not return so
Last edited on Dec 10, 2008 at 4:45pm
Dec 10, 2008 at 4:48pm
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.
Last edited on Dec 10, 2008 at 4:50pm
Dec 10, 2008 at 5:11pm
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?
Last edited on Dec 10, 2008 at 5:15pm
Dec 10, 2008 at 5:14pm
Neither INF nor IND have "values" so there are no mathematical operations you can perform on them to get meaningful results.
Dec 10, 2008 at 5:30pm
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...)
Last edited on Dec 10, 2008 at 6:30pm
Dec 10, 2008 at 9:29pm
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.
Dec 10, 2008 at 10:20pm
Thanks exception, I've found _isnan() from float.h and that works
(numeric_limits<double>::is_iec559 returns true)
Dec 11, 2008 at 6:33pm
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)
Last edited on Dec 11, 2008 at 6:36pm
Dec 11, 2008 at 10:08pm
Yes, that is working too.
Thanks again!
Topic archived. No new replies allowed.