[英]Order of operation int(4*x/y) vs int(x/(y/4))
Suppose x and y are of type int. 假设x和y的类型为int。
Are the two expressions: 是两个表达式:
int(4*x/y)
and: 和:
int(x/(y/4))
always evaluate to the same for all x
and y
of type int
? 始终对
int
类型的所有x
和y
求值相同? They should mathematically, but only the second expression is consistent (ie, producing the expected value) in a program I've written. 它们在数学上应该是正确的,但是在我编写的程序中,只有第二个表达式是一致的(即产生期望值)。
In many programming languages, 4*x/y
and x/(y/4)
are different because y/4
, an integer, is the truncated result of the division of y
by 4
. 在许多编程语言中,
4*x/y
和x/(y/4)
是不同的,因为整数y/4
是y
除以4
的截断结果。 No such truncation exists in 4*x/y
. 在
4*x/y
不存在这样的截断。 On obvious difference in when y
is 1
, in which case the second expression divides by zero, whereas the first one computes 4*x
. 当
y
为1
时存在明显差异,在这种情况下,第二个表达式除以零,而第一个表达式计算4*x
。
i assume x and y are ints 我假设x和y是整数
then of course not :) on integers: x/4=floor(x/4) mathematically 那么当然不是:)在整数上:x / 4 = floor(x / 4)数学上
which gives you: 这给你:
floor(4*x/y) and floor(x/floor(y/4)) 地板(4 * x / y)和地板(x /地板(y / 4))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.