为什么bucket4j中的计算先进行乘法然后除法?

时间:2018-09-17 13:34:55

标签: java

我发现bucket4j中的代码总是先进行乘法,然后进行除法。

private long calculateDelayNanosAfterWillBePossibleToConsumeForGreedyBandwidth(Bandwidth bandwidth, long deficit) {
    long refillPeriodNanos = bandwidth.refillPeriodNanos;
    long refillPeriodTokens = bandwidth.refillTokens;
    long divided = multiplyExactOrReturnMaxValue(refillPeriodNanos, deficit);
    if (divided == Long.MAX_VALUE) {
        // math overflow happen.
        // there is no sense to stay in integer arithmetic when having deal with so big numbers
        return (long)((double) deficit / (double)refillPeriodTokens * (double)refillPeriodNanos);
    } else {
        return divided / refillPeriodTokens;
    }
}

(以及计算类BucketState中的令牌的其他部分)

那么仅仅做(long)((double) deficit / (double)refillPeriodTokens * (double)refillPeriodNanos)有什么区别?

要更准确?但是怎么可能呢?
有人可以为此提供理由吗?我感到困惑。 :)

0 个答案:

没有答案