Java 中的简单划分 - 这是错误还是功能?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2909451/
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
Simple division in Java - is this a bug or a feature?
提问by msr
I'm trying this simple calculation in a Java application:
我正在 Java 应用程序中尝试这个简单的计算:
System.out.println("b=" + (1 - 7 / 10));
Obviously I expect the output to be b=0.3, but I actually get b=1instead.
显然我希望输出为b=0.3,但实际上我得到了b=1。
What?! Why does this happen?
什么?!为什么会发生这种情况?
If I write:
如果我写:
System.out.println("b=" + (1 - 0.7));
I get the right result, which is b=0.3.
我得到了正确的结果,即b=0.3.
What's going wrong here?
这里出了什么问题?
采纳答案by Xavier Ho
You're using integer division.
您正在使用整数除法。
Try 7.0/10instead.
试试吧7.0/10。
回答by JustJeff
You've used integers in the expression 7/10, and integer 7 divided by integer 10 is zero.
您在表达式 7/10 中使用了整数,整数 7 除以整数 10 为零。
What you're expecting is floating point division. Any of the following would evaluate the way you expected:
你期待的是浮点除法。以下任何一项都将评估您期望的方式:
7.0 / 10
7 / 10.0
7.0 / 10.0
7 / (double) 10
回答by Fred Haslam
I find letter identifiers to be more readable and more indicative of parsed type:
我发现字母标识符更具可读性,并且更能说明解析类型:
1 - 7f / 10
1 - 7 / 10f
or:
或者:
1 - 7d / 10
1 - 7 / 10d
回答by Blessed Geek
Please do not take this as an answer to the question. It is not, but an advice related to exploiting the difference of int and float. I would have put this under a comment except that the answer box allows me to format this comment.
请不要将此作为问题的答案。它不是,而是与利用 int 和 float 的差异相关的建议。除了答案框允许我设置此评论的格式外,我会将其放在评论下。
This feature has been used in every respectable programming language since the days of fortran (or earlier) - I must confess I was once a Fortran and Cobol punch card programmer.
自 fortran(或更早)时代以来,此功能已在每一种受人尊敬的编程语言中使用 - 我必须承认我曾经是 Fortran 和 Cobol 打孔卡程序员。
As an example, integer division of 10/3 yields integer value 3 since an integer has no facility to hold fractional residual .3333.. .
例如,10/3 的整数除法产生整数值 3,因为整数无法保存分数残差 .3333.. 。
One of the ways we (old time ancient programmers) had been using this feature is loop control.
我们(古老的古代程序员)一直使用此功能的方法之一是循环控制。
Let's say we wish to print an array of 1000 strings, but we wish to insert a line break after every 15th string, to insert some prettyfying chars at the end of the line and at the beginning of the next line. We exploit this, given that integer k is the position of a string in that array.
假设我们希望打印一个包含 1000 个字符串的数组,但我们希望在每 15 个字符串之后插入一个换行符,以便在行尾和下一行的开头插入一些漂亮的字符。我们利用这一点,因为整数 k 是该数组中字符串的位置。
int(k/15)*15 == k
is true only when k is divisible by 15, an occurrence at a frequency of every 15th cell. Which is akin to what my friend said about his grandfather's dead watch being accurate twice a day.
仅当 k 可被 15 整除时才为真,每 15 个单元格出现一次。这类似于我的朋友所说的关于他祖父的死表每天准确两次的说法。
int(1/15) = 0 -> int(1/15)*15 = 0
int(2/15) = 0 -> int(2/15)*15 = 0
...
int(14/15) = 0 -> int(14/15)*15 = 0
int(15/15) = 1 -> int(15/15)*15 = 15
int(16/15) = 1 -> int(16/15)*15 = 15
int(17/15) = 1 -> int(17/15)*15 = 15
...
int(29/15) = 1 -> int(29/15)*15 = 15
int(30/15) = 2 -> int(30/15)*15 = 30
Therefore, the loop,
因此,循环,
leftPrettyfy();
for(int k=0; k<sa.length; k++){
print(sa[k]);
int z = k + 1;
if ((z/15)*15 == z){
rightPrettyfy();
leftPrettyfy();
}
}
By varying k in a fanciful way in the loop, we could print a triangular printout
通过在循环中以奇特的方式改变 k,我们可以打印出一个三角形的打印输出
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
That is to demonstrate that, if you consider this a bug, this "bug" is a useful feature that we would not want to be removed from any of the various languages that we have used thus far.
那是为了证明,如果您认为这是一个错误,那么这个“错误”是一个有用的功能,我们不希望从我们迄今为止使用的任何各种语言中删除它。
回答by Felipe Volpato
In my case I was doing this:
就我而言,我是这样做的:
double a = (double) (MAX_BANDWIDTH_SHARED_MB/(qCount+1));
Instead of the "correct" :
而不是“正确”:
double a = (double)MAX_BANDWIDTH_SHARED_MB/(qCount+1);
Take attention with the parentheses !
注意括号!

