整数除法性质

发布于 2024-08-29 04:03:11 字数 197 浏览 4 评论 0原文

下面的整数算术性质成立吗?

(m/n)/l == m/(n*l)

起初我以为我知道答案(不成立),但现在不确定。 它适用于所有数字还是仅适用于某些条件,即 n >我?

该问题涉及计算机算术,即q = n/m, q*m != n,忽略溢出。

does the following integer arithmetic property hold?

(m/n)/l == m/(n*l)

At first I thought I knew answer (does not hold), but now am not sure.
Does it hold for all numbers or only for certain conditions, i.e. n > l?

the question pertains to computer arithmetic, namely q = n/m, q*m != n, ignoring overflow.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半岛未凉 2024-09-05 04:03:11
Case1 assume m = kn+b (b<n),
left = (m/n)/l = ((kn+b)/n)/l = (k+b/n)/l = k/l (b/n=0, because b<n)
right = (kn+b)/(n*l) = k/l + b/(n*l) = k/l (b/(n*l)=0, because b<n)
=> left = right

Case2 assume m = kn,
left = (m/n)/l = (kn/n)/l = k/l
right = kn/(n*l) = k/l
=> left = right

So, (m/n)/l == m/(n*l)
Case1 assume m = kn+b (b<n),
left = (m/n)/l = ((kn+b)/n)/l = (k+b/n)/l = k/l (b/n=0, because b<n)
right = (kn+b)/(n*l) = k/l + b/(n*l) = k/l (b/(n*l)=0, because b<n)
=> left = right

Case2 assume m = kn,
left = (m/n)/l = (kn/n)/l = k/l
right = kn/(n*l) = k/l
=> left = right

So, (m/n)/l == m/(n*l)
若沐 2024-09-05 04:03:11

你在谈论数学整数吗?或者编程语言中的固定宽度整数?

这两个方程与数学整数相同,但如果您使用固定宽度整数,则这两个函数具有不同的溢出行为。

例如,假设整数是 32 位

(1310720000/65536)/65537 = 20000/65537 = 0

,但是 65536 * 65537 会溢出 32 位整数,并且等于 65536,所以

1310720000/(65536*65537) = 1310720000/65536 = 20000

Are you talking about mathematical integers? Or fixed-width integers within a programming language?

The two equations are identical with mathematical integers, but the two functions have different overflow behaviors if you are using fixed-width integers.

For example, suppose integers are 32-bit

(1310720000/65536)/65537 = 20000/65537 = 0

However, 65536 * 65537 will overflow a 32-bit integer, and will equal 65536, so

1310720000/(65536*65537) = 1310720000/65536 = 20000
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文