1

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?

Phil Rosenberg
  • 1,597
  • 1
  • 14
  • 22
  • There is no *actual* separate representation for your `IND` over the IEEE standard `NAN`. You are maybe just getting confused with the way the MSVC compiler outputs a NaN value. See [What do 1.#INF00, -1.#IND00 and -1.#IND mean?](https://stackoverflow.com/a/52491720/10871073) for further discussiion. – Adrian Mole Mar 11 '20 at 10:30
  • Take a look at https://www.codeproject.com/Articles/824516/Concept-of-NaN-IND-INF-and-DEN . The guy there uses `const unsigned long lnIND[2] = {0x00000000, 0xfff80000}; const double AN_INDETERMINATE = *( double* )lnIND;`. Kinda ugly code, though. – Aziuth Mar 11 '20 at 11:36
  • Thing is, if I'd use your code as a black box, I wouldn't expect some rather exotic stuff like IND to be returned. I'd expect it to be robust (like an empty vector results in 0 being returned, depends on the situation) or to have an error code or to throw an exception, maybe set a implicitly given boolean. Otherwise, my code would simply continue to work with what yours returned as if it was a regular output, resulting then in my code to produce an error. Or worse, not, and carry that value on instead. – Aziuth Mar 11 '20 at 11:37
  • In modern C++ you can always write even floats as hex values so that you can generate any bit pattern you like. – Peter - Reinstate Monica Mar 11 '20 at 11:37
  • *For example in a function which derives the mean of a std::vector I would like to return IND if the vector has zero elements.* IF that is your use case, can I convince you to use a `std::optional` instead? That would allow you to return `nullopt` when there is no mean to compute instead of trying to specific hard to get value. – NathanOliver Mar 11 '20 at 12:28

0 Answers0