was: "mod operator for signed integers"

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

  1. dr.oktopus

    dr.oktopus Guest

    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
    #1
    1. Advertising

  2. dr.oktopus

    Eric Sosman Guest

    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
    d
     
    Eric Sosman, Apr 16, 2011
    #2
    1. Advertising

  3. dr.oktopus

    dr.oktopus Guest

    >
    >      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, Apr 16, 2011
    #3
  4. dr.oktopus

    dr.oktopus Guest

    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
    #4
  5. dr.oktopus

    Tim Rentsch Guest

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

    Eric Sosman Guest

    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
    #6
  7. dr.oktopus

    dr.oktopus Guest

    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
    #7
  8. dr.oktopus

    dr.oktopus Guest

    > It's not guaranteed to work portably.

    Why?
     
    dr.oktopus, Apr 16, 2011
    #8
  9. dr.oktopus

    Tim Rentsch Guest

    "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
    #9
  10. "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
    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) <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
    #10
  11. dr.oktopus

    James Kuyper Guest

    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
    #11
  12. dr.oktopus

    dr.oktopus Guest

    >
    > 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?" :)
     
    dr.oktopus, Apr 17, 2011
    #12
  13. dr.oktopus

    Tim Rentsch Guest

    "dr.oktopus" <> writes:

    >>
    >> 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?" :)


    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
    #13
  14. dr.oktopus

    James Kuyper Guest

    On 04/17/2011 01:36 PM, dr.oktopus wrote:
    >>
    >> 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.


    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.
    --
    James Kuyper
     
    James Kuyper, Apr 17, 2011
    #14
  15. dr.oktopus wrote:
    >> 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.


    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.
     
    J. J. Farrell, Apr 18, 2011
    #15
  16. 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
    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.

    --
    Morris Keesan --
     
    Morris Keesan, Apr 18, 2011
    #16
  17. dr.oktopus

    dr.oktopus Guest

    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
    #17
  18. dr.oktopus

    Ian Collins Guest

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


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

    --
    Ian Collins
     
    Ian Collins, Apr 18, 2011
    #18
  19. dr.oktopus

    James Kuyper Guest

    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
    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.
    --
    James Kuyper
     
    James Kuyper, Apr 18, 2011
    #19
  20. "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?

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

    --
    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
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dr.oktopus

    mod operator for signed integers

    dr.oktopus, Apr 9, 2011, in forum: C Programming
    Replies:
    12
    Views:
    457
    Eric Sosman
    Apr 10, 2011
  2. Luca Forlizzi

    was: "mod operator for signed integers"

    Luca Forlizzi, Jun 25, 2011, in forum: C Programming
    Replies:
    5
    Views:
    396
    Tim Rentsch
    Jul 6, 2011
Loading...

Share This Page