Can float (or double) be set to NaN?

你说的曾经没有我的故事 提交于 2019-12-04 22:28:45

The Float object contains a static value, which is a float type, called NaN.

So

float myFloat = Float.NaN;

gives you what you are asking.

http://download.oracle.com/javase/6/docs/api/java/lang/Float.html#NaN

Sure! NaN is a static constant in the Float and Double classes.

double x = Double.NaN;

Yes

float f = Float.NaN;

See the doc for more info. Note that if you want to compare a number to NaN, you should use isNan().

Despite your question above, this does have a practical purpose. You can use this to indicate a value hasn't been set/provided yet.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!