learning33

When I put:

int p = 13;

int r = (p % 10);

into my code it gives me r == 3 which is what I would expect. But when I put:

int p = -3;
int r = (p % 10);

it gives me -3!. And not 3 like I would expect and I want.

I see from the MSDN examples that this is the way it should be. But why In math when you take (-3 % 10) I'm pretty sure you get 3.

Just curious. And looking for a method that transforms negative values into positive. I've tried int q = [r], but that didn't work.


Re: Visual C# Language Unexpected % problems

Matthew Watson

>> And looking for a method that transforms negative values into positive

if (x < 0)
x = -x;

Or is that not what you meant

And yes, the % operator has the wrong name (modulus operator). It should be called "remainder", since that's what it does.

Also see http://en.wikipedia.org/wiki/Modulo_operation






Re: Visual C# Language Unexpected % problems

learning33

Thanks Matthew!




Re: Visual C# Language Unexpected % problems

theTroll527

Or you could use the Math.Abs method.

theTroll