I appreciate this is similar to this question What is the difference between IND and NAN numbers, but that question did not really get the answer I needed.
My understanding is that (with VC++) a IND (indefinite number) is caused from a mathematical operation that cannot be performed and is not infinity. Such as INF*0.0
, INF/INF
or 0.0/0.0
. This is different to a "regular" NaN which can be gotten from std::numeric_limits<double>::quiet_nan()
.
I have a function which I would like to return IND in some circumstances. For example in a function which derives the mean of a std::vector<double>
I would like to return IND
if the vector has zero elements.
If I do
return 0.0/0.0;
I get a compiler error C2124 I can divide by the result of a function that I know will return zero, but even then the compiler is often clever enough to realise this and issue warning C4723: potential divide by 0.
Is there a way to access this specific NaN without generating compiler errors or warnings?
Edit after comment by @adrian-mole
The following code
#include <iostream>
#include<limits>
int main()
{
std::cout << "numeric_limits nan = " << std::numeric_limits<double>::quiet_NaN() << "\n";
double zero = 0.0;
std::cout << "1.0/0.0 = " << 1.0 / zero << "\n";
std::cout << "0.0/0.0 = " << 0.0 / zero << "\n";
std::cout << "sqrt(-1.0) = " << std::sqrt(-1.0) << "\n";
}
when compiled with visual studio 2019 outputs
numeric_limits nan = nan
1.0/0.0 = inf
0.0/0.0 = -nan(ind)
sqrt(-1.0) = -nan(ind)
This potentially (but not necessarily) contradicts this answer https://stackoverflow.com/a/52491720/1051805
According to Wikipedia https://en.wikipedia.org/wiki/IEEE_754:
Due to the possibility of multiple encodings (at least in formats called interchange formats), a NaN may carry other information: a sign bit (which has no meaning, but may be used by some operations) and a payload, which is intended for diagnostic information indicating the source of the NaN
Which I assume is what is happening here.
Is it possible for me to access the -nan(ind) type of nan directly without generating compiler errors or warnings?