本文介绍了ceil在Java中与Math.floorDiv竞争?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ceil对应项="noreferrer"> Math.floorDiv()

Is there any ceil counterpart for Math.floorDiv()

如何用我们现有的方法最快地计算出来?

How to calculate it fastest way with what we have?

更新

floorDiv()的代码如下:

 public static long floorDiv(long x, long y) {
        long r = x / y;
        // if the signs are different and modulo not zero, round down
        if ((x ^ y) < 0 && (r * y != x)) {
            r--;
        }
        return r;
    }

我们可以用类似的方式对ceil进行编码吗?

Can we code ceil the similar way?

更新2

我看到了这个答案 https://stackoverflow.com/a/7446742/258483 ,但似乎也有许多不必要的操作.

I saw this answer https://stackoverflow.com/a/7446742/258483 but it seems to have too many unnecessary operations.

推荐答案

Math类中没有任何内容,但是您可以轻松地计算出它

There is none in the Math class, but you can easily calculate it

long ceilDiv(long x, long y){
    return -Math.floorDiv(-x,y);
}

例如,ceilDiv(1,2) = -floorDiv(-1,2) = -(-1) = 1(正确答案).

For example, ceilDiv(1,2) = -floorDiv(-1,2) =-(-1)= 1 (correct answer).

这篇关于ceil在Java中与Math.floorDiv竞争?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 09:40