在 java 中打印大的乘数
Printing large multiplied numbers in java
我正在做 AP Comp Sci Review sheet 我不明白为什么我编译时
System.out.println(365 * 24 * 3600 * 1024 * 1024 * 1024);
答案是0。我知道一个int是32位的,最大为2147483647,不能给你3386152216166400,但为什么不给溢出异常错误或类似的错误?
只需添加一个L
来标识其中一个值是原始类型long
:
System.out.println(365L * 24 * 3600 * 1024 * 1024 * 1024);
/* ^
* 'L' to indicate that one of the factors is long
*/
In Java, all math is done in the largest data type required to handle
all of the current values. So, if you have int * int, it will always
do the math as an integer, but int * long is done as a long.
In this case, the 1024*1024*1024*80 is done as an Int, which overflows
int.
The "L" of course forces one of the operands to be an Int-64 (long),
therefore all the math is done storing the values as a Long, thus no
overflow occurs.
Credit goes to Erich:
编辑:
In many cases Java is based on C or C++ and these are based on
Assembly. An overflow/underflow is silent in C and C++ and almost
silent in assembly (unless you check special flags). This is likely
due to the fact that C and C++ didn't have exceptions when they were
first proposed. If you wanted to see overflows/underflows you just
used a larger type. e.g. long long int or long double ;) BTW assembly
has something similar to exceptions called traps or interrupts,
overflows/underflow doesn't cause a trap AFAIK.
Credit goes to Peter Lawrey:
提示:有时使用 google 可以在您提出问题之前回答您的问题。
你必须使用 BigInteger。
BigInteger bi1, bi2, bi3;
// assign values to bi1, bi2
bi1 = new BigInteger("123");
bi2 = new BigInteger("50");
// perform multiply operation on bi1 using bi2
bi3 = bi1.multiply(bi2);
String str = "Result of multiply is " +bi3;;
// print bi3 value
System.out.println( str );
来自Java Language Specification,
The integer operators do not indicate overflow or underflow in any
way.
这就是你没有得到任何异常的原因。
结果specified by the language如下,
If an integer multiplication overflows, then the result is the
low-order bits of the mathematical product as represented in some
sufficiently large two's-complement format. As a result, if overflow
occurs, then the sign of the result may not be the same as the sign of
the mathematical product of the two operand values.
Integer.MAX_VALUE + 1 == Integer.MIN_VALUE
Integer.MIN_VALUE - 1 == Integer.MAX_VALUE
这就是您得到零结果的原因。
我正在做 AP Comp Sci Review sheet 我不明白为什么我编译时
System.out.println(365 * 24 * 3600 * 1024 * 1024 * 1024);
答案是0。我知道一个int是32位的,最大为2147483647,不能给你3386152216166400,但为什么不给溢出异常错误或类似的错误?
只需添加一个L
来标识其中一个值是原始类型long
:
System.out.println(365L * 24 * 3600 * 1024 * 1024 * 1024);
/* ^
* 'L' to indicate that one of the factors is long
*/
In Java, all math is done in the largest data type required to handle all of the current values. So, if you have int * int, it will always do the math as an integer, but int * long is done as a long.
In this case, the 1024*1024*1024*80 is done as an Int, which overflows int.
The "L" of course forces one of the operands to be an Int-64 (long), therefore all the math is done storing the values as a Long, thus no overflow occurs.
Credit goes to Erich:
编辑:
In many cases Java is based on C or C++ and these are based on Assembly. An overflow/underflow is silent in C and C++ and almost silent in assembly (unless you check special flags). This is likely due to the fact that C and C++ didn't have exceptions when they were first proposed. If you wanted to see overflows/underflows you just used a larger type. e.g. long long int or long double ;) BTW assembly has something similar to exceptions called traps or interrupts, overflows/underflow doesn't cause a trap AFAIK.
Credit goes to Peter Lawrey:
提示:有时使用 google 可以在您提出问题之前回答您的问题。
你必须使用 BigInteger。
BigInteger bi1, bi2, bi3;
// assign values to bi1, bi2
bi1 = new BigInteger("123");
bi2 = new BigInteger("50");
// perform multiply operation on bi1 using bi2
bi3 = bi1.multiply(bi2);
String str = "Result of multiply is " +bi3;;
// print bi3 value
System.out.println( str );
来自Java Language Specification,
The integer operators do not indicate overflow or underflow in any way.
这就是你没有得到任何异常的原因。
结果specified by the language如下,
If an integer multiplication overflows, then the result is the low-order bits of the mathematical product as represented in some sufficiently large two's-complement format. As a result, if overflow occurs, then the sign of the result may not be the same as the sign of the mathematical product of the two operand values.
Integer.MAX_VALUE + 1 == Integer.MIN_VALUE
Integer.MIN_VALUE - 1 == Integer.MAX_VALUE
这就是您得到零结果的原因。