13

It seems that in order to find both the quotient and remainder of a division in Java, one has to do:

int a = ...
int b = ...

int quotient = a / b;
int remainder = a % b;

Is there a way to write this so that the quotient and remainder are found in a single step (one division operation)? Or does Java already automatically optimize this code so that they are?

FuzzyCat444
  • 315
  • 2
  • 7
  • 2
    How would you do it in a single operation on paper? – Shark Sep 21 '18 at 16:14
  • You can't assign a value to two different variables with two different values in one line. – maio290 Sep 21 '18 at 16:14
  • @Shark On paper, long division provides you with the quotient and remainder at the same time. I am told division algorithms tend to only provide one or the other. – FuzzyCat444 Sep 21 '18 at 16:21
  • not really. long division is something else... consider `11` and `5`. `11` divided by `5` is `2`. remainder of `11/5` is `1`. long division would give you `2.2`, which isn't both things in one operation. – Shark Sep 21 '18 at 16:36
  • 1
    @Shark you can stop long division at any step. You get that `5` goes into `11` twice. `2*5=10`. `11-10=1=remainder`. – FuzzyCat444 Sep 21 '18 at 16:52
  • 1
    In Java there is not way to do that exactly. When it gets JIT’d it may simplify it into the single `idiv` instruction. You could also potentially use JNI, but that will likely end up being significantly slower. – vandench Sep 21 '18 at 16:58
  • @vandench Thank you! That is the answer I was looking for! – FuzzyCat444 Sep 21 '18 at 16:59
  • a%b --> what it provides?? It provides Remainder. But a/b provides quotient. – Ravi May 10 '22 at 09:22
  • According to [this answer](https://stackoverflow.com/a/59806366/6735159), Java seems to optimize such code by itself. – Jakob Aug 17 '23 at 07:40

4 Answers4

5

The natural behaviour of all architectures is for the divide instructions to supply the quotient and remainder in separate registers (for binary) or storage areas (for packed decimal as found on the IBM zSeries). The only high level language that I know of that does the same is COBOL. It does always seem wasteful having to repeat the divide instruction again to get the remainder.

Mike B
  • 74
  • 1
  • 2
  • C's stdlib has `div()`, python has `divmod()`, C# has `Math.DivRem()`, so there seems to be support for it in most languages. Java seems to support it only for BigInteger (and related) with `.divideAndRemainder()`, but it's probably only relevant for such special cases. – Jakob Aug 17 '23 at 07:47
4

There is no way to do this in one step as both are different operations. One is division and other is remainder. So you require two variables to store result for both operations.

Yug Singh
  • 3,112
  • 5
  • 27
  • 52
  • 1
    But the computer finds the remainder when it does division right? I want to avoid doing a second division just to get the remainder. – FuzzyCat444 Sep 21 '18 at 16:18
  • 2
    @FuzzyCat444 "the computer finds the remainder when it does division right?" - no, it finds the quotient. Remainders are a separate instruction. – Strikegently Sep 21 '18 at 16:19
  • 1
    @Strikegently That answers my question fully then – FuzzyCat444 Sep 21 '18 at 16:20
  • 2
    @Strikegently I found on one post by Reinderien [here](https://www.google.com/url?sa=t&source=web&rct=j&url=https://stackoverflow.com/questions/5454574/how-do-computers-find-modulus&ved=2ahUKEwipgJCiwczdAhXtRN8KHVZmAzMQFjAMegQICBAB&usg=AOvVaw1Kuvj1ncwiXZJ4RXO9kHmo) that "Often, the modulus and divide operations on a processor are the same thing. For instance, refer to http://jsimlo.sk/docs/cpu/index.php/div.html. This is the implementation of the divide instruction on Intel processors." – FuzzyCat444 Sep 21 '18 at 16:26
  • 1
    But Java can't run assembly because different processors have different instruction sets. You could perhaps do this in an inline `__asm` block in C, but not Java, without overcomplicating things... By writing the necessary C code, compiling it as a shared library, loading the library, and invoking the necessary JNI instruction to return a `Pair` which would include the quotient and the remainder. – Shark Sep 21 '18 at 16:35
  • @FuzzyCat444 As mentioned in the https://stackoverflow.com/q/5454574/6407858 this is the case with assembly. – Yug Singh Sep 21 '18 at 16:36
-1

If you need to do this,

int a = ...
int b = ...

int quotient = a / b;
int remainder = b - (a * b);

uses slightly less CPU, but there's not much in it unless you're doing it lots of times. Doing it a billion times took 5.3 seconds the first way, 3.8 the second. There's a FOR loop in there too in my test.

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
ChrisH
  • 1
-2
public int []result (int a , int b){
int quotient = a / b;
int remainder = a % b;
int [] array=new int[2];
array[0]=quotient;
array[1]=remainder;
return array;
}

public void main(){

int a=..;
int b=...;

int [] results=result(a,b);
}