Future division patch available (PEP 238)
tanzer at swing.co.at
Fri Jul 27 04:45:36 EDT 2001
david at boddie.net (David Boddie) wrote:
> tanzer at swing.co.at (Christian Tanzer) wrote in
> message news:<mailman.996052189.14819.python-list at python.org>...
> > What if `/` applied to two integer values returned neither an
> > integer nor a float but an object carrying the float result but
> > behaving like an integer if used in an integer context?
> > For instance:
> > >>> x = 1/2
> > >>> type(x)
> > <type 'Ratio'>
> > >>> print "%d %f %s" % (x, x, x)
> > 0 0.5 0.5
> > >>> 2 * x
> > 0
> > >>> 2. * x
> > 1.0
> > The difficult issue here is how `integer context` is defined.
> > Should multiplication by an integer be considered an integer
> > context? Pro: would preserve correctness of existing code like
> > `(size / 8) * 8`. Con: is incompatible with Rationals which might
> > be added in the future.
> A problem arises if the type of the result (x in this example) is not
> immediately resolved, requiring this context to be carried forward. A
> "neutral" operation immediately following the assignment above, such
> as a print statement (for want of a better example) or even another
> division would further complicate the issue by leaving the
> interpretation of the result until later when the original context is
The original context wouldn't matter. The Ratio object would need to
carriy all information needed to behave properly depending on context.
It has enough other problems to make it an attractive option, though.
Christian Tanzer tanzer at swing.co.at
Glasauergasse 32 Tel: +43 1 876 62 36
A-1130 Vienna, Austria Fax: +43 1 877 66 92
More information about the Python-list