I found the following from SoloLearn:
I found that 0.0/0.0 makes negative nan(-nan). It makes sense to me that such a mathematical result is undefined, or "not a number". [...]
Furthermore, why is -nan+2^nan=nan?
For example:
#include <iostream>
using namespace std;
int main() {
cout<<0.0/0.0<<endl;
cout<<"important "+to_string(1.0/0.0)+"o:"<<endl;
cout<<"ba"+to_string(0.0/0.0*-1)+"a"<<endl;
cout<<(0.0/0.0)+2^(0.0/0.0*-1);//-nan+2^nan=nan
return 0;
}
But this doesn't suffice my logic..
As far as the C++ language is concerned, the behaviour of the program is undefined. So, from C++ perspective there is no "reason" for anything about the behaviour of the program.
According to the IEEE-754 floating point standard, assuming your hardware and language implementation conform to it (they probably do), result of such division is NaN. IEEE-754 does not specify the sign of NaN for any operation. So from IEEE-754's perspective, the answer is: It just happened to be so on your system for whatever reason the language / hardware implementation has chosen (or simply for no particular reason).
In conclusion: You should never rely on sign of NaN being anything in particular. And you should generally avoid division by zero.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With