[英]parseFloat() vs. parseDouble()
I am trying the code below to convert string to float and double but getting different results. 我正在尝试下面的代码将字符串转换为float和double但得到不同的结果。
System.out.println(Float.parseFloat("120059389"));
System.out.println(Double.parseDouble("120059389"));
1.20059392E8
1.20059389E8
Could somebody explain me why I got different result for parsing string in float and double? 有人可以解释一下为什么我在float和double中解析字符串会得到不同的结果吗? What are the ranges for float and double?
float和double的范围是多少?
This is because you're trying to parse a float
by giving it more digits of precision than it can handle. 这是因为你试图通过给它提供比它能处理的更多精度数字来解析
float
。 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. 在一个的“ULP”(最后的地方单位)
float
:大的8.0
,但“ULP”的double
那大仍然是相当小的。 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. 也就是说,在该幅度上,最接近的可能
float
值相差8
,但是具有更高精度的最接近的double
值相差更远。
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
. 因此,
Float
解析器必须使用最接近的float
到120059389
的真值,恰好是1.20059392E8
。
The difference lies in the fact that Double
and Float
store numbers differently. 不同之处在于
Double
和Float
不同的方式存储数字。
With Single and Double precision, allowing Double
to give Double the amount of precision the Float
can handle. 使用单精度和双精度,允许
Double
为Float
提供双倍的精度。
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
的内存有限,与Double
信息相比,在Float
可以处理的范围之外的数字转换时会丢失。
Float
: 32-bit Numbers Float
:32位数字 Double
: 64-bit Numbers Double
:64位数字 So in the conversion some information is lost when converting to the Float
because it is truncated. 因此在转换过程中,一些信息在转换为
Float
时会丢失,因为它会被截断。
Generally... 通常...
Float
stores numbers as 1 bit for the sign ( -/+
), 8 bits for the Exponent, and 23 bits for the fraction. Float
将数字存储为符号( -/+
)的1位,指数的8位,以及分数的23位。
Double
stores numbers as 1 bit for the sign ( -/+
), 8 bits for the Exponent, and 53 bits for the fraction. Double
将数字存储为符号( -/+
)的1位,指数的8位,以及分数的53位。
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. 当您转换您的号码时,
120059389 = 111001001111111010111111101b
有27位信息,可以由Double
的53位120059389 = 111001001111111010111111101b
覆盖,但不能由Float
的23位120059389 = 111001001111111010111111101b
覆盖,并且数据在最不重要的一端被截断。
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. 转换将使用23位
1.20059392 = 111001001111111011000000000b
将数字四舍五入到最接近的可表示数字,并且指数将处理剩余的扩展。
The earlier links and answers give good technical answers. 较早的链接和答案提供了很好的技术答案。 The 'laymans' answer is that a float is 32 bits and a double is 64 bits.
'laymans'的答案是浮点数为32位,双精度值为64位。 Some of those bits are used for the number and some are used for the exponent.
其中一些位用于数字,一些用于指数。 The number you put in your code simply had too many digits for the 32 bit 'float'.
你在代码中输入的数字只有32位'float'的数字太多。 The 64 bit 'double' has more bits and can be more precise with larger numbers.
64位“双”具有更多位,并且对于更大的数字可以更精确。
The same concept holds for even larger numbers when you reach the limits of a 64 bit double and need 128 bits of precision. 当达到64位双精度并且需要128位精度时,相同的概念适用于更大的数字。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.