java 浮点数(或双精度数)可以设置为 NaN 吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7419241/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can float (or double) be set to NaN?
提问by ahodder
Note: Similar to Can an integer be NaN in C++?
注意:类似于C++ 中的整数可以是 NaN 吗?
I understand this has little practical purpose, but can a float
or double
be set to NaN
?
我知道这几乎没有实际用途,但是可以将 afloat
或double
设置为NaN
吗?
采纳答案by Codemwnci
The Float object contains a static value, which is a float
type, called NaN
.
Float 对象包含一个静态值,它是一种float
类型,称为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
http://download.oracle.com/javase/6/docs/api/java/lang/Float.html#NaN
回答by japreiss
Sure! NaN is a static constant in the Float and Double classes.
当然!NaN 是 Float 和 Double 类中的静态常量。
double x = Double.NaN;
回答by Brian Agnew
Yes
是的
float f = Float.NaN;
See the docfor more info. Note that if you want to compare a number to NaN, you should use isNan().
有关更多信息,请参阅文档。请注意,如果要将数字与 NaN 进行比较,则应使用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.
尽管您提出了上述问题,但这确实有实际用途。您可以使用它来指示尚未设置/提供的值。