Java中的Double可以多个BigInteger吗?

时间:2021-12-31 16:48:31

I have a very large number (number1) stored as a BigInteger, and a double (number2). I plan to multiply number1 and number2, and store the result as a double.

我有一个非常大的数字(number1)存储为BigInteger和一个double(number2)。我计划将number1和number2相乘,并将结果存储为double。

Using the multiply() method has not helped me achieve this. What is the way forward?

使用multiply()方法并没有帮助我实现这一目标。前进的方向是什么?

4 个解决方案

#1


7  

The simplest solution is probably big.doubleValue() * myDouble.

最简单的解决方案可能是big.doubleValue()* myDouble。

This won't be particularly fast, unfortunately, since BigInteger.doubleValue() has a notably slow implementation. (It might be faster in the future...perhaps if Oracle applies my patch.)

遗憾的是,这不会特别快,因为BigInteger.doubleValue()的实现速度非常慢。 (未来可能会更快......也许如果Oracle应用我的补丁。)

Alternately, you can round a double directly to a BigInteger using Guava's DoubleMath.roundToBigInteger(double, RoundingMode).

或者,您可以使用Guava的DoubleMath.roundToBigInteger(double,RoundingMode)将double直接舍入到BigInteger。

#2


14  

In order to preserve the arbitrary precision as long as possible, do the multiplication in BigDecimal, and then convert the result to double, like this:

为了尽可能长时间地保留任意精度,请在BigDecimal中进行乘法运算,然后将结果转换为double,如下所示:

BigDecimal tmp = new BigDecimal(myBigInteger);
tmp = tmp.multiply(new BigDecimal(myDouble));
double res = tmp.doubleValue();

#3


1  

Call .doubleValue() on the BigInteger, and multiply them as doubles.

在BigInteger上调用.doubleValue(),并将它们乘以双精度数。

#4


1  

Why is BigInteger#multiply() not helpful? There are really only two reasonable answers:

为什么BigInteger#multiply()没有帮助?实际上只有两个合理的答案:

BigInteger a = /* whatever */;
double b = /* whatever */

// either
double result = a.multiply(new BigInteger(b)).doubleValue();
// or
double result = a.doubleValue() * b;

#1


7  

The simplest solution is probably big.doubleValue() * myDouble.

最简单的解决方案可能是big.doubleValue()* myDouble。

This won't be particularly fast, unfortunately, since BigInteger.doubleValue() has a notably slow implementation. (It might be faster in the future...perhaps if Oracle applies my patch.)

遗憾的是,这不会特别快,因为BigInteger.doubleValue()的实现速度非常慢。 (未来可能会更快......也许如果Oracle应用我的补丁。)

Alternately, you can round a double directly to a BigInteger using Guava's DoubleMath.roundToBigInteger(double, RoundingMode).

或者,您可以使用Guava的DoubleMath.roundToBigInteger(double,RoundingMode)将double直接舍入到BigInteger。

#2


14  

In order to preserve the arbitrary precision as long as possible, do the multiplication in BigDecimal, and then convert the result to double, like this:

为了尽可能长时间地保留任意精度,请在BigDecimal中进行乘法运算,然后将结果转换为double,如下所示:

BigDecimal tmp = new BigDecimal(myBigInteger);
tmp = tmp.multiply(new BigDecimal(myDouble));
double res = tmp.doubleValue();

#3


1  

Call .doubleValue() on the BigInteger, and multiply them as doubles.

在BigInteger上调用.doubleValue(),并将它们乘以双精度数。

#4


1  

Why is BigInteger#multiply() not helpful? There are really only two reasonable answers:

为什么BigInteger#multiply()没有帮助?实际上只有两个合理的答案:

BigInteger a = /* whatever */;
double b = /* whatever */

// either
double result = a.multiply(new BigInteger(b)).doubleValue();
// or
double result = a.doubleValue() * b;