What string should I pass to java.lang.Long.parseLong() to return a NaN?
I unfortunately cant use expressions like Long.Nan as the string is actually a return value from a different C module. Is there a string开发者_开发问答 I can pass to parseLong() to return an NaN ?
There is no Long.NaN -- you are confused.
For Double.NaN, how about this:
public double myParseDouble(String s)
{
double result;
try
{
result = Double.parseDouble(s);
}
catch (NumberFormatException nfe)
{
result = Double.NaN;
}
return result;
}
(edit: and the obvious approach is to pass in any string that is an invalid double, e.g. the empty string or NaN)
There is no such thing as Long
.NaN
, which makes sense when you consider that every bit pattern within long represents a valid integer within the range of [Long.MIN_VALUE
, Long.MAX_VALUE
].
You might consider trying to get the bit pattern of Double.NaN
instead.
Long.Nan? That doesn't exist in Java. You only have Long.MIN_VALUE and Long.MAX_VALUE
"Nan" as argument to Double.parseDouble(String)
gives Double.NaN as value if that is what you want.
java.lang.Long
represent 64-bit signed integral value, while NaN
is introduced only in floating-point computations. That being said there is no special string return Long NaN
, but you can for instance use:
double notANumber = java.lang.Double.NaN;
Java only has a concept of NaN for floating point datatypes, not integer ones such as Long
.
精彩评论