Java - 如何检查除法是整数还是浮点数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12098072/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java - How to check if a division is an integer or a float?
提问by user1541106
Couldnt think of a better title. Well the problem is: I have the "int i", it can be any value. my goal is to transform the "int i" to the closest number divisible by 16.
想不出更好的标题。那么问题是:我有“int i”,它可以是任何值。我的目标是将“int i”转换为最接近的可被16整除的数字。
For example, I got i = 33. Then i will be turned to 32(16x2).But if I get i = 50, then it will be turned to 48(16x3).
例如,我得到i = 33。然后我将变成32(16x2)。但是如果我得到i = 50,那么它将变成48(16x3)。
I tried many things for example:
我尝试了很多东西,例如:
for (int x = i; x < 999; x++){
if ( (i - x)/16 *is an integer*){
i = i - x;
}
But I dont know how to check if its an integer. So maybe my previous code work, but I just need to find a way to check if its an integer or a float. So.. any help is appreciated.
但我不知道如何检查它是否是整数。所以也许我以前的代码有效,但我只需要找到一种方法来检查它是整数还是浮点数。所以.. 任何帮助表示赞赏。
采纳答案by Paul
Since all ints which are divisible by 16 will have their last 4 bits all set to 0. You can accomplish what you want without a loop or even an if statement:
由于所有可被 16 整除的整数的最后 4 位都设置为 0。您可以在没有循环甚至 if 语句的情况下完成您想要的操作:
i &= 0xfffffff0; // Sets i to the greatest multiple of 16 less than i, or 0 for i < 16
For example:
例如:
int i = 50;
i &= 0xfffffff0; // i == 48
i = 39;
i &= 0xfffffff0; // i == 32
i = 16;
i &= 0xfffffff0; // i == 16
回答by Will Hartung
Use the mod operator. Mod gives you the remainder of a division operation.
使用 mod 运算符。Mod 为您提供除法运算的剩余部分。
public boolean isEvenlyDivisable(int a, int b) {
return a % b == 0;
}
回答by Nandkumar Tekale
(i - x)/16
is integer when remainder of (i - x)/16
is 0. Use %(modulus) operator like :
(i - x)/16
当余数(i - x)/16
为 0时为整数。使用 %(modulus) 运算符,如:
if((i - x)%16 == 0) {
// (i-x)/16 is integer
}
回答by user268396
There are a number of striking issues with your original code:
您的原始代码有许多引人注目的问题:
- If you are rounding down to the nearest multiple of 16, it follows that the highest value you possibly may have to substract is 15. Therefore the upper boundary of your loop should be at most 15.
- As others have noted you could use the modulo operator (
%
) to determine the exact value to substract from a given value to round it down to the nearest multiple of 16. This removes the need for a loop entirely. - But since 16 is a power of 2, and since integers are represented as a binary number with 32 digits (i.e. 32bits), you can calculate the value more directly by using a bitmask to zero out any digits smaller than 16 in the number. In Java you can use the binary & operator for that purpose like this:
i & 0xfffffff0
. This will zero out the last 4 digits (those representing: 8-4-2-1), which effectively rounds your number down to the nearest value divisible by 16. - If you need to perform integer division of number and ignore any remainder you can simply shift (
>>
) by 4 bits to do that instead.
- 如果您四舍五入到最接近的 16 倍数,那么您可能需要减去的最高值是 15。因此,您的循环的上限应最多为 15。
- 正如其他人所指出的,您可以使用模运算符 (
%
) 来确定从给定值中减去的确切值,以将其四舍五入到最接近的 16 的倍数。这完全消除了对循环的需要。 - 但由于 16 是 2 的幂,并且由于整数表示为具有 32 位(即 32 位)的二进制数,您可以通过使用位掩码将数字中小于 16 的任何数字归零来更直接地计算该值。在Java中,你可以使用二进制和运营商用于该目的是这样的:
i & 0xfffffff0
。这会将最后 4 位数字(那些代表:8-4-2-1)清零,从而有效地将您的数字向下舍入到最接近的可被 16 整除的值。 - 如果您需要执行数字的整数除法并忽略任何余数,您可以简单地将 (
>>
)移位4 位来代替。
回答by xbakesx
To find out whether a number evenly divides another, check out the other answers, Modulo (%) is the way to do that.
要找出一个数是否能整除另一个数,请查看其他答案,模数 (%) 是这样做的方法。
To do what you want to do above, you don't need a loop:
要做上面你想做的事情,你不需要循环:
public int nearestDivider(final int input)
{
final int multiple = input / 16; // this will divide by 16 and it's integer math, so it loses the decimal
return multiple * 16;
}
That will return 48 if you give it 50 like your example.
如果你像你的例子那样给它 50,那将返回 48。
If you really want nearestthen you will have to do some floating point division
如果你真的想要最近的那么你将不得不做一些浮点除法
public int nearestDivider(final int input)
{
final int multiple = Math.round((float) input / 16);
return multiple * 16;
}
Now 46 returns 48, 149 returns 144 etc.
现在 46 返回 48,149 返回 144 等等。
回答by Rajesh J Advani
If you need the nearestmultiple of 16, then you have two cases for dealing with odd multiples of 8. 1. 8 becomes 16, 24 becomes 32 2. 8 becomes 0, 24 becomes 16
如果你需要最接近的16的倍数,那么你有两种情况来处理8的奇数倍。1.8变成16,24变成32 2.8变成0,24变成16
For the first:
为了第一:
int j = ((i+8)/16)*16;
In the second case:
在第二种情况下:
int j = ((i+7)/16)*16;
If you want always want to round DOWN (i.e. 17 becomes 16, and 15 becomes 0):
如果你想一直向下取整(即 17 变成 16,15 变成 0):
int j = (i/16)*16;
If you wanted to always round UP (not what your example says), you would have done this instead:
如果你想总是四舍五入(不是你的例子所说的),你可以这样做:
int j = ((i+15)/16)*16;
回答by RabbitBones22
In order to check if a random division results in an integer or fraction you need the following:
为了检查随机除法结果是整数还是分数,您需要以下内容:
int n = 9;
int p = 3;
if (n % p == 0) {
//the division results in an integer.
}
else
{
//the division results in a fraction.
}
You can do this as an alternative:
您可以这样做作为替代:
if (n / p == Math.ceil((double) n / (double) p)) {
//the division results in an integer.
}
else
{
//the division results in a fraction.
}
One needs Math.ceil() and not round or floor, because a integer division is almost equal to a floor and fraction will be rounded down and appear as 'integer division'.
需要 Math.ceil() 而不是 round 或 floor,因为整数除法几乎等于 floor,分数将向下舍入并显示为“整数除法”。