Domanda

Come potrei fare per fare calcoli con numeri estremamente grandi in Java?

Ho provato long ma che arriva al massimo a 9223372036854775807, e quando si utilizza un numero intero non salva cifre sufficienti e, pertanto, non è abbastanza preciso per quello che mi serve.

C'è comunque intorno a questo?

È stato utile?

Soluzione

È possibile utilizzare la BigInteger classe per gli interi e BigDecimal per i numeri con cifre decimali. Entrambe le classi sono definite in java.math pacchetto.

Esempio:

BigInteger reallyBig = new BigInteger("1234567890123456890");
BigInteger notSoBig = new BigInteger("2743561234");
reallyBig = reallyBig.add(notSoBig);

Altri suggerimenti

Utilizzare la BigInteger classe che è una parte della libreria Java.

http: //java.sun. com / J2SE / 1.5.0 / docs / api / java / math / BigInteger.html

Ecco un esempio che ottiene grandi numeri molto rapidamente.

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;
    }
}

Checkout BigDecimal e BigInteger.

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));
    }
}

A seconda di quello che stai facendo, come si potrebbe dare un'occhiata a GMP (gmplib.org), che è una libreria multi-precisione ad alte prestazioni. Per usarlo in Java è necessario wrapper JNI intorno alla biblioteca binario.

Vedere parte del codice Alioth Shootout per un esempio di utilizzo invece di BigInteger per calcolare Pi ad un numero arbitrario di cifre.

https: //benchmarksgame-team.pages .debian.net / benchmarksgame / programma / pidigits-java-2.html

utilizzando stringa tipo di dati si risolve facilmente questo problema.

class Account{
      String acc_no;
      String name;
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top