Java 使用 Math.ceil 向上舍入为 int [英] Java rounding up to an int using Math.ceil

查看:21
本文介绍了Java 使用 Math.ceil 向上舍入为 int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

int total = (int) Math.ceil(157/32);

为什么还是返回4?157/32 = 4.90625,我需要四舍五入,我环顾四周,这似乎是正确的方法.

我尝试将 total 作为 double 类型,但得到 4.0.

我做错了什么?

解决方案

你正在做 157/32 将两个整数相除,这总是导致一个向下取整的整数.因此 (int) Math.ceil(...) 没有做任何事情.有三种可能的解决方案来实现你想要的.我推荐使用选项 1选项 2.请不要使用选项 0.

选项 0

ab 转换为双精度型,您可以根据需要使用除法和 Math.ceil.但是我强烈反对使用这种方法,因为双除法可能不精确.要了解有关双精度不精确的更多信息,请参阅 上提问.

int total = (int) Math.ceil(157/32);

Why does it still return 4? 157/32 = 4.90625, I need to round up, I've looked around and this seems to be the right method.

I tried total as double type, but get 4.0.

What am I doing wrong?

解决方案

You are doing 157/32 which is dividing two integers with each other, which always result in a rounded down integer. Therefore the (int) Math.ceil(...) isn't doing anything. There are three possible solutions to achieve what you want. I recommend using either option 1 or option 2. Please do NOT use option 0.

Option 0

Convert a and b to a double, and you can use the division and Math.ceil as you wanted it to work. However I strongly discourage the use of this approach, because double division can be imprecise. To read more about imprecision of doubles see this question.

int n = (int) Math.ceil((double) a / b));

Option 1

int n = a / b + ((a % b == 0) ? 0 : 1); 

You do a / b with always floor if a and b are both integers. Then you have an inline if-statement witch checks whether or not you should ceil instead of floor. So +1 or +0, if there is a remainder with the division you need +1. a % b == 0 checks for the remainder.

Option 2

This option is very short, but maybe for some less intuitive. I think this less intuitive approach would be faster than the double division and comparison approach:
Please note that this doesn't work for b < 0.

int n = (a + b - 1) / b;

To reduce the chance of overflow you could use the following. However please note that it doesn't work for a = 0 and b < 1.

int n = (a - 1) / b + 1;

Explanation behind the "less intuitive approach"

Since dividing two integer in Java (and most other programming languages) will always floor the result. So:

int a, b;
int result = a/b (is the same as floor(a/b) )

But we don't want floor(a/b), but ceil(a/b), and using the definitions and plots from Wikipedia:

With these plots of the floor and ceil function you can see the relationship.

You can see that floor(x) <= ceil(x). We need floor(x + s) = ceil(x). So we need to find s. If we take 1/2 <= s < 1 it will be just right (try some numbers and you will see it does, I find it hard myself to prove this). And 1/2 <= (b-1) / b < 1, so

ceil(a/b) = floor(a/b + s)
          = floor(a/b + (b-1)/b)
          = floor( (a+b-1)/b) )

This is not a real proof, but I hope your are satisfied with it. If someone can explain it better I would appreciate it too. Maybe ask it on MathOverflow.

这篇关于Java 使用 Math.ceil 向上舍入为 int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆