Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   was: "mod operator for signed integers" (http://www.velocityreviews.com/forums/t746893-was-mod-operator-for-signed-integers.html)

dr.oktopus 04-16-2011 11:22 AM

was: "mod operator for signed integers"
 
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 04-16-2011 11:44 AM

Re: was: "mod operator for signed integers"
 
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?


Why add `n'?

--
Eric Sosman
esosman@ieee-dot-org.invalid

dr.oktopus 04-16-2011 01:41 PM

Re: was: "mod operator for signed integers"
 
>
> * * *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 04-16-2011 01:46 PM

Re: was: "mod operator for signed integers"
 
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 04-16-2011 02:12 PM

Re: was: "mod operator for signed integers"
 
"dr.oktopus" <blindwilly@freeonline.zzn.com> 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 04-16-2011 02:12 PM

Re: was: "mod operator for signed integers"
 
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
esosman@ieee-dot-org.invalid

dr.oktopus 04-16-2011 04:27 PM

Re: was: "mod operator for signed integers"
 
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 04-16-2011 04:28 PM

Re: was: "mod operator for signed integers"
 
> It's not guaranteed to work portably.

Why?

Tim Rentsch 04-16-2011 05:28 PM

Re: was: "mod operator for signed integers"
 
"dr.oktopus" <blindwilly@freeonline.zzn.com> 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 04-16-2011 08:09 PM

Re: was: "mod operator for signed integers"
 
"dr.oktopus" <blindwilly@freeonline.zzn.com> 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
possible to tell what you're asking about.

Please include enough context from parent articles so your followup
makes sense on its own (but trim any quoted text that you're not
actually replying to).

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) kst-u@mib.org <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"


All times are GMT. The time now is 01:13 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.