[Python-Dev] Floor division

Nick Maclaren nmm1 at cus.cam.ac.uk
Fri Jan 26 10:58:20 CET 2007


"Tim Peters" <tim.peters at gmail.com> wrote:
>
> It could, but who would have a (sane) use for a possibly 2000-bit quotient?

Well, the 'exact rounding' camp in IEEE 754 seem to think that there
is one :-)

As you can gather, I can't think of one.  Floating-point is an inherently
inaccurate representation for anything other than small integers.

> This is a bit peculiar to me, because there are ways to compute
> "remainder" using a number of operations proportional to the log of
> the exponent difference.  It could be that people who spend their life
> doing floating point forget how to work with integers ;-)

Aargh!  That is indeed the key!  Given that I claim to know something
about integer arithmetic, too, how can I have been so STUPID?  Yes,
you are right, and that is the only plausible way to calculate the
remainder precisely.  You don't get the quotient precisely, which is
what my (insane) specification would have provided.

I would nitpick with your example, because you don't want to reduce
modulo 3.14 but modulo pi and therefore the modular arithmetic is
rather more expensive (given Decimal).  However, it STILL doesn't
help to make remquo useful!

The reason is that pi is input only to the floating-point precision,
and so the result of remquo for very large arguments will depend
more on the inaccuracy of pi as input than on the mathematical
result.  That makes remquo totally useless for the example you quote.

Yes, I have implemented 'precise' range reduction, and there is no
substitute for using an arbitrary precision pi value :-(

> > But it isn't obviously WRONG.
>
> For floats, fmod(x, y) is exactly congruent to x modulo y -- I don't
> think it's possible to get more right than exactly right ;-)

But, as a previous example of yours pointed out, it's NOT exactly
right.  It is also supposed to be in the range [0,y) and it isn't.
-1%1e100 is mathematically wrong on two counts.
1a
Cc: "Tim Peters" <tim.peters at gmail.com>


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