# was: "mod operator for signed integers"

Discussion in 'C Programming' started by dr.oktopus, Apr 16, 2011.

1. ### dr.oktopusGuest

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?

dr.oktopus, Apr 16, 2011

2. ### Eric SosmanGuest

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
d

Eric Sosman, Apr 16, 2011

3. ### dr.oktopusGuest

>
>

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

dr.oktopus, Apr 16, 2011
4. ### dr.oktopusGuest

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.

dr.oktopus, Apr 16, 2011
5. ### Tim RentschGuest

"dr.oktopus" <> 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.

Tim Rentsch, Apr 16, 2011
6. ### Eric SosmanGuest

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
d

Eric Sosman, Apr 16, 2011
7. ### dr.oktopusGuest

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, Apr 16, 2011
8. ### dr.oktopusGuest

> It's not guaranteed to work portably.

Why?

dr.oktopus, Apr 16, 2011
9. ### Tim RentschGuest

"dr.oktopus" <> 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?

Tim Rentsch, Apr 16, 2011
10. ### Keith ThompsonGuest

"dr.oktopus" <> 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) <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"

Keith Thompson, Apr 16, 2011
11. ### James KuyperGuest

On 04/16/2011 04:09 PM, Keith Thompson wrote:
....
> 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

There is - sort of. However, they've changed an important feature of the
old interface. When I look at newsgroup, the threads are sorted by the
date of the first message on the thread, rather than the most recent
message. I have no idea why they changed this - it's not the way the old
interface used to work, and it's not the way the new one works.
--
James Kuyper

James Kuyper, Apr 16, 2011
12. ### dr.oktopusGuest

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

I included enough of Tim Rentsch post.
He said: "It's not guaranteed to work portably.", referring at my
code,
I think it is enough clear, saying more would be saying:
"Why it's not guaranteed to work portably?"

dr.oktopus, Apr 17, 2011
13. ### Tim RentschGuest

"dr.oktopus" <> writes:

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

>
> I included enough of Tim Rentsch post.
> He said: "It's not guaranteed to work portably.", referring at my
> code,
> I think it is enough clear, saying more would be saying:
> "Why it's not guaranteed to work portably?"

What you didn't include was the "it", the code (quoted in my
response) of which I said it is not guaranteed to work
portably. I'm with Keith on this one. Your response should
also include an attribution line showing whose posting you
are responding to (like the "dr.oktopus" line in this message).

Tim Rentsch, Apr 17, 2011
14. ### James KuyperGuest

On 04/17/2011 01:36 PM, dr.oktopus wrote:
>>
>> makes sense on its own (but trim any quoted text that you're not
>>

>
> I included enough of Tim Rentsch post.

No, you did not. In particular, your message included nothing to
indicate that it was responding to a post from Tim Rentsch.

> He said: "It's not guaranteed to work portably.", referring at my
> code,

How are we supposed to know the Tim was referring to your code? You
didn't quote enough from Tim's message to make that clear.

At a minimum, your message should have quoted enough of Tim's message to
simultaneously, have some idea whether or not Tim had a reasonable
objection to your code, and have some idea whether you were justified in
questioning Tim's objections.

> I think it is enough clear, saying more would be saying:
> "Why it's not guaranteed to work portably?"

That's not really saying more, just using more words to say the same
thing. Saying more would include letting us know that you were
responding to a message from Tim, and would also let us know what it was
that Tim was objecting to.
--
James Kuyper

James Kuyper, Apr 17, 2011
15. ### J. J. FarrellGuest

dr.oktopus wrote:
>> makes sense on its own (but trim any quoted text that you're not

>
> I included enough of Tim Rentsch post.

No you didn't, nowhere near enough.

> He said: "It's not guaranteed to work portably.", referring at my
> code,

How am I supposed to know it was referring to some code rather than a
lawn mower? I might reasonably guess we're talking about code, but what
code?

> I think it is enough clear, saying more would be saying:
> "Why it's not guaranteed to work portably?"

The "why" was clear, the "what on earth are you talking about" was
entirely absent.

J. J. Farrell, Apr 18, 2011
16. ### Morris KeesanGuest

On Sat, 16 Apr 2011 09:41:29 -0400, dr.oktopus
<> wrote:

>>
>> Â  Â  Â Why add `n'?
>>

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

You appear to be under the impression that everyone reading this message
is using the same interface that you're using. I don't know what this
looks like to you, but I'm reading it in comp.lang.c, which is a
Usenet newsgroup, not a Google Group, in spite of what Google's interface
may be trying to make you believe. This message of yours has absolutely
zero context. A Usenet user seeing this message by itself (as most of
us do) has no idea what 'n' is, what you're adding it to, or why you're

it in isolation has some clue what you're talking about.

--
Morris Keesan --

Morris Keesan, Apr 18, 2011
17. ### dr.oktopusGuest

I am sorry, I thought that even with google interface, replies
were directed to the message I answer to, building the classic
message tree. In that case, my reply would be sufficient.

dr.oktopus, Apr 18, 2011
18. ### Ian CollinsGuest

On 04/18/11 08:05 PM, dr.oktopus wrote:
> I am sorry, I thought that even with google interface, replies
> were directed to the message I answer to, building the classic
> message tree. In that case, my reply would be sufficient.

--
Ian Collins

Ian Collins, Apr 18, 2011
19. ### James KuyperGuest

On 04/18/2011 04:05 AM, in response to an unidentified message posted by
some unspecified person, dr.oktopus wrote:
> I am sorry, I thought that even with google interface, replies
> were directed to the message I answer to, building the classic
> message tree. In that case, my reply would be sufficient.

One of the known problems with the google interface is that it often
leaves out the headers that permit construction of that tree. However,
that does not appear to be the case with the case with your messages in
this thread. However, you did break that mechanism when you changed the
subject to 'was: "mod operator for signed integers"'. You should have
continued the same discussion, not started a new one.

You're right, though, that most newsreaders provide methods for going
back to previous messages, but it's poor form to write your message in
such a way that it requires use of those facilities to understand what
you're saying.

There are several reasons why this is the case. First of all, not all
newsreaders provide that capability. Secondly, the message you're
replying to might not reach the reader's news server until after your
response; it might never get there. Even if it has already arrived, it
might have been filtered out by the reader's news server, or by the

The third reason seems weakest, but is the most widely applicable, and
as a result, the most important: having to go back to the previous
message is slightly, but unnecessarily, more difficult than getting the
needed information from within the same message. It's not particularly
difficult with most news-reading software to insert a copy of the
message you're replying to, and then trimming that copy down to contain
only the information needed to give suitable context to your response.
Do so.
--
James Kuyper

James Kuyper, Apr 18, 2011
20. ### Keith ThompsonGuest

"dr.oktopus" <> writes:
> I am sorry, I thought that even with google interface, replies
> were directed to the message I answer to, building the classic
> message tree. In that case, my reply would be sufficient.

Even after multiple people have told you that it doesn't work
that way?

We typically see one message at a time. At least the newsreader
I use has a command to jump to the parent of an article, but then
it's inconvenient to get back to the message I was reading.

When I leave this newsgroup and come back to it, I don't even see

Most posters provide enough context in each article, so this usually
isn't a problem. Take a look at a selection of articles that
others have posted here. A few people don't quote enough context,
a few more quote too much context (i.e., the entire parent article
including signatures), but most posters get it right. If you
construct your followups the way most other posters here do, it
will make it a lot easier for your readers. (And if you switch from
Google Groups to a real newsreader, you might find that easier to do.)

--
Keith Thompson (The_Other_Keith) <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"

Keith Thompson, Apr 18, 2011