What's the best way to use NaNs in C++?
I found std::numeric_limits<double>::quiet_NaN()
and std::numeric_limits<double>::signaling_NaN()
. I'd like to use signaling_NaN
to represent an uninitialized variable as follows:
double diameter = std::numeric_limits<double>::signaling_NaN();
This, however, signals (raises an exception) on assignment. I want it to raise an exception on use, not on assignment.
Is there any way to use signaling_NaN
without raising an exception on assignment? Is there a good, portable alternative to signaling_NaN
that will raise a floating point exception when used?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…