Java How to check if division is integer or float?

I could not think of a better title. Well, the problem: I have "int i", it can be any value. my goal is to convert "int i" to the nearest number divisible by 16 .

For example, I got i = 33 . Then I will be rotated 32 (16x2). But if I get i = 50 , then it will be rotated to 48 (16x3).

I have tried many things, for example:

for (int x = i; x < 999; x++){ if ( (i - x)/16 *is an integer*){ i = i - x; } 

But I do not know how to check if an integer is. So maybe my previous code works, but I just need to find a way to check if its integer or floating. So ... any help is appreciated.

+8
java math int division
source share
7 answers

Since all ints that are divisible by 16 will have their last 4 bits, all equal to 0. You can do 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 
+5
source share

Use the mod statement. Mod gives you the rest of the division operation.

 public boolean isEvenlyDivisable(int a, int b) { return a % b == 0; } 
+12
source share

(i - x)/16 is an integer if the remainder of (i - x)/16 is 0. Use the% (module) operator, for example:

 if((i - x)%16 == 0) { // (ix)/16 is integer } 
+4
source share

The source code has a number of striking issues:

  • If you round to the nearest multiple of 16, it follows that the largest value you might have to subtract is 15. Therefore, the upper limit of your cycle should be no more than 15.
  • As already noted, you can use the modulo operator ( % ) to determine the exact value to subtract from the given value, to round it to the nearest multiple of 16. This completely eliminates the need for a loop.
  • But since 16 is a power of 2, and since integers are represented as a binary number with 32 digits (i.e. 32 bits), you can calculate a larger value using the bitmask to reset any digits smaller than 16 to the number. In Java, you can use the binary and operator for this purpose as follows: i & 0xfffffff0 . This will reset the last 4 digits (those that represent: 8-4-2-1), which effectively rounds your number to the nearest value divisible by 16.
  • If you need to perform integer division of a number and ignore any remainder, you can simply shift ( >> ) by 4 bits to do this instead.
+4
source share

To find out if the number divides evenly, check other answers, Modulo (%) is a way to do this.

To do what you want to do above, you do not need a loop:

 public int nearestDivider(final int input) { final int multiple = input / 16; // this will divide by 16 and it integer math, so it loses the decimal return multiple * 16; } 

This will return 48 if you give it 50, as your example.

If you really want the closest, you will need to do some floating point separation

 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.

+1
source share

If you need the nearest multiple of 16, you have two cases for working with the odd multiple 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 to always want to round DOWN (i.e. 17 becomes 16 and 15 becomes 0):

 int j = (i/16)*16; 

If you want to always round UP (not what your example says), you would do this instead:

 int j = ((i+15)/16)*16; 
0
source share

To check if 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. } 

You need Math.ceil (), not round or floor, because integer division is almost equal to gender, and the fraction is rounded down and displayed as "integer division".

0
source share

All Articles