Java float 比 double 更精确?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19444081/
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
Java float is more precise than double?
提问by Mustafa
Code:
代码:
class Main {
public static void main (String[] args) {
System.out.print("float: ");
System.out.println(1.35f-0.00026f);
System.out.print("double: ");
System.out.println(1.35-0.00026);
}
}
Output:
输出:
float: 1.34974
double: 1.3497400000000002
??? float got the right answer, but double is adding extra stuff from no where, Why??
???float 得到了正确的答案,但是 double 正在从无处添加额外的东西,为什么?
Isn't double supposed to be more precise than float?
double 不是应该比 float 更精确吗?
采纳答案by Rahul Tripathi
A float is 4 bytes wide, whereas a double is 8 bytes wide.
浮点数为 4 字节宽,而双精度数为 8 字节宽。
Check What Every Computer Scientist Should Know About Floating-Point Arithmetic
Surely the double has more precision so it has slightly less rounding error.
当然,double 具有更高的精度,因此它的舍入误差略小。
Squeezing infinitely many real numbers into a finite number of bits requires an approximate representation. Although there are infinitely many integers, in most programs the result of integer computations can be stored in 32 bits. In contrast, given any fixed number of bits, most calculations with real numbers will produce quantities that cannot be exactly represented using that many bits. Therefore the result of a floating-point calculation must often be rounded in order to fit back into its finite representation.This rounding error is the characteristic feature of floating-point computation.
将无限多的实数压缩为有限数量的位需要近似表示。尽管有无限多个整数,但在大多数程序中,整数计算的结果可以存储在 32 位中。相比之下,给定任何固定数量的位,大多数实数计算将产生无法使用这么多位精确表示的数量。因此,浮点计算的结果通常必须四舍五入以适应其有限表示。这种舍入误差是浮点计算的特征。
On a side note:-
附注:-
I would suggest if you want the exact decimal values then use java.math.BigDecimal
我建议如果你想要精确的十进制值然后使用 java.math.BigDecimal
回答by qiGuar
If you know something about the rules for converting doublevalues to strings, which are specified by the documentation for Double.toString[Java-API], you know that the program prints the shortest decimal fraction sufficient to distinguish the double value from its nearest neighbor, with at least one digit before and after the decimal point.
如果您了解Double.toString[Java-API]文档中指定的将double值 转换为 strings的规则,您就会知道该程序会打印最短的十进制小数,足以将 double 值与其最近的邻居区分开来, 小数点前后至少一位。
Check Joshua Bloch's Java Puzzlers: Traps, Pitfalls, and Corner Cases- Puzzle 2: Time for a Change. He explains this question in that chapter.
查看 Joshua Bloch 的Java Puzzlers: Traps, Pitfalls, and Corner Cases- Puzzle 2: Time for Change。他在那一章解释了这个问题。
回答by Mikhail Kopylov
The reason of this is the mechanism of numbers with floating point calculation. Also when java tries to print a float value, it truncates trailing zeroes. And, you know, doubletype uses 8 bytes and floatuses 4. So the doubleprecision is bigger.
这样做的原因是浮点计算的数字机制。同样,当 java 尝试打印浮点值时,它会截断尾随零。而且,你知道,double类型使用 8 个字节,而float使用 4个字节。所以双精度更大。
So, when java calculates your float value, it gets something like 1.3497400 and truncates it before output. And when java calculates your double value, it gets more digits and so you get such result.
因此,当 java 计算您的浮点值时,它会得到类似 1.3497400 的值并在输出之前将其截断。当 java 计算你的 double 值时,它会得到更多的数字,所以你会得到这样的结果。
Try to execute this simple example for better understanding:
尝试执行这个简单的例子以更好地理解:
public class Test {
public static void main( String[] args ) {
float sum = 0;
for (int i = 1; i <= 10; i++) {
sum += 0.1;
}
System.out.println( sum );
}
}
public class Test {
public static void main( String[] args ) {
double sum = 0;
for (int i = 1; i <= 10; i++) {
sum += 0.1;
}
System.out.println( sum );
}
}