PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.c > x % y = ( x -(x/y) * y)
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
x % y = ( x -(x/y) * y)

Réponse
 
LinkBack Outils de la discussion
Vieux 25/04/2008, 10h30   #1
sophia
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut x % y = ( x -(x/y) * y)

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.

  Réponse avec citation
Vieux 25/04/2008, 11h06   #2
santosh
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: x % y = ( x -(x/y) * y)

sophia wrote:

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

  Réponse avec citation
Vieux 25/04/2008, 11h10   #3
santosh
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: x % y = ( x -(x/y) * y)

santosh wrote:

> sophia wrote:
>
>> 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>


Sorry to the OP. This is wrong. Your equation is indeed correct.

  Réponse avec citation
Vieux 25/04/2008, 11h12   #4
sophia
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: x % y = ( x -(x/y) * y)

On Apr 25, 2:06pm, santosh <santosh....@gmail.com> wrote:
> sophia wrote:
> > 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.


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;
}



  Réponse avec citation
Vieux 25/04/2008, 11h16   #5
santosh
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: x % y = ( x -(x/y) * y)

sophia wrote:

> On Apr 25, 2:06pm, santosh <santosh....@gmail.com> wrote:
>> sophia wrote:
>> > 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.

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

  Réponse avec citation
Vieux 25/04/2008, 11h27   #6
Eugeny Myunster
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: x % y = ( x -(x/y) * y)

There are one more interesting fact for integers:

x%y==0 <==> (x/y)*y==x


sophia wrote:
> 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.
>

  Réponse avec citation
Vieux 25/04/2008, 11h42   #7
CBFalconer
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: x % y = ( x -(x/y) * y)

santosh wrote:
> sophia wrote:
>
>> 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.


Huh? I find x/y yields 1.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.

** Posted from http://www.teranews.com **
  Réponse avec citation
Vieux 25/04/2008, 13h44   #8
Barry Schwarz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: x % y = ( x -(x/y) * y)

On Fri, 25 Apr 2008 01:30:05 -0700 (PDT), sophia
<sophia.agnes@gmail.com> wrote:

>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
  Réponse avec citation
Vieux 25/04/2008, 13h56   #9
Martien Verbruggen
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: x % y = ( x -(x/y) * y)

On Fri, 25 Apr 2008 12:27:18 +0300,
Eugeny Myunster <box@eugeny.ws> wrote:
> sophia wrote:
>> 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


> There are one more interesting fact for integers:
>
> x%y==0 <==> (x/y)*y==x


To me, that seems to be the same 'fact'.

Martien
--
|
Martien Verbruggen | We are born naked, wet and hungry. Then
| things get worse.
|
  Réponse avec citation
Vieux 26/04/2008, 04h32   #10
pete
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: x % y = ( x -(x/y) * y)

sophia wrote:
> 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.


int n_is_Power_of_two(long unsigned n)
{
return (n & n - 1) == 0 && n != 0;
}

int n_is_Power_of_four(long unsigned n)
{
return (n & n - 1) == 0 && n % 3 == 1;
}

int n_is_Power_of_eight(long unsigned n)
{
return (n & n - 1) == 0 && n % 7 == 1;
}

--
pete
  Réponse avec citation
Vieux 27/07/2008, 13h55   #11
ArWeGod
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: x % y = ( x -(x/y) * y)

"sophia" <sophia.agnes@gmail.com> wrote in message
news:79d97bf1-4071-4637-934e-21428324a1b0@y22g2000prd.googlegroups.com...
> 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?!


  Réponse avec citation
Vieux 27/07/2008, 16h28   #12
Ben Bacarisse
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: x % y = ( x -(x/y) * y)

sophia <sophia.agnes@gmail.com> writes:

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

--
Ben.
  Réponse avec citation
Vieux 27/07/2008, 16h29   #13
Ben Bacarisse
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: x % y = ( x -(x/y) * y)

"ArWeGod" <ArWeGod?@sbcglobal.net> writes:

> "sophia" <sophia.agnes@gmail.com> wrote in message
> news:79d97bf1-4071-4637-934e-21428324a1b0@y22g2000prd.googlegroups.com...
>> 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?!


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

--
Ben.
  Réponse avec citation
Vieux 28/07/2008, 10h38   #14
Nick Keighley
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: x % y = ( x -(x/y) * y)

On 27 Jul, 15:29, Ben Bacarisse <ben.use...@bsb.me.uk> wrote:
> "ArWeGod" <ArWeG...@sbcglobal.net> writes:
> > "sophia" <sophia.ag...@gmail.com> wrote in message
> >news:79d97bf1-4071-4637-934e-21428324a1b0@y22g2000prd.googlegroups.com....


> >> 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?!

>
> 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
  Réponse avec citation
Vieux 28/07/2008, 21h25   #15
CBFalconer
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: x % y = ( x -(x/y) * y)

Nick Keighley wrote:
>

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

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.

  Réponse avec citation
Vieux 29/07/2008, 09h45   #16
Nick Keighley
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: x % y = ( x -(x/y) * y)

On 28 Jul, 20:25, CBFalconer <cbfalco...@yahoo.com> wrote:
> Nick Keighleywrote:


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


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

<google>

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

--
Nick Keighley

"Resistance is futile. Read the C-faq."
-- James Hu (c.l.c.)
  Réponse avec citation
Vieux 29/07/2008, 21h44   #17
CBFalconer
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: x % y = ( x -(x/y) * y)

Nick Keighley wrote:
> CBFalconer <cbfalco...@yahoo.com> wrote:
>> Nick Keighleywrote:

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

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

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.

  Réponse avec citation
Vieux 30/07/2008, 08h47   #18
ArWeGod
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: x % y = ( x -(x/y) * y)

"CBFalconer" <cbfalconer@yahoo.com> wrote in message
news:488F732B.2D78A640@yahoo.com...
> Nick Keighley wrote:
> > CBFalconer <cbfalco...@yahoo.com> wrote:
> >> Nick Keighleywrote:

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

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


Define Bush.



  Réponse avec citation
Vieux 30/07/2008, 09h07   #19
ArWeGod
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: x % y = ( x -(x/y) * y)

"Ben Bacarisse" <ben.usenet@bsb.me.uk> wrote in message
news:87ej5f764i.fsf@bsb.me.uk...
> "ArWeGod" <ArWeGod?@sbcglobal.net> writes:
>
> > "sophia" <sophia.agnes@gmail.com> wrote in message
> >

news:79d97bf1-4071-4637-934e-21428324a1b0@y22g2000prd.googlegroups.com...
> >> 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?!

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

--
ArWeConfused


  Réponse avec citation
Vieux 30/07/2008, 09h18   #20
Gordon Burditt
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: x % y = ( x -(x/y) * y)

>> >> 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?!

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


The / in that expression is INTEGER DIVISION. An integer divided
by an integer yields an integer result.

>All I see is X divided by Y, times Y
>(i.e. X, for those following along)


That's true if you are doing your mathematics in reals, but not if
you're doing it in integers.

>subtracted from itself to acheive ZERO in 100% of the cases. So I guess I
>don't see how this works...


15/10 in C is 1, not 1.5, since the result of dividing two integers
is an integer.

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


2/10 is zero.

  Réponse avec citation
Vieux 30/07/2008, 11h22   #21
ArWeGod
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: x % y = ( x -(x/y) * y)

> 2/10 is zero.

Oh. Right. Carry on.

--
ArWeWrong/ArWeNew=1 (cos it's TRUE!)


  Réponse avec citation
Vieux 30/07/2008, 13h28   #22
vippstar@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: x % y = ( x -(x/y) * y)

On Jul 30, 10:18 am, gordonb.ed...@burditt.org (Gordon Burditt) wrote:
<snip>
> 2/10 is zero.


Please don't remove attributions.
  Réponse avec citation
Vieux 30/07/2008, 13h28   #23
vippstar@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: x % y = ( x -(x/y) * y)

On Jul 30, 12:22 pm, "ArWeGod" <ArWeG...@sbcglobal.net> wrote:
> > 2/10 is zero.

>
> Oh. Right. Carry on.


Please don't remove attributions.
  Réponse avec citation
Vieux 30/07/2008, 17h34   #24
soscpd
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: x % y = ( x -(x/y) * y)

Hello Sophia, list

On Apr 25, 4:30 am, sophia <sophia.ag...@gmail.com> wrote:

http://en.wikipedia.org/wiki/Operato...etic_operators
http://en.wikipedia.org/wiki/Modulo_operation

Regards
Ccello

By the way... CBFalconer, sorry about your wife, brother-in-law,
sister-in-law, cat and dog.
  Réponse avec citation
Vieux 31/07/2008, 02h13   #25
CBFalconer
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: x % y = ( x -(x/y) * y)

ArWeGod wrote:
>

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


Using the usual integer arithmetic, 2 / 10 is unequivicably zero.
After which zero times 10 remains zero, and that is the value of
the expression.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.


  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 23h52.


Édité par : vBulletin®
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,26180 seconds with 33 queries