float 类型Type float

浮点数使用 IEEE(电气和电子工程师协会)格式。Floating-point numbers use the IEEE (Institute of Electrical and Electronics Engineers) format. 浮点类型的单精度值具有 4 个字节,包括一个符号位、一个 8 位 excess-127 二进制指数和一个 23 位尾数。Single-precision values with float type have 4 bytes, consisting of a sign bit, an 8-bit excess-127 binary exponent, and a 23-bit mantissa. 尾数表示一个介于 1.0 和 2.0 之间的数。The mantissa represents a number between 1.0 and 2.0. 由于尾数的高顺序位始终为 1,因此它不是以数字形式存储的。Since the high-order bit of the mantissa is always 1, it is not stored in the number. 此表示形式为 float 类型提供了一个大约在 3.4E-38 和 3.4E+38 之间的范围。This representation gives a range of approximately 3.4E-38 to 3.4E+38 for type float.

您可根据应用程序的需求将变量声明为 float 或 double。You can declare variables as float or double, depending on the needs of your application. 这两种类型之间的主要差异在于它们可表示的基数、它们需要的存储以及它们的范围。The principal differences between the two types are the significance they can represent, the storage they require, and their range. 下表显示了基数与存储需求之间的关系。The following table shows the relationship between significance and storage requirements.

浮点类型Floating-Point Types

类型Type 有效位Significant digits 字节数Number of bytes
floatfloat 6 - 76 - 7 44
doubledouble 15 - 1615 - 16 88

浮点变量由尾数(包含数字的值)和指数(包含数字的数量级)表示。Floating-point variables are represented by a mantissa, which contains the value of the number, and an exponent, which contains the order of magnitude of the number.

下表显示了分配给每个浮点类型的尾数和指数的位数。The following table shows the number of bits allocated to the mantissa and the exponent for each floating-point type. 任何 float 或 double 的最高有效位始终是符号位。The most significant bit of any float or double is always the sign bit. 如果符号位为 1,则将数字视为负数;否则,将数字视为正数。If it is 1, the number is considered negative; otherwise, it is considered a positive number.

指数和尾数的长度Lengths of Exponents and Mantissas

类型Type 指数长度Exponent length 尾数长度Mantissa length
floatfloat 8 位8 bits 23 位23 bits
doubledouble 11 位11 bits 52 位52 bits

由于指数是以无符号形式存储的,因此指数的偏差为其可能值的一半。Because exponents are stored in an unsigned form, the exponent is biased by half its possible value. 对于 float 类型,偏差为 127;对于 double 类型,偏差为 1023。For type float, the bias is 127; for type double, it is 1023. 您可以通过将指数值减去偏差值来计算实际指数值。You can compute the actual exponent value by subtracting the bias value from the exponent value.

存储为二进制分数的尾数大于或等于 1 且小于 2。The mantissa is stored as a binary fraction greater than or equal to 1 and less than 2. 对于 float 和 double 类型,最高有效位位置的尾数中有一个隐含的前导 1,这样,尾数实际上分别为 24 和 53 位长,即使最高有效位从未存储在内存中也是如此。For types float and double, there is an implied leading 1 in the mantissa in the most-significant bit position, so the mantissas are actually 24 and 53 bits long, respectively, even though the most-significant bit is never stored in memory.

浮点包可以将二进制浮点数存储为非标准化数,而不使用刚刚介绍的存储方法。Instead of the storage method just described, the floating-point package can store binary floating-point numbers as denormalized numbers. “非标准化数”是带有保留指数值的非零浮点数,其中尾数的最高有效位为 0。"Denormalized numbers" are nonzero floating-point numbers with reserved exponent values in which the most-significant bit of the mantissa is 0. 通过使用非标准化格式,浮点数的范围可以扩展,但会失去精度。By using the denormalized format, the range of a floating-point number can be extended at the cost of precision. 您无法控制浮点数以标准化形式还是非标准化形式表示;浮点包决定了表示形式。You cannot control whether a floating-point number is represented in normalized or denormalized form; the floating-point package determines the representation. 浮点包从不使用非标准化形式,除非指数变为小于可以标准化形式表示的最小值。The floating-point package never uses a denormalized form unless the exponent becomes less than the minimum that can be represented in a normalized form.

下表显示了可在每种浮点类型的变量中存储的最小值和最大值。The following table shows the minimum and maximum values you can store in variables of each floating-point type. 此表中所列的值仅适用于标准化浮点数;非标准化浮点数的最小值更小。The values listed in this table apply only to normalized floating-point numbers; denormalized floating-point numbers have a smaller minimum value. 请注意,在 80x87 寄存器中保留的数字始终以 80 位标准化形式表示;数字存储在 32 位或 64 位浮点变量(float 类型和 long 类型的变量)中时只能以非标准化形式表示。Note that numbers retained in 80x87 registers are always represented in 80-bit normalized form; numbers can only be represented in denormalized form when stored in 32-bit or 64-bit floating-point variables (variables of type float and type long).

浮点类型的范围Range of Floating-Point Types

类型Type 最小值Minimum value 最大值Maximum value
浮动float 1.175494351 E - 381.175494351 E - 38 3.402823466 E + 383.402823466 E + 38
doubledouble 2.2250738585072014 E - 3082.2250738585072014 E - 308 1.7976931348623158 E + 3081.7976931348623158 E + 308

如果存储比精度更重要,请考虑对浮点变量使用 float 类型。If precision is less of a concern than storage, consider using type float for floating-point variables. 相反,如果精度是最重要的条件,则使用 double 类型。Conversely, if precision is the most important criterion, use type double.

浮点变量可以提升为更大基数的类型(从 float 类型到 double 类型)。Floating-point variables can be promoted to a type of greater significance (from type float to type double). 当您对浮点变量执行算术时,通常会出现提升。Promotion often occurs when you perform arithmetic on floating-point variables. 此算术始终以与具有最高精度的变量一样高的精度执行。This arithmetic is always done in as high a degree of precision as the variable with the highest degree of precision. 例如,请考虑下列类型声明:For example, consider the following type declarations:

float f_short;  
double f_long;  
long double f_longer;  

f_short = f_short * f_long;  

在前面的示例中,变量 f_short 提升到类型 double 并且与 f_long 相乘;然后,结果舍入到类型 float,然后赋给 f_shortIn the preceding example, the variable f_short is promoted to type double and multiplied by f_long; then the result is rounded to type float before being assigned to f_short.

在以下示例中(使用前面示例中的声明),将以浮点(32 位)精度对变量执行算术;结果随后将提升到 double 类型:In the following example (which uses the declarations from the preceding example), the arithmetic is done in float (32-bit) precision on the variables; the result is then promoted to type double:

f_longer = f_short * f_short;  

请参阅See Also

基本类型的存储Storage of Basic Types