x % y = ( x -(x/y) * y)

S

sophia

Dear all,

In C, x % y = ( x -(x/y) * y); is n't it ...???? where x and y are
integers and y not equal to zero

is there any other identity involving % operator..??? or any other
binary operator..??

for example

w % n = w & (n-1); when n is a power of 2.
 
S

santosh

sophia said:
Dear all,

In C, x % y = ( x -(x/y) * y); is n't it ...???? where x and y are
integers and y not equal to zero

No.

Try x = 5 and y = 3. Straightaway x/y will give you 0.

<snip>
 
S

sophia

No.

Try x = 5 and y = 3. Straightaway x/y will give you 0.

see this
#include<stdio.h>
#include<stdlib.h>

int main(void)
{
int x,y;
x=5,y=3;

printf("\n%d", ( x -(x/y) * y));
printf("\n%d", x%y);

return EXIT_SUCCESS;
}
 
S

santosh

sophia said:
see this
#include<stdio.h>
#include<stdlib.h>

int main(void)
{
int x,y;
x=5,y=3;

printf("\n%d", ( x -(x/y) * y));
printf("\n%d", x%y);

return EXIT_SUCCESS;
}

Yes, my response was incorrect. I'll leave it to the others to answer
your original query since it's meaning is rather unclear to me.
 
B

Barry Schwarz

Dear all,

In C, x % y = ( x -(x/y) * y); is n't it ...???? where x and y are
integers and y not equal to zero

is there any other identity involving % operator..??? or any other
binary operator..??

for example

w % n = w & (n-1); when n is a power of 2.

If your instructor is spending this much time on esoteric
equivalences, you need to find a new one. If you are doing this
yourself, your time would be much better spent on mastering the
fundamental concepts of the language before you go looking for "cute
tricks".


Remove del for email
 
A

ArWeGod

sophia said:
Dear all,

In C, x % y = ( x -(x/y) * y); is n't it ...???? where x and y are
integers and y not equal to zero

WTF? x%y does not always equal zero. What use woudl that be?!
 
B

Ben Bacarisse

sophia said:
In C, x % y = ( x -(x/y) * y); is n't it ...???? where x and y are
integers and y not equal to zero

Yes, but you have to add also that x/y is representable. When x =
INT_MIN and y = -1, x/y may not be representable.
is there any other identity involving % operator..??? or any other
binary operator..??

for example

w % n = w & (n-1); when n is a power of 2.

Again you have to add extra conditions. It requires n > 1 and w >= 0.
 
B

Ben Bacarisse

ArWeGod said:
WTF? x%y does not always equal zero. What use woudl that be?!

The expression given is a C arithmetic expression. It is not intended
to be read as mathematics.
 
N

Nick Keighley

The expression given is a C arithmetic expression.  It is not intended
to be read as mathematics.

I'd have said exactly the opposite. It is not a C expression
(because x%y does not make sense on the right hand side of
an assignment), but it *is* a valid mathematical expression
(given a suitable definition of /). I've occaisionally
found it useful in languages that don't provide a remainder
operator.

--
Nick Keighley

A lot of the c.l.c. verbiage seems to be devoted to the numerical
density of cavorting nubile seraphim upon pinheads.
CBFalconer
 
C

CBFalconer

Nick said:
.... snip ...

--
Nick Keighley

A lot of the c.l.c. verbiage seems to be devoted to the numerical
density of cavorting nubile seraphim upon pinheads.
CBFalconer

Did I ever say that? I don't remember it. Seems accurate, though.
 
N

Nick Keighley

Nick Keighleywrote:

Did I ever say that?  I don't remember it.  Seems accurate, though.

It was from some time ago, but I *believe* I correctly attributed it.

<google>

yep, 2001-03-02
subject: "Sleep command for noninteger time"
 
C

CBFalconer

Nick said:
It was from some time ago, but I *believe* I correctly attributed it.

<google>

yep, 2001-03-02
subject: "Sleep command for noninteger time"

Oh my. Only 7 years ago, and what a difference. My wife,
brother-in-law, sister-in-law, cat, dog were all alive, I was not
fully retired, hadn't had 3 serious operations, and Bush hadn't had
a chance to do serious harm.
 
A

ArWeGod

CBFalconer said:
Oh my. Only 7 years ago, and what a difference. My wife,
brother-in-law, sister-in-law, cat, dog were all alive, I was not
fully retired, hadn't had 3 serious operations, and Bush hadn't had
a chance to do serious harm.

Define Bush.
;)
 
A

ArWeGod

Ben Bacarisse said:
The expression given is a C arithmetic expression. It is not intended
to be read as mathematics.

Okay, I don't see how something arithmetic can avoid being mathematics, but
let's push forward.

All I see is X divided by Y, times Y (i.e. X, for those following along)
subtracted from itself to acheive ZERO in 100% of the cases. So I guess I
don't see how this works...

Let's do the math (or arithmetic):

10 % 10 = 10 - ( (10/10, ie 1) * 10 (ie: back to 10)) = ZERO!!! Yay!
2 % 10 = 2 minus (2 divided by 10 times 10) = ?
Anyone.... Anyone...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,142
Latest member
DewittMill
Top