Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #85262

Re: K&R exercise 3-4 handle largest negative number implementation

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: K&R exercise 3-4 handle largest negative number implementation
Date 2016-03-29 12:31 -0700
Organization None to speak of
Message-ID <ln8u1113ov.fsf@kst-u.example.com> (permalink)
References <d1bcdf9a-bb8c-4783-ac20-71ff2eb1a5b2@googlegroups.com> <glpifb1eh94iv045hugjqv596t3tn78pcm@4ax.com> <c7f1b100-3a36-4b3b-bfb1-72153c3d55de@googlegroups.com> <bpljfbdp362mgeamf62mqktdn8jqr3o2pq@4ax.com> <lny4911dn6.fsf@kst-u.example.com>

Show all headers | View raw


Keith Thompson <kst-u@mib.org> writes:
> Barry Schwarz <schwarzb@dqel.com> writes:
>> On Mon, 28 Mar 2016 13:11:59 -0700 (PDT), supercat@casperkitty.com
>> wrote:
>>>On Monday, March 28, 2016 at 1:48:35 PM UTC-5, Barry Schwarz wrote:
>>>> I wonder why K&R thought it advisable to work only with positive
>>>> values.  The process of repeatedly using % and / works just as well
>>>> with negative numbers if you use the absolute value of the remainder.
>>>> When you do it that way, INT_MIN presents no special issue.
>>>
>>>K&R didn't specify whether "%" and "/" use Euclidian or truncated
>>>division semantics.  Further, even if they had specified truncated
>>>division semantics,
>>
>> I don't know what you mean by Euclidian division but page 41 of K&R2
>> specifies truncated division.
>
> I'm guessing supercat is referring to K&R1 (or perhaps to Kernighan
> and Ritchie themselves).  I'll check my copy of K&R1 later today.
>
> supercat: If you're referring to Brian Kernighan and Dennis Ritchie,
> I suggest not abbreviating their names to "K&R".  That term almost
> universally refers to the book, not the people.

Here's what K&R1 says about division (section 7.3 in the reference
manual appendix):

    The binary / operator indicates division.  When positive integers
    are divided truncation is toward 0, but the form of truncation is
    machine-dependent if either operand is negative.  On all machines
    covered by this manual, the remainder has the same sign as the
    dividend.  It is always true that (a/b)*b + a%b is equal to a (if b
    is not 0).

    The binary % operator yields the remainder from the division of the
    first expression by the second.  The usual arithmetic conversions
    are performed.  The operands must not be float.

(I presume the omission of double was unintentional.)

K&R2 says:

    [...] If both operands are non-negative, then the remainder is
    non-negative and smaller than the divisor; if not, it is guaranteed
    only that the absolute value of the remainder is smaller than the
    absolute value of the divisor.

C99 changed the rules for integer division, requiring truncation toward
zero.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

Back to comp.lang.c | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

K&R exercise 3-4 handle largest negative number implementation Alla _ <modelling.data@gmail.com> - 2016-03-28 01:34 -0700
  Re: K&R exercise 3-4 handle largest negative number implementation Alla _ <modelling.data@gmail.com> - 2016-03-28 01:36 -0700
  Re: K&R exercise 3-4 handle largest negative number implementation Barry Schwarz <schwarzb@dqel.com> - 2016-03-28 10:08 -0700
    Re: K&R exercise 3-4 handle largest negative number implementation Alla _ <modelling.data@gmail.com> - 2016-03-28 11:55 -0700
    Re: K&R exercise 3-4 handle largest negative number implementation Alla _ <modelling.data@gmail.com> - 2016-03-28 12:08 -0700
  Re: K&R exercise 3-4 handle largest negative number implementation Barry Schwarz <schwarzb@dqel.com> - 2016-03-28 11:37 -0700
    Re: K&R exercise 3-4 handle largest negative number implementation supercat@casperkitty.com - 2016-03-28 13:11 -0700
      Re: K&R exercise 3-4 handle largest negative number implementation Barry Schwarz <schwarzb@dqel.com> - 2016-03-28 18:16 -0700
        Re: K&R exercise 3-4 handle largest negative number implementation Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-03-29 03:17 +0100
          Re: K&R exercise 3-4 handle largest negative number implementation Barry Schwarz <schwarzb@dqel.com> - 2016-03-28 22:10 -0700
            Re: K&R exercise 3-4 handle largest negative number implementation Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-03-29 11:39 +0100
        Re: K&R exercise 3-4 handle largest negative number implementation Keith Thompson <kst-u@mib.org> - 2016-03-29 08:56 -0700
          Re: K&R exercise 3-4 handle largest negative number implementation Keith Thompson <kst-u@mib.org> - 2016-03-29 12:31 -0700
            Re: K&R exercise 3-4 handle largest negative number implementation supercat@casperkitty.com - 2016-03-30 09:54 -0700
              Re: K&R exercise 3-4 handle largest negative number implementation Keith Thompson <kst-u@mib.org> - 2016-03-30 12:03 -0700
                Re: K&R exercise 3-4 handle largest negative number implementation supercat@casperkitty.com - 2016-03-30 14:00 -0700
    Re: K&R exercise 3-4 handle largest negative number implementation Alla _ <modelling.data@gmail.com> - 2016-03-30 03:41 -0700
      Re: K&R exercise 3-4 handle largest negative number implementation Barry Schwarz <schwarzb@dqel.com> - 2016-03-30 10:26 -0700
        Re: K&R exercise 3-4 handle largest negative number implementation Alla _ <modelling.data@gmail.com> - 2016-04-01 04:41 -0700
          Re: K&R exercise 3-4 handle largest negative number implementation Richard Heathfield <rjh@cpax.org.uk> - 2016-04-01 13:33 +0100
          Re: K&R exercise 3-4 handle largest negative number implementation Barry Schwarz <schwarzb@dqel.com> - 2016-04-01 09:23 -0700
        Re: K&R exercise 3-4 handle largest negative number implementation Alla _ <modelling.data@gmail.com> - 2016-04-01 04:53 -0700
          Re: K&R exercise 3-4 handle largest negative number implementation Barry Schwarz <schwarzb@dqel.com> - 2016-04-01 09:10 -0700
            Re: K&R exercise 3-4 handle largest negative number implementation Alla _ <modelling.data@gmail.com> - 2016-04-02 00:41 -0700
              Re: K&R exercise 3-4 handle largest negative number implementation Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-04-02 11:07 +0100
        Re: K&R exercise 3-4 handle largest negative number implementation Alla _ <modelling.data@gmail.com> - 2016-04-02 01:03 -0700
    Re: K&R exercise 3-4 handle largest negative number implementation Tim Rentsch <txr@alumni.caltech.edu> - 2016-03-30 10:05 -0700
      Re: K&R exercise 3-4 handle largest negative number implementation supercat@casperkitty.com - 2016-03-30 10:15 -0700
        Re: K&R exercise 3-4 handle largest negative number implementation Tim Rentsch <txr@alumni.caltech.edu> - 2016-04-05 12:25 -0700
          Re: K&R exercise 3-4 handle largest negative number implementation supercat@casperkitty.com - 2016-04-05 14:09 -0700
            Re: K&R exercise 3-4 handle largest negative number implementation asetofsymbols@gmail.com - 2016-04-05 22:49 -0700
              Re: K&R exercise 3-4 handle largest negative number implementation Richard Heathfield <rjh@cpax.org.uk> - 2016-04-06 07:16 +0100
                Re: K&R exercise 3-4 handle largest negative number implementation BartC <bc@freeuk.com> - 2016-04-06 11:24 +0100
                Re: K&R exercise 3-4 handle largest negative number implementation asetofsymbols@gmail.com - 2016-04-06 10:27 -0700
                Re: K&R exercise 3-4 handle largest negative number implementation Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-04-06 11:46 +0100
                Re: K&R exercise 3-4 handle largest negative number implementation Richard Heathfield <rjh@cpax.org.uk> - 2016-04-06 12:48 +0100
                Re: K&R exercise 3-4 handle largest negative number implementation supercat@casperkitty.com - 2016-04-06 08:36 -0700
                Re: K&R exercise 3-4 handle largest negative number implementation supercat@casperkitty.com - 2016-04-06 07:59 -0700
            Re: K&R exercise 3-4 handle largest negative number implementation Tim Rentsch <txr@alumni.caltech.edu> - 2016-05-05 11:51 -0700
  Re: K&R exercise 3-4 handle largest negative number implementation programming109@gmail.com - 2016-04-11 07:25 -0700
    Re: K&R exercise 3-4 handle largest negative number implementation Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-04-11 19:38 +0100
      Re: K&R exercise 3-4 handle largest negative number implementation programming109@gmail.com - 2016-04-11 12:14 -0700
        Re: K&R exercise 3-4 handle largest negative number implementation Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-04-11 20:32 +0100

csiph-web