Java 中的大数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/849813/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Large Numbers in Java
提问by Petey B
How would I go about doing calculations with extremely large numbers in Java?
我将如何在 Java 中进行非常大的计算?
I have tried long
but that maxes out at 9223372036854775807, and when using an integer it does not save enough digits and therefore is not accurate enough for what I need.
我试过,long
但在 9223372036854775807 处达到最大值,当使用整数时,它没有保存足够的数字,因此对于我需要的来说不够准确。
Is there anyway around this?
有没有办法解决?
采纳答案by Fabio Vinicius Binder
You can use the BigInteger
class for integers and BigDecimal
for numbers with decimal digits. Both classes are defined in java.math
package.
您可以将该BigInteger
类用于整数和BigDecimal
带有十进制数字的数字。这两个类都在java.math
包中定义。
Example:
例子:
BigInteger reallyBig = new BigInteger("1234567890123456890");
BigInteger notSoBig = new BigInteger("2743561234");
reallyBig = reallyBig.add(notSoBig);
回答by Clint Miller
Checkout BigDecimal
and BigInteger
.
结帐BigDecimal
和BigInteger
。
回答by AlbertoPL
Use the BigInteger
class that is a part of the Java library.
使用BigInteger
属于 Java 库一部分的类。
http://java.sun.com/j2se/1.5.0/docs/api/java/math/BigInteger.html
http://java.sun.com/j2se/1.5.0/docs/api/java/math/BigInteger.html
回答by Peter Lawrey
Here is an example which gets big numbers very quickly.
这是一个非常快速地获得大数字的示例。
import java.math.BigInteger;
/*
250000th fib # is: 36356117010939561826426 .... 10243516470957309231046875
Time to compute: 3.5 seconds.
1000000th fib # is: 1953282128707757731632 .... 93411568996526838242546875
Time to compute: 58.1 seconds.
*/
public class Main {
public static void main(String... args) {
int place = args.length > 0 ? Integer.parseInt(args[0]) : 250 * 1000;
long start = System.nanoTime();
BigInteger fibNumber = fib(place);
long time = System.nanoTime() - start;
System.out.println(place + "th fib # is: " + fibNumber);
System.out.printf("Time to compute: %5.1f seconds.%n", time / 1.0e9);
}
private static BigInteger fib(int place) {
BigInteger a = new BigInteger("0");
BigInteger b = new BigInteger("1");
while (place-- > 1) {
BigInteger t = b;
b = a.add(b);
a = t;
}
return b;
}
}
回答by Trevor Tippins
Depending on what you're doing you might like to take a look at GMP (gmplib.org) which is a high-performance multi-precision library. To use it in Java you need JNI wrappers around the binary library.
根据你在做什么,你可能想看看 GMP (gmplib.org),它是一个高性能的多精度库。要在 Java 中使用它,您需要围绕二进制库的 JNI 包装器。
See some of the Alioth Shootout code for an example of using it instead of BigInteger to calculate Pi to an arbitrary number of digits.
有关使用它代替 BigInteger 将 Pi 计算为任意位数的示例,请参阅一些 Alioth Shootout 代码。
https://benchmarksgame-team.pages.debian.net/benchmarksgame/program/pidigits-java-2.html
https://benchmarksgame-team.pages.debian.net/benchmarksgame/program/pidigits-java-2.html
回答by Rupendra Sharma
import java.math.BigInteger;
import java.util.*;
class A
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
System.out.print("Enter The First Number= ");
String a=in.next();
System.out.print("Enter The Second Number= ");
String b=in.next();
BigInteger obj=new BigInteger(a);
BigInteger obj1=new BigInteger(b);
System.out.println("Sum="+obj.add(obj1));
}
}