[Python-Dev] Floor division

Nick Maclaren nmm1 at cus.cam.ac.uk
Fri Jan 26 11:26:18 CET 2007

"Tim Peters" <tim.peters at gmail.com> wrote:
> [Tim (misattributed to Guido)]

Apologies to both!

> > C90 is unclear about its intent,
> But am skeptical of that.  I don't have a copy of C90 here, but before
> I wrote that I checked Kernighan & Ritchie's seminal C book, Harbison
> & Steele's generally excellent "C: A Reference Manual" (2nd ed), and a
> web version of Plauger & Brodie's "Standard C":
>      http://www-ccs.ucsd.edu/c/
> They all agree that the Cs they describe (all of which predate C99)
> convert floating to integral types via truncation, when possible.

I do.  Kernighan & Ritchie's seminal C book describes the Unix style
of "K&R" C - one of the reasons that ANSI/ISO had to make incompatible
changes was that many important PC and embedded Cs differed.  Harbison
and Steele is generally reliable, but not always; I haven't looked at
the last, but I would regard it suspiciously.

What C90 says is:

    When a value of floating type is converted to integer type, the
    fractional part is discarded.

There is other wording, but none relevant to this issue.  Now, given
the history of floating-point remainder, that is seriously ambiguous.

> > but C99 is specific that truncation is towards zero.
> As opposed to what?  Truncation away from zero?  I read "truncation"
> as implying toward 0, although the Plauger & Brodie source is explicit
> about "the integer part of X, truncated toward zero" for the sake of
> logic choppers ;-)

Towards -infinity, of course.  That was as common as truncation towards
zero up until the 1980s.  It was near-universal on twos complement
floating-point systems, and not rare on signed magnitude ones.  During
the standardisation of C90, the BSI tried to explain to ANSI that this
needed spelling out, but were ignored.  C99 did not add the normative
text "(i.e., the value is truncated toward zero)" because there was
no ambiguity, after all!

Nick Maclaren,
University of Cambridge Computing Service,
New Museums Site, Pembroke Street, Cambridge CB2 3QH, England.
Email:  nmm1 at cam.ac.uk
Tel.:  +44 1223 334761    Fax:  +44 1223 334679

More information about the Python-Dev mailing list