Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

parseFloat() vs. parseDouble()

I am trying the code below to convert string to float and double but getting different results.

Code:

 System.out.println(Float.parseFloat("120059389"));
 System.out.println(Double.parseDouble("120059389"));

Output:

1.20059392E8
1.20059389E8

Could somebody explain me why I got different result for parsing string in float and double? What are the ranges for float and double?

like image 382
vikifor Avatar asked Apr 09 '14 17:04

vikifor


People also ask

What is the purpose of the parseFloat () method?

The parseFloat() function is used to accept the string and convert it into a floating-point number. If the string does not contain a numeral value or If the first character of the string is not a Number then it returns NaN i.e, not a number.

Is parseFloat expensive?

It is a fairly expensive operation if you need to do this many, many times but can be less expensive than Double. toString() or Integer. toString() if you used those to create the data in the first place.

What is parseFloat in JS?

The parseFloat() function is used to accept a string and convert it into a floating-point number. If the input string does not contain a numeral value or If the first character of the string is not a number then it returns NaN i.e, not a number.

Does JavaScript have a parseDouble?

JavaScript only has one kind of number: "number", which is a IEEE-754 Double Precision ("double") value. As such, parseFloat in JavaScript is the equivalent of a "parse double" in other languages.


2 Answers

This is because you're trying to parse a float by giving it more digits of precision than it can handle. The "ulp" (unit in last place) of a float that big is 8.0, but the "ulp" for a double that big is still reasonably small. That is, at that magnitude, the closest possible float values differ by 8, but the closest double values, with more precision, differ by far less.

System.out.println(Math.ulp(120059389f));
System.out.println(Math.ulp(120059389d));

This prints:

8.0
1.4901161193847656E-8

So the Float parser must use the closest float to the true value of 120059389, which happens to be 1.20059392E8.

like image 91
rgettman Avatar answered Oct 02 '22 10:10

rgettman


The difference lies in the fact that Double and Float store numbers differently.

With Single and Double precision, allowing Double to give Double the amount of precision the Float can handle.

So the simple answer is that because of Float's limited memory in comparison to Double information is lost upon conversion of numbers out of the range Float can handle.

  • Float : 32-bit Numbers
  • Double : 64-bit Numbers

So in the conversion some information is lost when converting to the Float because it is truncated.

Generally...

Float stores numbers as 1 bit for the sign (-/+), 8 bits for the Exponent, and 23 bits for the fraction.

Double stores numbers as 1 bit for the sign (-/+), 8 bits for the Exponent, and 53 bits for the fraction.

When you convert your number 120059389 = 111001001111111010111111101b has 27 bits worth of information which can be covered by the Double's 53 bit allowence, but not by the Float's 23 bit allowance, and the data is truncated at the least significant end.

The conversion will round the number to the nearest representable number using 23 bits 1.20059392 = 111001001111111011000000000b and the exponent will handle the rest of the expansion.

like image 25
Farmer Joe Avatar answered Oct 02 '22 08:10

Farmer Joe