Is it possible to have multiple BigInteger using Double in Java?

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

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

+4
source share
4 answers

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

This will not be particularly fast, unfortunately, since BigInteger.doubleValue() has a noticeably slow implementation. (Perhaps it will be faster in the future ... perhaps if Oracle applies my patch .)

Alternatively, you can combine double directly into BigInteger using Guava DoubleMath.roundToBigInteger(double, RoundingMode) .

+7
source

To maintain arbitrary precision for as long as possible, do the multiplication in BigDecimal , and then convert the result to double , for example:

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

Call .doubleValue() on BigInteger and double them.

+1
source

Why doesn't BigInteger#multiply() help? In fact, there are only two reasonable answers:

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

Source: https://habr.com/ru/post/1442567/


All Articles