Velocity Reviews > was: "mod operator for signed integers"

was: "mod operator for signed integers"

dr.oktopus
Guest
Posts: n/a

 04-16-2011
Hello,
I think I found a proper solution for the problem I post a few days
ago.
Here it is:

unsigned mymod (int m, unsigned n)
{
if (m >= 0)
return m % n;
else {
m += INT_MAX;
return (m + n - INT_MAX % n) % n;
}
}

What do you think about it?

Eric Sosman
Guest
Posts: n/a

 04-16-2011
On 4/16/2011 7:22 AM, dr.oktopus wrote:
> Hello,
> I think I found a proper solution for the problem I post a few days
> ago.
> Here it is:
>
> unsigned mymod (int m, unsigned n)
> {
> if (m>= 0)
> return m % n;
> else {
> m += INT_MAX;
> return (m + n - INT_MAX % n) % n;
> }
> }
>
> What do you think about it?

--
Eric Sosman
http://www.velocityreviews.com/forums/(E-Mail Removed)d

dr.oktopus
Guest
Posts: n/a

 04-16-2011
>
> * * *Why add `n'?
>

Mmmhh, I found it through successive steps.
Now I will try without it and see if it works for all values.

dr.oktopus
Guest
Posts: n/a

 04-16-2011
At a first glance, adding INT_MAX to m translate
the interval INT_MIN .. -1 (extremes included) to
-1 .. INT_MAX - 1 (extrems included)

So m - INT_MAX % n could be < 0 without add n.

Tim Rentsch
Guest
Posts: n/a

 04-16-2011
"dr.oktopus" <(E-Mail Removed)> writes:

> Hello,
> I think I found a proper solution for the problem I post a few days
> ago.
> Here it is:
>
> unsigned mymod (int m, unsigned n)
> {
> if (m >= 0)
> return m % n;
> else {
> m += INT_MAX;
> return (m + n - INT_MAX % n) % n;
> }
> }
>
> What do you think about it?

It's not guaranteed to work portably. It also has a needless
dependence on <limits.h>, specifically INT_MAX.

The solution I posted earlier:

return m < 0 ? n-1 - -(m+1)%n : m%n;

does not need <limits.h>, and gives results that
are portably correct for all values of m and n
with n>0.

Eric Sosman
Guest
Posts: n/a

 04-16-2011
On 4/16/2011 9:46 AM, dr.oktopus wrote:
> At a first glance, adding INT_MAX to m translate
> the interval INT_MIN .. -1 (extremes included) to
> -1 .. INT_MAX - 1 (extrems included)
>
> So m - INT_MAX % n could be< 0 without add n.

No, because n is unsigned. The expression is

int - int % unsigned

.... which promotes to

int - unsigned % unsigned

..... which is

int - unsigned

.... which promotes to

unsigned - unsigned

.... which is

unsigned

.... which cannot be less than zero.

--
Eric Sosman
(E-Mail Removed)d

dr.oktopus
Guest
Posts: n/a

 04-16-2011
Remove n from the addition in my tests gives
me wrong values for m near INT_MIN (or better,
since I did my tests with chars, for values
near SCHAR_MIN I get mymod returns values near UCHAR_MAX, 255)

?!?!

dr.oktopus
Guest
Posts: n/a

 04-16-2011
> It's not guaranteed to work portably.

Why?

Tim Rentsch
Guest
Posts: n/a

 04-16-2011
"dr.oktopus" <(E-Mail Removed)> writes:

>> It's not guaranteed to work portably.

>
> Why?

If we have 'int m;' and 'unsigned n;', with m in [ INT_MIN .. -1 ]
and n in [ 1 .. UINT_MAX ], consider the expression

(m + INT_MAX) + n

Question 1: What is the maximum value of this expression,
considered as a mathematical expression?

Question 2: Considered as a C expression, what is the
type of the expression? (Hint: there is more than one
possibility.)

Question 3: Have you tried working through the solution
that was posted, to see how it works?

Keith Thompson
Guest
Posts: n/a

 04-16-2011
"dr.oktopus" <(E-Mail Removed)> writes:
>> It's not guaranteed to work portably.

>
> Why?

Why what?

Looking just at your followup, and not at the article you were
replying to (which is what I and many others are doing), it's not

makes sense on its own (but trim any quoted text that you're not

The new Google Groups interface makes this gratuitously difficult;
it also generates articles with missing headers. There should be
an option to go back to the older interface -- or, better yet,
get an actual new client (Mozilla Thunderbird is one of many
examples) and an account with one of the free NNTP servers (I use
eternal-september.org).

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"