Java中float和double的包含范围是多少?

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

What is the inclusive range of float and double in Java?

javatypesfloating-pointdoublerange

提问by Catharsis

What is the inclusive range of float and double in Java?

Java中float和double的包含范围是多少?

Why are you not recommended to use float or double for anything where precision is critical?

为什么不建议在精度至关重要的任何地方使用 float 或 double ?

采纳答案by jjnguy

Java's Doubleclass has members containing the Min and Max value for the type.

Java 的Double类具有包含该类型的 Min 和 Max 值的成员。

2^-1074 <= x <= (2-2^-52)·2^1023 // where x is the double.

Check out the Min_VALUEand MAX_VALUEstatic final members of Double.

退房Min_VALUEMAX_VALUE静态最终成员Double

(some)People will suggest against using floating point types for things where accuracy and precision are critical because rounding errors can throw off calculations by measurable (small) amounts.

(一些)人们会建议不要在准确性和精度至关重要的事情上使用浮点类型,因为舍入误差可以通过可测量的(小)量来破坏计算。

回答by unwind

Of course you can use floats or doubles for "critical" things ... Many applications do nothing but crunch numbers using these datatypes.

当然,您可以将浮点数或双精度数用于“关键”的事情……许多应用程序除了使用这些数据类型处理数字之外什么都不做。

You might have misunderstood some of the various caveats regarding floating-point numbers, such as the recommendation to never compare for exact equality, and so on.

您可能误解了一些有关浮点数的各种警告,例如永远不要比较完全相等的建议,等等。

回答by Pascal Thivent

From Primitives Data Types:

原语数据类型

  • float:The floatdata type is a single-precision 32-bit IEEE 754 floating point. Its range of values is beyond the scope of this discussion, but is specified in section 4.2.3of the Java Language Specification. As with the recommendations for byteand short, use a float(instead of double) if you need to save memory in large arrays of floating point numbers. This data type should never be used for precise values, such as currency. For that, you will need to use the java.math.BigDecimalclass instead. Numbers and Stringscovers BigDecimaland other useful classes provided by the Java platform.

  • double: The doubledata type is a double-precision 64-bit IEEE 754 floating point. Its range of values is beyond the scope of this discussion, but is specified in section 4.2.3of the Java Language Specification. For decimal values, this data type is generally the default choice. As mentioned above, this data type should never be used for precise values, such as currency.

  • float:float数据类型是单精度32位IEEE 754浮点。它的取值范围超出了本讨论的范围,但 在 Java 语言规范的第4.2.3节中指定。正如所提建议byteshort,使用float(而不是 double如果你需要节省内存浮点数的大型阵列)。此数据类型不应用于精确值,例如货币。为此,您需要改用java.math.BigDecimal类。数字和字符串涵盖BigDecimal了 Java 平台提供的其他有用的类。

  • doubledouble数据类型是双精度 64 位 IEEE 754 浮点数。它的取值范围超出了本讨论的范围,但 在 Java 语言规范的第4.2.3节中指定。对于十进制值,此数据类型通常是默认选择。如上所述,这种数据类型不应用于精确值,例如货币。

For the range of values, see the section 4.2.3 Floating-Point Types, Formats, and Valuesof the JLS.

有关值的范围,请参阅JLS 的4.2.3 浮点类型、格式和值部分。

回答by Daniel Pryden

Binary floating-point numbers have interesting precision characteristics, since the value is stored as a binary integer raised to a binary power. When dealing with sub-integer values (that is, values between 0 and 1), negative powers of two "round off" very differently than negative powers of ten.

二进制浮点数具有有趣的精度特性,因为该值存储为二进制整数的二进制幂。在处理子整数值(即 0 和 1 之间的值)时,二的负幂“四舍五入”与十的负幂非常不同。

For example, the number 0.1 can be represented by 1 x 10-1, but there is no combination of base-2 exponent and mantissa that can precisely represent 0.1 -- the closest you get is 0.10000000000000001.

例如,数字 0.1 可以用 1 x 10 -1 表示,但是没有可以精确表示 0.1 的基数为 2 的指数和尾数的组合——您得到的最接近的是 0.100000000000000001。

So if you have an application where you are working with values like 0.1 or 0.01 a great deal, but where small (less than 0.000000000000001%) errors cannot be tolerated, then binary floating-point numbers are not for you.

因此,如果您的应用程序经常使用 0.1 或 0.01 等值,但不能容忍小(小于 0.000000000000001%)错误,那么二进制浮点数不适合您。

Conversely, if powers of ten are not "special" to your application (powers of ten are important in currency calculations, but not in, say, most applications of physics), then you are actually better off using binary floating-point, since it's usually at least an order of magnitude faster, and it is much more memory efficient.

相反,如果十的幂对您的应用程序不是“特殊的”(十的幂在货币计算中很重要,但在大多数物理应用中并不重要),那么您实际上最好使用二进制浮点数,因为它是通常至少快一个数量级,而且内存效率更高。

The article from the Python documentation on floating point issues and limitationsdoes an excellent job of explaining this issue in an easy to understand form. Wikipedia also has a good article on floating pointthat explains the math behind the representation.

Python 文档中关于浮点问题和限制的文章出色地以易于理解的形式解释了这个问题。维基百科也有一篇关于浮点数的好文章,解释了表示背后的数学原理。

回答by jechaviz

Java's Primitive Data Types

Java 的原始数据类型

boolean: 1-bit. May take on the values true and false only.

布尔值:1 位。只能采用 true 和 false 值。

byte: 1 signed byte (two's complement). Covers values from -128 to 127.

字节:1 个有符号字节(二进制补码)。涵盖从 -128 到 127 的值。

short: 2 bytes, signed (two's complement), -32,768 to 32,767

短:2 个字节,有符号(二进制补码),-32,768 到 32,767

int: 4 bytes, signed (two's complement). -2,147,483,648 to 2,147,483,647.

int:4 个字节,有符号(二进制补码)。-2,147,483,648 至 2,147,483,647。

long: 8 bytes signed (two's complement). Ranges from -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807.

long:8 字节有符号(二进制补码)。范围从 -9,223,372,036,854,775,808 到 +9,223,372,036,854,775,807。

float: 4 bytes, IEEE 754. Covers a range from 1.40129846432481707e-45 to 3.40282346638528860e+38 (positive or negative).

浮点数:4 字节,IEEE 754。覆盖范围从 1.40129846432481707e-45 到 3.40282346638528860e+38(正或负)。

double: 8 bytes IEEE 754. Covers a range from 4.94065645841246544e-324d to 1.79769313486231570e+308d (positive or negative).

double:8 字节 IEEE 754。覆盖范围从 4.94065645841246544e-324d 到 1.79769313486231570e+308d(正或负)。

char: 2 bytes, unsigned, Unicode, 0 to 65,535

char:2 个字节,无符号,Unicode,0 到 65,535