Java parseFloat() 与 parseDouble()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22970240/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 19:41:51  来源:igfitidea点击:

parseFloat() vs. parseDouble()

javastringfloating-pointdouble

提问by vikifor

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

我正在尝试使用下面的代码将字符串转换为 float 和 double 但得到不同的结果。

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?

有人能解释一下为什么我在 float 和 double 中解析字符串时得到不同的结果吗?float 和 double 的范围是多少?

回答by Farmer Joe

The difference lies in the fact that Doubleand Floatstore numbers differently.

不同之处在于DoubleFloat存储数字不同。

With Single and Double precision, allowing Doubleto give Doublethe amount of precision the Floatcan handle.

使用单精度和双精度,允许Double精度Float可以处理的量。

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

所以简单的答案是,由于FloatDouble信息相比的有限内存在转换超出Float可以处理的范围的数字时丢失。

  • Float: 32-bit Numbers
  • Double: 64-bit Numbers
  • Float: 32 位数字
  • Double: 64 位数字

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

所以在转换中,一些信息在转换为 时会丢失,Float因为它被截断了。

Generally...

一般来说...

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

Float将数字存储为符号 ( -/+) 的 1 位、指数的 8 位和分数的 23 位。

Doublestores 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 = 111001001111111010111111101bhas 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 位的信息,这些信息可以被Double53 位允许覆盖,但不能被Float23 位允许覆盖,并且数据在最低有效端被截断。

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

转换将使用 23 位将数字四舍五入到最接近的可表示数字1.20059392 = 111001001111111011000000000b,指数将处理其余的扩展。

回答by rgettman

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

这是因为您试图float通过为其提供比它可以处理的更多位的精度来解析 a 。那个大的“ulp”(最后一个单位)float8.0,但是那个大的“ulp”double仍然相当小。也就是说,在那个量级,最接近的可能float值相差8,但最接近的double值,精度更高,相差小得多。

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

This prints:

这打印:

8.0
1.4901161193847656E-8

So the Floatparser must use the closest floatto the true value of 120059389, which happens to be 1.20059392E8.

所以Float解析器必须使用最接近float的真实值120059389,也就是1.20059392E8.

回答by CramerTV

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. 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'. The 64 bit 'double' has more bits and can be more precise with larger numbers.

较早的链接和答案提供了很好的技术答案。“外行”的答案是浮点数为 32 位,双精度数为 64 位。这些位中的一些用于数字,一些用于指数。您在代码中输入的数字对于 32 位“浮点数”来说数字太多。64 位“double”具有更多位,并且可以使用更大的数字更精确。

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 位精度时,同样的概念适用于更大的数字。