英文:
Why this program with for loop give zero when y>5 and x=2
问题
每当我给出 x = 2 且 y > 5 时,结果为零。对于任何其他值,都正常。
package forLoopquestionsForForLoop;
import java.util.Scanner;
public class CalculatexXPowerY {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入 x");
long x = sc.nextInt();
System.out.println("请输入 y");
long y = sc.nextInt();
int power = 1;
for(int i = 0; i < y; i++) {
x *= x;
}
System.out.println(x);
}
}
英文:
whenever I give x =2 and y>5 the result is zero. for any other value it's ok..
package forLoopquestionsForForLoop;
import java.util.Scanner;
public class CalculatexXPowerY {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("enter X");
long x = sc.nextInt();
System.out.println("enter y");
long y = sc.nextInt();
int power = 1;
for(int i =0;i<y;i++) {
x *= x;
}
System.out.println(x);
}
}
答案1
得分: 0
你正超出了Long的最大值,当你赋值x = 2和y > 5时。
你可以使用BigInteger替代。
import java.math.BigInteger;
BigInteger result = new BigInteger("1");
for(int i = 0; i < y; i++) {
result = result.multiply(BigInteger.valueOf(x));
}
System.out.println(result);
}
英文:
You are exceeding the max value of Long, when you assign values x = 2 and y > 5.
You can use BigInteger instead.
import java.math.BigInteger;
BigInteger result = new BigInteger("1");
for(int i =0;i<y;i++)
{
result = result.multiply(BigInteger.valueOf(x));
}
System.out.println(result);
}
答案2
得分: 0
有一些时间,所以我想我会回来更详细地解释发生了什么。您的 long
变量在系统中表示为一系列位,实际上有 64 位,每个位可以包含值为 0 或 1。它们可以被可视化为:
0000 0000 0000 0010 0000 0000 1100 0000 0000 0000 0001 0000 0000 0000 0000 0100
所有位都为 0 对应的是值为零,最前面的位被保留用作标记,表示数字是正数还是负数。这意味着您可以拥有的最大正值为 2^63-1,约为 9.22 x 10^18。
现在,当您乘以这些位时,它们实际上会在添加自身之前向右移动若干位。这可能会导致意外的行为,称为溢出,将计算出的值推到其变量容器的最大大小之上。当这种情况发生时,系统无法再跟踪超过序列的位,实际上会“忘记”它们。让我们看一个 4 位的例子:
1111 (15)
x 0011 (3)
---------
1111 (15)
+ 11110 (30)
+ 000000 (0)
+ 0000000 (0)
---------
0101101 (45)
然而,值 0101101
超出了我们的 4 位,因此系统会“忘记”超过 4 位的位,并将其变为 1101
。这就是您的清零问题所发生的情况。不过您的情况很有趣,因为对于值 4294967296
,您实际上执行了:
0000 0000 0000 0000 0000 0000 0000 0001 0000 0000 0000 0000 0000 0000 0000 0000
x 0000 0000 0000 0000 0000 0000 0000 0001 0000 0000 0000 0000 0000 0000 0000 0000
-----------------------------------------------------------------------------------
1 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000
然后,因为最前面的 1
大于 long
的最大位数,它被“忘记”,只剩下 0
作为值。之后,每次乘法都是不断进行 0 x 0
。希望这更完整地回答了您的问题。
英文:
Have some time, so I figured I'd come back and give a more proper explanation of what is happening. Your long
variable is represented in the system as a series of bits, 64 of them in fact, which can contain either 0 or 1 as their value. They can be visualized like so:
0000 0000 0000 0010 0000 0000 1100 0000 0000 0000 0001 0000 0000 0000 0000 0100
Having all 0's translates to the value zero and the front most bit is reserved for special usage as a marker for whether the number is positive or negative. This means the maximum positive value you can have is 2^63-1 which ~9.22 e+18.
Now, when you multiply these bits they essentially shift over a certain number of spots before adding themselves. This can lead to unexpected behavior called overflow which pushes the calculated value over the maximum size of it's variable container. When this happens, the system can no longer track the bits that exceed the sequence and essentially "forgets" about them. Lets look at a 4 bit example:
1111 (15)
x 0011 (3)
---------
1111 (15)
+ 11110 (30)
+ 000000 (0)
+ 0000000 (0)
---------
0101101 (45)
The value 0101101
exceeds our 4 bits though, so the system "forgets" about the bits that exceed the 4 and turns into 1101
instead. This is what is happening with your zeroing out problem. Yours is an interesting case though because with the value 4294967296
you're essentially performing:
0000 0000 0000 0000 0000 0000 0000 0001 0000 0000 0000 0000 0000 0000 0000 0000
x 0000 0000 0000 0000 0000 0000 0000 0001 0000 0000 0000 0000 0000 0000 0000 0000
-----------------------------------------------------------------------------------
1 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000
And then because the foremost 1
is greater than the maximum bits for a long
it is "forgot" which leaves only 0
as the value. After that, every multiplication is doing 0 x 0
over and over. Hope this answers your question more completely.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论