Java - How to check if a division is an integer or a float?
Asked Answered
Q

7

12

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.

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 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.

Quinary answered 23/8, 2012 at 18:40 Comment(2)
Division of two ints will always have an int result. You can use modulus (%) instead.Texas
FWIW, I'd suggest trying a different approach to solve this problem. This approach of looping to look for a number you can subtract from i to get a multiple of 16 is terribly inefficient. And what if the closest multiple of 16 is greater than i?Unpriced
I
5

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:

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
Iasis answered 23/8, 2012 at 20:1 Comment(0)
C
18

Use the mod operator. Mod gives you the remainder of a division operation.

public boolean isEvenlyDivisable(int a, int b) {
    return a % b == 0;
}
Cutoff answered 23/8, 2012 at 18:44 Comment(0)
F
5

(i - x)/16 is integer when remainder of (i - x)/16 is 0. Use %(modulus) operator like :

if((i - x)%16 == 0) {
   // (i-x)/16 is integer
}
Foetation answered 23/8, 2012 at 18:42 Comment(0)
I
5

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:

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
Iasis answered 23/8, 2012 at 20:1 Comment(0)
G
4

There are a number of striking issues with your original code:

  1. 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.
  2. 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.
  3. 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.
  4. If you need to perform integer division of number and ignore any remainder you can simply shift (>>) by 4 bits to do that instead.
Geometrid answered 23/8, 2012 at 18:53 Comment(0)
A
1

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.

If you really want nearest then 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.

Ascent answered 23/8, 2012 at 18:53 Comment(3)
Depending on the definition of "closest", you might need to add 16.Unpriced
True, his examples (and code) seemed to indicate, he wanted closest less than or equal to...Ascent
@Unpriced now you really have nearest : )Ascent
P
1

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'.

Pidgin answered 6/2, 2017 at 15:57 Comment(0)
J
0

If you need the nearest multiple 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

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):

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;
Jenaejenda answered 23/8, 2012 at 19:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.