我们如何才能在 Java 中获得大于 10^20 的 long 值,如 Sedgewick 的算法第 4 版,p. 中所建议的那样。 778?

How can we get a long value larger than 10^20 in Java, as suggested in Sedgewick's Algorithms 4th edition, p. 778?

在谈到 Rabin-Karp 的实施时,Robert Sedgewick 和 Kevin Wayne 的 "Algorithms - Fourth Edition",第 5.3 章 "Substring Search" 第 778 页指出:

We will use a long value greater than 10^20, making the probability that a random key hashes to the same value as our pattern less than 10^(-20), an exceedingly small value.

但是,10^20 似乎 Long.MAX_VALUE 的 9,223,372,036,854,775,807 或 2^63 - 1

"a long value greater than 10^20"如何使用?

如果你使用 long,你可以使用 BigInteger。这可以任意长

您还可以在处理浮点数时使用 BigDecimal

long 无法存储你是对的


  • 这些可以:

    float f  = 10000000000000000000f;  
    double d = 10000000000000000000d; 
    BigInteger bigI = new BigInteger("100000000000000000000");
    BigDecimal bigD = new BigDecimal("100000000000000000000");
    
  • 这些不能

    byte b  = 10000000000000000000;    
    short s = 10000000000000000000; 
    int i   = 10000000000000000000;    
    long l  = 10000000000000000000L;