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


Groups > comp.lang.c > #85373

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-30 12:03 -0700
Organization None to speak of
Message-ID <lnvb43yei8.fsf@kst-u.example.com> (permalink)
References (2 earlier) <c7f1b100-3a36-4b3b-bfb1-72153c3d55de@googlegroups.com> <bpljfbdp362mgeamf62mqktdn8jqr3o2pq@4ax.com> <lny4911dn6.fsf@kst-u.example.com> <ln8u1113ov.fsf@kst-u.example.com> <5caca6ab-a075-4462-b0e1-ed74057955a5@googlegroups.com>

Show all headers | View raw


supercat@casperkitty.com writes:
> On Tuesday, March 29, 2016 at 2:31:38 PM UTC-5, Keith Thompson wrote:
>> C99 changed the rules for integer division, requiring truncation toward
>> zero.
>
> I sometimes get confused with some of the differences between K&R2 and the
> C89/C90 Standard.

In principle, there is no difference; K&R2 describes the language
defined by the 1989 ANSI C standard (which is the same language
described by the 1990 ISO C standard).  K&R2's description is less
rigorous.

What differences to you see?

>                    I find it curious that C90 chose to loosen things compared
> to K&R2 but C99 chose to restore things to the pre-C90 state of affairs.

I don't believe that's accurate -- nor, as far as I know, is the C99
behavior the same as "the pre-C90 state of affairs".

> I also find it interesting that a behavior which was chosen to make
> things simplest for ancient hardware is actually more expensive in
> many cases on modern hardware than would be Euclidian division/modulus
> (in a lot of hardware, division by most constants could be handled by
> doing a multiply- by-constant and ignoring the lower word, but the
> required signed-division hardware needs an extra step to correct
> negative results).

The C99 rationale says:

    In C89, division of integers involving negative operands could
    round upward or downward in an implementation-defined manner; the
    intent was to avoid incurring overhead in run-time code to check
    for special cases and enforce specific behavior. In Fortran,
    however, the result will always truncate toward zero, and the
    overhead seems to be acceptable to the numeric programming
    community. Therefore, C99 now requires similar behavior, which
    should facilitate porting of code from Fortran to C. The table in
    ยง7.20.6.2 of this document illustrates the required semantics.

C89/C90 has the div() and ldiv() functions (C99 added lldiv())
which specify the truncate toward zero behavior when the "/" and
"%" operators didn't specify it.  With C99's change to specify
the behavior of "/" and "%" more precisely, those functions are
not as useful, but they can still be used to get both results in
one operation.

(Personally, I find a definition where, for example, -7 % 5 yields
3 rather than -2 to make more mathematical sense, but apparently
the Fortran numeric programming community doesn't share my opinion.)

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