简体   繁体   中英

When would you use object BigInteger instead of simply using double?

So I was given a problem telling me to make a table of factorials of integers up to number 30. The book specifically tells me to use the object BigInteger. (using BigInteger big= BigInteger.valueOf(x)) However doing so is pretty tricky and gives me a bunch of errors that I have no idea how to fix.

for example

public static BigInteger factorial(int a){
        if(a == 0){
            return BigInteger.valueOf(1);
        }
        else{
            return BigInteger.valueOf(a*BigInteger.valueOf(factorial(a-1)));// this will keep giving me an error message that I need to change the value type to "long" and back and forth to BIgInteger.  I've tried many different variations including where I use BigInteger.valueOf(..) to every values.
        }

    }  

Do you know a correct way to use the BigInteger object?

When would you ever use BigInteger instead of double?

   import java.math.BigInteger;
        public class BigInt {

            public static double factorial(int a){
                if(a == 0){
                    return 1;
                }
                else{
                    return a* factorial(a-1);
                }

            }
            public static void table(int a){
                for(int i =0; i<=a; i++){
                    System.out.println(i + ", " + factorial(i) );

                    }
                }

            public static void main(String[] args) {
            table(30);
            }

        }

Instead of

BigInteger.valueOf(a*BigInteger.valueOf(factorial(a-1)))

try

factorial(a - 1).multiply(BigInteger.valueOf(a))

You are currently trying to use the * operator to multiply an int and BigInteger ; that isn't allowed in Java, since operator overloading isn't supported.

As to why you'd use BigInteger instead of double : double only supports a finite number of significant figures before it starts rounding. Using BigInteger allows you to have arbitrarily-large numbers.

When you are using BigInteger you can't use operators such as * . You must use methods of the BigInteger class :

return factorial(a-1).multiply(a);

The reason for using BigInteger instead of double is precision. double has limited precision, so large integers can't be represented accurately.

EDIT: You should actually use

return BigInteger.valueOf(a).multiply(factorial(a-1));

since BigInteger multiply(long v) is package private.

It's not a primitive type so * doesn't work. For detail description read this article, A brief description about use of object BigInteger. I Hope it will help you a lot. Java:Why should we use BigDecimal instead of Double in the real world?

When we deal with numbers related to money , where we require results in nearest precision . Then we use BigInteger instead of double. But comparing with double , Big Integer is slow in performance as internally BigInteger requires more number of operators for processing. I recommend use of javadoc ( http://docs.oracle.com/javase/1.5.0/docs/api/java/math/BigDecimal.html ).

Using BigInteger is also necessary when performing numerical calculations with large integers for eg Cryptographic purposes. The rounding used on floats or doubles would make applying the theories underlying these Cryptographic methods impossible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM