Avoiding having to do the same calculation twice

Suppose I want to find both one number divided by another and the remainder of this, like so:
[CODE]int x = 50, y = 7;

int a = x / y,
b = x % y;[/CODE]This seems like an efficient way of doing things because the same calculation is being carried out twice. Does anyone know a trick for only having to do the calculation once and thereby saving processing time?

Dan :-)

• [b][red]This message was edited by IDK at 2006-2-28 8:41:56[/red][/b][hr]
: Suppose I want to find both one number divided by another and the remainder of this, like so:
: [CODE]int x = 50, y = 7;
:
: int a = x / y,
: b = x % y;[/CODE]This seems like an efficient way of doing things because the same calculation is being carried out twice. Does anyone know a trick for only having to do the calculation once and thereby saving processing time?
:
: Thanks in advance,
: Dan :-)
:

I think the compiler optimises it...

The asm for it is:
[code]
mov ax,50
mov dx,0
mov bx,7
div bx ; means ax:dx divided by bx
;ax got the answear
;dx got the remainder
[/code]

Disassemble it and compare...

What I'm saying if you don't understand assembly is that the computer only got one way of dividing, and it returns the result and the remainder when doing it.

The compiler should, (if it is a good one) optimise it to the code I entered above, wich got only one division.
• Thank you very much my friend. So a compiler like VC++ 6 should optmize this automatically?

: [b][red]This message was edited by IDK at 2006-2-28 8:41:56[/red][/b][hr]
: : Suppose I want to find both one number divided by another and the remainder of this, like so:
: : [CODE]int x = 50, y = 7;
: :
: : int a = x / y,
: : b = x % y;[/CODE]This seems like an efficient way of doing things because the same calculation is being carried out twice. Does anyone know a trick for only having to do the calculation once and thereby saving processing time?
: :
: : Thanks in advance,
: : Dan :-)
: :
:
: I think the compiler optimises it...
:
: The asm for it is:
: [code]
: mov ax,50
: mov dx,0
: mov bx,7
: div bx ; means ax:dx divided by bx
: ;ax got the answear
: ;dx got the remainder
: [/code]
:
: Disassemble it and compare...
:
: What I'm saying if you don't understand assembly is that the computer only got one way of dividing, and it returns the result and the remainder when doing it.
:
: The compiler should, (if it is a good one) optimise it to the code I entered above, wich got only one division.
:

• [b][red]This message was edited by stober at 2006-2-28 10:41:17[/red][/b][hr]
if you set your compiler to produce assembly code output you will find that VC++ 6.0 compiled for Release mode optimizes out both those divisions. But if you put that code in another function, the compiler will not optimize it out -- both divisions are performed.
[code]
int rval(int a, int b, int& c)
{
c = a % b;
return a / b;
}
int main(int argc, char* argv[])
{
int x = 50, y = 7;
int b = 0;
int a = rval(x,y,b);
cout << a << " " << b;
return 0;
}
[/code]

• And ofcourse there is the div-function in ...

: [b][red]This message was edited by stober at 2006-2-28 10:41:17[/red][/b][hr]
: if you set your compiler to produce assembly code output you will find that VC++ 6.0 compiled for Release mode optimizes out both those divisions. But if you put that code in another function, the compiler will not optimize it out -- both divisions are performed.
: [code]
: int rval(int a, int b, int& c)
: {
: c = a % b;
: return a / b;
: }
: int main(int argc, char* argv[])
: {
: int x = 50, y = 7;
: int b = 0;
: int a = rval(x,y,b);
: cout << a << " " << b;
: return 0;
: }
: [/code]
:
:
:
:
:

Greets,
Eric Goldstein
www.gvh-maatwerk.nl

• : And ofcourse there is the div-function in ...
:

That function does the same thing, so it saves nothing at all -- actually it is more time consuming because it has to populate a structure with the results.
[code]
div_t __cdecl div (
int numer,
int denom
)
{
div_t result;

result.quot = numer / denom;
result.rem = numer % denom;

if (numer < 0 && result.rem > 0) {
/* did division wrong; must fix up */
++result.quot;
result.rem -= denom;
}

return result;
}
[/code]