Java整数双除法混淆
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32571909/
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 integer-double division confusion
提问by
Program 1
方案一
int sum = 30;
double avg = sum / 4; // result is 7.0, not 7.5 !!!
VS.
对比。
Program 2
方案二
int sum= 30
double avg =sum/4.0 // Prints lns 7.5
Is this because the '4' in program 1 is acting as a literal integer? so 30/4 would give me 7. However since this data type is a double, we need to add a .0 to end. so '7.0'
这是因为程序 1 中的 '4' 充当文字整数吗?所以 30/4 会给我 7。但是由于这个数据类型是双精度的,我们需要添加一个 .0 来结束。所以'7.0'
Program 2 has 4.0 which is acting as a literal double. an int/double would always give double because it more precise. so we get '7.5'. I don't understand what double data type is doing to the result though.. it really doesn't need to do anything since the conditions of the double data type are already satisfied.(have the most precise result out of the computation).
程序 2 有 4.0,它充当文字双精度。int/double 总是会给出 double 因为它更精确。所以我们得到'7.5'。我不明白 double 数据类型对结果做了什么..它真的不需要做任何事情,因为 double 数据类型的条件已经满足。(从计算中获得最精确的结果)。
Am I wrong? I encourage you to correct me! This is how I learn.. :)
我错了吗?我鼓励你纠正我!这就是我学习的方式.. :)
回答by Pedro Affonso
In java, operations involving only integer types(int, long and so on) have integer results. This includes divisions.
在 java 中,仅涉及整数类型(int、long 等)的操作具有整数结果。这包括分裂。
Both 30 and 4 are integer values, so if you try to divide them, integerdivision is used.
30 和 4 都是整数值,因此如果您尝试对它们进行除法,则使用整数除法。
But if you try either 30.0/4
or 30/4.0
then the result will be 7.5
because you will be using floating-pointdivision.
但是,如果您尝试任一30.0/4
或30/4.0
那么结果将是7.5
因为您将使用浮点除法。
The declared type of the variable avg
has no influence on the result. The decimal part is lost during the division, and not when you assign the value to the variable.
变量的声明类型avg
对结果没有影响。小数部分在除法过程中丢失,而不是在将值分配给变量时丢失。
回答by Cacho Santa
This is an integer division (because it involves two integers)
这是一个整数除法(因为它涉及两个整数)
int sum = 30;
double avg = (sum / 4); // result is 7
Integer division, will round down to the nearest integer.
整数除法,将向下舍入到最接近的整数。
However, this is a double division (because 4.0 is a double)
但是,这是双除法(因为 4.0 是双除法)
int sum= 30
double avg = (sum/4.0) // result is 7.5
This is the expected behaviour, and the conversion are all well defined. No need to guess. Take a look at the java docsabout conversion.
这是预期的行为,并且转换都是明确定义的。无需猜测。查看有关转换的java 文档。
A widening conversion of an int or a long value to float, or of a long value to double, may result in loss of precision - that is, the result may lose some of the least significant bits of the value. In this case, the resulting floating-point value will be a correctly rounded version of the integer value, using IEEE 754 round-to-nearest mode
从 int 或 long 值到 float 或从 long 值到 double 的扩展转换可能会导致精度损失——也就是说,结果可能会丢失值的一些最低有效位。在这种情况下,生成的浮点值将是整数值的正确舍入版本,使用 IEEE 754 舍入到最近模式
回答by Ben M.
In your first example:
在你的第一个例子中:
int sum = 30;
double avg = sum / 4; // result is 7.0, not 7.5 !!!
sum
is an int, and 4
is also an int. Java is dividing one integer by another and getting an integer result. This all happens beforeit assigns the value to double avg
, and by then you've already lost all information to the right of the decimal point.
sum
是一个int,4
也是一个int。Java 将一个整数除以另一个整数并得到一个整数结果。这一切都发生在将值分配给之前,到double avg
那时您已经丢失了小数点右侧的所有信息。
Try some casting.
尝试一些铸造。
int sum = 30;
double avg = (double) sum / 4; // result is 7.5
OR
或者
int sum = 30;
double avg = sum / 4.0d; // result is 7.5
回答by K139
Image taken from : http://www.mathcs.emory.edu/~cheung/Courses/170/Syllabus/04/mixed.html
图片取自:http: //www.mathcs.emory.edu/~cheung/Courses/170/Syllabus/04/mixed.html
Refer above URL for more clear explanation.
请参阅上面的 URL 以获得更清晰的解释。
回答by Tomas Ceruti
PROGRAM 1 In Java, when you do a division between two integers, the result is an integer. So when you do sum/4, the result is the integer 7. Then you assign that integer to a double variable, so that 7 turns into 7.0.
程序 1 在 Java 中,当您在两个整数之间进行除法时,结果是一个整数。所以当你做 sum/4 时,结果是整数 7。然后你把这个整数赋给一个 double 变量,这样 7 就变成了 7.0。
PROGRAM 2 In Java, when you do a division between an integer and a double, the result is a double. So when you do sum/4.0, the result is the double 7.5. Then you assign that to a double variable, so it's still 7.5.
程序 2 在 Java 中,当您在整数和双精度数之间进行除法时,结果是双精度数。所以当你做 sum/4.0 时,结果是双倍 7.5。然后你将它分配给一个双变量,所以它仍然是 7.5。
So the problem is not how println prints the variables, but how division works in Java
所以问题不是 println 如何打印变量,而是除法在 Java 中是如何工作的