简体   繁体   中英

How does BigInteger algorithm work internally (Java)?

How does Java's BigInteger divide work internally? I did a ton of research and I can't find anything. So instead, I did some research into how CPUs do division, and looked into stuff like restoring/non-restoring division and fast division algorithms. However, I don't think these would work for BigInteger, because it seems like BigInteger has to make use of Java's native number primitive operations. If BigInteger did stuff bit by bit like these techniques outline, it would be too slow.

OpenJDK's implementation (yes this is implementation-dependent) uses either Knuth's "Algorithm D", or the Burnikel-Ziegler Algorithm, depending on how big the divisor and the dividend are. If the divisor is small, or if they are both of similar magnitude, then the former is used. You can see the source code here .

public BigInteger divide(BigInteger val) {
    if (val.mag.length < BURNIKEL_ZIEGLER_THRESHOLD ||
            mag.length - val.mag.length < BURNIKEL_ZIEGLER_OFFSET) {
        return divideKnuth(val);
    } else {
        return divideBurnikelZiegler(val);
    }
}

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