Skip to content Skip to sidebar Skip to footer

Double.parsedouble() Returns Infinity When I Thought It Should Be Numberformatexception

There is astrange and unexpected behavior of Double.parseDouble() method: Double.parseDouble('4cff9d79-a696-4dfc-89f9-a265ae117257'); That didn't thrown NumberFormatException as e

Solution 1:

Double.parseDouble() code:

if (result.e < -1024) {
    result.zero = true;
    return result;
} elseif (result.e > 1024) {
    result.infinity = true;
    return result;
}

Double has the exponencial view MeP, where M - mantissa and P - exponent (MeP is equal to M*10^P). Android firstly checks if exponent exists, and if it is > 1024, decides it is Infinity and stops any other verifications.

In this code we can see, that in case if after e letter is any negative number, less than 1024, this number is recognized as correct but equal to zero.

Double.parseDouble("Any characterse1025"); //InfinityDouble.parseDouble("Any characterse-1025"); //0.0

And our case whit UUIDs:

Double.parseDouble("4cff9d79-a696-4dfc-89f9-a265ae117257"); //InfinityDouble.parseDouble("4cff9d79-a696-4dfc-89fe-126534117257"); //0.0

So, it is not safe to use this method. Look for the alternatives.

UPD:Original investigation page (ru). Translated not for a rate.

UPD2: Bug is already fixed.

Post a Comment for "Double.parsedouble() Returns Infinity When I Thought It Should Be Numberformatexception"