Java浮点数的显示精度 [英] Displayed precision of Java floating-point

查看:275
本文介绍了Java浮点数的显示精度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我们运行以下代码:

If we run the following code:

float f = 1.2345678990922222f;
double d = 1.22222222222222222222d;
System.out.println("f = " + f + "\t" + "d = " + d);

它打印:

f = 1.2345679   d = 1.2222222222222223

文字1.2345678990922222中的长尾将被忽略,但1.22222222222222222222222中的长尾不会被忽略(变量d中的最后一个十进制数字变为3而不是2).为什么?

The long tail in the literal 1.2345678990922222 is ignored but the long tail in 1.22222222222222222222 is not (the last decimal digit in the variable d becomes 3 instead of 2). Why?

推荐答案

打印floatdouble时看到的位数是Java的float和<默认转换规则的结果. c1>转换为小数.

The number of digits you see when a float or a double is printed is a consequence of Java’s rules for default conversion of float and double to decimal.

Java的浮点数默认格式使用最少的有效十进制数字来区分数字和附近的可表示数字. 1

Java’s default formatting for floating-point numbers uses the fewest significant decimal digits needed to distinguish the number from nearby representable numbers.1

在您的示例中,源文本中的1.2345678990922222f转换为float值1.2345678806304931640640625,因为在float类型可以表示的所有值中,一个值最接近1.2345678990922222.下一个较低的值和下一个较高的值是1.23456776142120361328125和1.23456799983978271484375.

In your example, 1.2345678990922222f in source text is converted to the float value 1.2345678806304931640625, because, of all the values representable in the float type, that one is closest to 1.2345678990922222. The next lower and next higher values are 1.23456776142120361328125 and 1.23456799983978271484375.

在打印此值时,Java只需要打印"1.2345679",因为这足以使我们可以从其邻居1.23456776142120361328125和1.23456799983983978271484375中选择float值1.2345678806304931640625.

When printing this value, Java only needs to print "1.2345679", because that is enough that we can pick out the float value 1.2345678806304931640625 from its neighbors 1.23456776142120361328125 and 1.23456799983978271484375.

对于您的double示例,1.22222222222222222222d转换为1.22222222222222232090871330001391470432281494140625. double中可表示的下一个较低和下一个较高的值是1.2222222222222220988641083749826066195964813232421875和1.2222222222222225429533182250452227890491485595703125.如您所见,为了将1.22222222222222232090871330001391470432281494140625与邻居区分开来,Java需要打印"1.2222222222222223".

For your double example, 1.22222222222222222222d is converted to 1.22222222222222232090871330001391470432281494140625. The next lower and next higher values representable in double are 1.2222222222222220988641083749826066195964813232421875 and 1.2222222222222225429533182250452227890491485595703125. As you can see, to distinguish 1.22222222222222232090871330001391470432281494140625 from its neighbors, Java needs to print "1.2222222222222223".

1

1 The rule for Java SE 10 can be found in the documentation for java.lang.float, in the toString(float d) section. The double documentation is similar. The passage, with the most relevant part in bold, is:

返回float argument的字符串表示形式.下面提到的所有字符都是ASCII字符.

Returns a string representation of the float argument. All characters mentioned below are ASCII characters.

  • 如果参数为NaN,则结果为字符串"NaN".

  • If the argument is NaN, the result is the string "NaN".

否则,结果是一个字符串,代表参数的符号和大小(绝对值).如果符号为负,则结果的第一个字符为'-'('\u002D');如果符号为正,则结果中不显示符号字符.至于 m 的大小:

Otherwise, the result is a string that represents the sign and magnitude (absolute value) of the argument. If the sign is negative, the first character of the result is '-' ('\u002D'); if the sign is positive, no sign character appears in the result. As for the magnitude m:

  • 如果 m 为无穷大,则用字符"Infinity"表示;因此,正无穷大产生结果"Infinity",而负无穷大产生结果"-Infinity".

  • If m is infinity, it is represented by the characters "Infinity"; thus, positive infinity produces the result "Infinity" and negative infinity produces the result "-Infinity".

如果 m 为零,则用字符"0.0"表示;因此,负零会产生结果"-0.0",而正零会产生结果"0.0".

If m is zero, it is represented by the characters "0.0"; thus, negative zero produces the result "-0.0" and positive zero produces the result "0.0".

如果 m 大于或等于10 -3 但小于10 7 ,则表示为 m 的整数部分,十进制形式,无前导零,后跟"."('\u002E'),后跟一个或多个十进制数字,表示 m <的小数部分/em>.

If m is greater than or equal to 10-3 but less than 107, then it is represented as the integer part of m, in decimal form with no leading zeroes, followed by '.' ('\u002E'), followed by one or more decimal digits representing the fractional part of m.

如果 m 小于10 -3 或大于或等于10 7 ,则表示为所谓的计算机科学计数法".设 n 为唯一整数,以使10 n m < 10 n +1 ;然后让 a m 和10 n 的数学精确商,这样1≤ a < 10.然后,将幅度表示为 a 的整数部分,用单个十进制数字表示,后跟"."('\u002E'),后跟十进制数字表示 a ,后跟字母"E"('\u0045'),后跟由方法Integer.toString(int)生成的以十进制整数表示的 n .

If m is less than 10-3 or greater than or equal to 107, then it is represented in so-called "computerized scientific notation." Let n be the unique integer such that 10nm < 10n+1; then let a be the mathematically exact quotient of m and 10n so that 1 ≤ a < 10. The magnitude is then represented as the integer part of a, as a single decimal digit, followed by '.' ('\u002E'), followed by decimal digits representing the fractional part of a, followed by the letter 'E' ('\u0045'), followed by a representation of n as a decimal integer, as produced by the method Integer.toString(int).

m a 的小数部分必须打印多少个数字? 必须有至少一位数字来表示小数部分,并且除此以外,还必须要有多少个数字才能唯一地将参数值与相邻的float类型的值区分开.即,假设 x 是由此方法为有限的非零参数 f 生成的十进制表示形式所表示的精确数学值.然后 f 必须是最接近 x float值;或者,如果两个float值相等地接近 x ,则 f 必须是其中之一,并且是 f的有效位的最低有效位必须为0.

How many digits must be printed for the fractional part of m or a? There must be at least one digit to represent the fractional part, and beyond that as many, but only as many, more digits as are needed to uniquely distinguish the argument value from adjacent values of type float. That is, suppose that x is the exact mathematical value represented by the decimal representation produced by this method for a finite nonzero argument f. Then f must be the float value nearest to x; or, if two float values are equally close to x, then f must be one of them and the least significant bit of the significand of f must be 0.

这篇关于Java浮点数的显示精度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆