was: "mod operator for signed integers"

D

dr.oktopus

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

Eric Sosman

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'?
 
D

dr.oktopus

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

Tim Rentsch

dr.oktopus said:
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.
 
E

Eric Sosman

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

dr.oktopus

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)

?!?!
 
T

Tim Rentsch

dr.oktopus said:

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

Keith Thompson

dr.oktopus said:

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

James Kuyper

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

dr.oktopus

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

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

Tim Rentsch

dr.oktopus said:
I included enough of Tim Rentsch post.
He said: "It's not guaranteed to work portably.", referring at my
code,
and I asked "why?".
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).
 
J

James Kuyper

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.
and I asked "why?".

At a minimum, your message should have quoted enough of Tim's message to
include his quotation of your code. That way the reader could,
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.
 
J

J. J. Farrell

dr.oktopus said:
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?
and I asked "why?".
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.
 
M

Morris Keesan

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
asking yourself, "Why add 'n'?" (or if you're not asking yourself a
question and replying to your own question, to whom you're replying).

Please include enough context in EACH MESSAGE so that someone reading
it in isolation has some clue what you're talking about.
 
D

dr.oktopus

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. :)
 
I

Ian Collins

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

Well it isn't. There isn't any context or attributions in your reply.
 
J

James Kuyper

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
reader's spam filters.

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

Keith Thompson

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

Most of us don't read messages using the Google interface.
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
articles that I've already read.

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top