- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当然,Java 没有“无符号”长值,但有时有符号长值被有效地视为无符号长值(例如 System.nanoTime()
的结果)。从这个意义上说,算术溢出与其说是值的溢出,不如说是 64 位表示的溢出。示例:
Long.MAX_VALUE * 2L // overflows the signed product but not the unsigned product
Long.MAX_VALUE * 4L // overflows the signed product and the unsigned product
-1L * 2L // overflows the unsigned product but not the signed product
测试乘法是否溢出似乎有些复杂,因为操作的符号会妨碍。注意到任何负值乘以 0 或 1 以外的任何值都会溢出无符号乘积可能会有所帮助,因为负值的最高位已设置。
确定两个“无符号”长值(实际上是有符号长值)的乘积是否会溢出 64 位表示的最佳方法是什么?使用 BigInteger
的实例是一个显而易见的解决方案,我推导出了一个仅涉及原始操作的复杂测试,但我觉得我遗漏了一些明显的东西。
最佳答案
给定两个我们假装成无符号长值的有符号长值,这里是如何确定无符号乘积是否会溢出,只使用有符号的原始操作(请原谅迂腐):
boolean unsignedMultiplyOverflows(final long a, final long b) {
if ((a == 0L) || (b == 0L)) {
// Unsigned overflow of a * b will not occur, since the result would be 0.
return false;
}
if ((a == 1L) || (b == 1L)) {
// Unsigned overflow of a * b will not occur, since the result would be a or b.
return false;
}
if ((a < 0L) || (b < 0L)) {
// Unsigned overflow of a * b will occur, since the highest bit of one argument is set, and a bit higher than the lowest bit of the other argument is set.
return true;
}
/*
* 1 < a <= Long.MAX_VALUE
* 1 < b <= Long.MAX_VALUE
*
* Let n == Long.SIZE (> 2), the number of bits of the primitive representation.
* Unsigned overflow of a * b will occur if and only if a * b >= 2^n.
* Each side of the comparison must be re-written such that signed overflow will not occur:
*
* [a.01] a * b >= 2^n
* [a.02] a * b > 2^n - 1
* [a.03] a * b > ((2^(n-1) - 1) * 2) + 1
*
* Let M == Long.MAX_VALUE == 2^(n-1) - 1, and substitute:
*
* [a.04] a * b > (M * 2) + 1
*
* Assume the following identity for non-negative integer X and positive integer Y:
*
* [b.01] X == ((X / Y) * Y) + (X % Y)
*
* Let X == M and Y == b, and substitute:
*
* [b.02] M == ((M / b) * b) + (M % b)
*
* Substitute for M:
*
* [a.04] a * b > (M * 2) + 1
* [a.05] a * b > ((((M / b) * b) + (M % b)) * 2) + 1
* [a.06] a * b > ((M / b) * b * 2) + ((M % b) * 2) + 1
*
* Assume the following identity for non-negative integer X and positive integer Y:
*
* [c.01] X == ((X / Y) * Y) + (X % Y)
*
* Let X == ((M % b) * 2) + 1 and Y == b, and substitute:
*
* [c.02] ((M % b) * 2) + 1 == (((((M % b) * 2) + 1) / b) * b) + ((((M % b) * 2) + 1) % b)
*
* Substitute for ((M % b) * 2) + 1:
*
* [a.06] a * b > ((M / b) * b * 2) + ((M % b) * 2) + 1
* [a.07] a * b > ((M / b) * b * 2) + (((((M % b) * 2) + 1) / b) * b) + ((((M % b) * 2) + 1) % b)
*
* Divide each side by b (// represents real division):
*
* [a.08] (a * b) // b > (((M / b) * b * 2) + (((((M % b) * 2) + 1) / b) * b) + ((((M % b) * 2) + 1) % b)) // b
* [a.09] (a * b) // b > (((M / b) * b * 2) // b) + ((((((M % b) * 2) + 1) / b) * b) // b) + (((((M % b) * 2) + 1) % b) // b)
*
* Reduce each b-divided term that otherwise has a known factor of b:
*
* [a.10] a > ((M / b) * 2) + ((((M % b) * 2) + 1) / b) + (((((M % b) * 2) + 1) % b) // b)
*
* Let c == ((M % b) * 2) + 1), and substitute:
*
* [a.11] a > ((M / b) * 2) + (c / b) + ((c % b) // b)
*
* Assume the following tautology for integers X, Y and real Z such that 0 <= Z < 1:
*
* [d.01] X > Y + Z <==> X > Y
*
* Assume the following tautology for non-negative integer X and positive integer Y:
*
* [e.01] 0 <= (X % Y) // Y < 1
*
* Let X == c and Y == b, and substitute:
*
* [e.02] 0 <= (c % b) // b < 1
*
* Let X == a, Y == ((M / b) * 2) + (c / b), and Z == ((c % b) // b), and substitute:
*
* [d.01] X > Y + Z <==> X > Y
* [d.02] a > ((M / b) * 2) + (c / b) + ((c % b) // b) <==> a > ((M / b) * 2) + (c / b)
*
* Drop the last term of the right-hand side:
*
* [a.11] a > ((M / b) * 2) + (c / b) + ((c % b) // b)
* [a.12] a > ((M / b) * 2) + (c / b)
*
* Substitute for c:
*
* [a.13] a > ((M / b) * 2) + ((((M % b) * 2) + 1) / b)
*
* The first term of the right-hand side is clearly non-negative.
* Determine the upper bound for the first term of the right-hand side (note that the least possible value of b == 2 produces the greatest possible value of (M / b) * 2):
*
* [f.01] (M / b) * 2 <= (M / 2) * 2
*
* Assume the following tautology for odd integer X:
*
* [g.01] (X / 2) * 2 == X - 1
*
* Let X == M and substitute:
*
* [g.02] (M / 2) * 2 == M - 1
*
* Substitute for (M / 2) * 2:
*
* [f.01] (M / b) * 2 <= (M / 2) * 2
* [f.02] (M / b) * 2 <= M - 1
*
* The second term of the right-hand side is clearly non-negative.
* Determine the upper bound for the second term of the right-hand side (note that the <= relation is preserved across positive integer division):
*
* [h.01] M % b < b
* [h.02] M % b <= b - 1
* [h.03] (M % b) * 2 <= (b - 1) * 2
* [h.04] ((M % b) * 2) + 1 <= (b * 2) - 1
* [h.05] (((M % b) * 2) + 1) / b <= ((b * 2) - 1) / b
* [h.06] (((M % b) * 2) + 1) / b <= 1
*
* Since the upper bound of the first term is M - 1, and the upper bound of the second term is 1, the upper bound of the right-hand side is M.
* Each side of the comparison has been re-written such that signed overflow will not occur.
*/
final boolean unsignedMultiplyOverflows = (a > ((Long.MAX_VALUE / b) * 2L) + ((((Long.MAX_VALUE % b) * 2L) + 1L) / b));
return unsignedMultiplyOverflows;
}
关于java - 如何检测Java中 "unsigned"长乘法的溢出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20059529/
如果矩阵A在X中,矩阵B在Y中。 进行乘法运算只是 Z = X*Y。正确假设两个数组的大小相同。 如何使用 for 循环计算它? 最佳答案 ja72 的anwser 是错误的,请查看我在其下的评论以了
我有一个 C 程序,它有 n 次乘法(单次乘法和 n 次迭代),我发现另一个逻辑有 n/2 次迭代(1 次乘法 + 2 次加法)。我知道两者都是 O(n) 的复杂性。但就 CPU 周期而言。哪个更快?
我有一个矩阵x: x <- matrix(1:8, nrow = 2, ncol = 4, byrow = 2) # [,1] [,2] [,3] [,4] #[1,] 1 2 3
我有一个矩阵x: x <- matrix(1:8, nrow = 2, ncol = 4, byrow = 2) # [,1] [,2] [,3] [,4] #[1,] 1 2 3
我正在创建一个基于电影 InTime 的 Minecraft 插件,并尝试创建代码,在玩家死亡时玩家将失去 25% 的时间。 当前代码是: String minus = itapi.getTimeSt
我正在尝试将 2 个矩阵与重载的 * 运算符相乘并打印结果。虽然看起来我不能为重载函数提供超过 1 个参数。如何将这两个矩阵传递给重载函数?请在下面查看我的实现。 #include #include
为什么在 Java 中使用 .*?例如 double probability = 1.*count/numdata; 给出相同的输出: double probability = count/numda
如果我尝试将两个值与单位相乘,则会出现意外错误。 $test: 10px; .testing{ width: $test * $test; } result: 100px*px isn't a v
我正在尝试计算库存中所有产品的总值(value)。表中的每种产品都有价格和数量。因此,我需要将每种产品的价格乘以数量,然后将所有这些加在一起以获得所有产品的总计。根据上一个问题,我现在可以使用 MyS
我正在尝试计算库存中所有产品的总值(value)。表中的每种产品都有价格和数量。因此,我需要将每种产品的价格乘以数量,然后将所有这些加在一起以获得所有产品的总计。根据上一个问题,我现在可以使用 MyS
大家好,我有以下代码行 solution first = mylist.remove((int)(Math.random() * mylist)); 这给了我一个错误说明 The operator *
我必须做很多乘法运算。如果我考虑效率,那么我应该使用位运算而不是常规的 * 运算吗?如果有差异如何进行位运算?提前致谢.. 最佳答案 不,您应该使用乘法运算符,让优化编译器决定如何最快地完成它。 您会
两个 n 位数字 A 和 B 的乘法可以理解为移位的总和: (A << i1) + (A << i2) + ... 其中 i1, i2, ... 是 B 中设置为 1 的位数。 现在让我们用 OR
我想使用 cuda 6 进行 bool 乘法,但我无法以正确的方式做到这一点。B 是一个 bool 对称矩阵,我必须进行 B^n bool 乘法。 我的 C++ 代码是: for (m=0; m
我正在编写一个定点类,但遇到了一些问题...乘法、除法部分,我不确定如何模拟。我对部门运算符(operator)进行了非常粗暴的尝试,但我确信这是错误的。到目前为止,它是这样的: class Fixe
我有TABLE_A我需要创建 TABLE_A_FINAL 规则: 在TABLE_A_FINAL中我们有包含 ID_C 的所有可能组合的行如果在 TABLE_A与 ID_C 的组合相同我们乘以 WEIG
这个问题在这里已经有了答案: Simple way to repeat a string (32 个答案) 关闭 6 年前。 我有一个任务是重复字符乘以它例如用户应该写重复输入 3 R 输出的字母和
我最近学习了C++的基础知识。我发现了一些我不明白的东西。这是让我有点困惑的程序。 #include using namespace std; int main()
我有两个列表: list_a = list_b = list(范围(2, 6)) final_list = [] 我想知道如何将两个列表中的所有值相乘。我希望我的 final_list 包含 [2*2
如何修改此代码以适用于任何基数? (二进制、十六进制、基数 10 等) int mult(int a, int b, int base){ if((a<=base)||(b<=base)){
我是一名优秀的程序员,十分优秀!