PEP0238 lament

Steve Horne sh at ttsoftware.co.uk
Mon Jul 23 10:08:14 EDT 2001


On 23 Jul 2001 10:48:44 GMT, m.faassen at vet.uu.nl (Martijn Faassen)
wrote:

>Moshe Zadka <moshez at zadka.site.co.il> wrote:
>> On Sun, 22 Jul 2001 16:27:32 -0700, David Eppstein <eppstein at ics.uci.edu> wrote:
>>> My intuition is that the users who will be surprised that 1/2 = 0 are the 
>>> same ones who would be bitten by roundoff-error bugs if 1/2 = 0.5.
>>> Result: buggier code since the errors are less obvious.
>
>> The thing is, making 1/2=0.5 won't make the problem *worse*. Because
>> after two days of being baffled by 1/2=0, these newbies will eventually
>> figure out the solution float(1)/2. So, I'm just getting to show them
>> the buggy roundoff-error code *faster*, instead of making them suffer
>> for it. You want to fix *that* problem, you have to give easy to access
>> rationals.
>
>Will you introduce a special 'rational division' operator for that? 
>Or change the semantics of '/' again? :)

There would certainly be three meanings of division - create a
rational from integers, do integer division *and* do float division.
Unless rationals were made into an independent type, some kind of
change would be necessary to support this.

If we just keep the same old division operator, the obvious thing
would be to define a rational type for which literals could only be
defined using integers or decimal notation, but supporting some
compile-time calculation. For instance...

  Source    Value in Bytecode 
  ------    -----------------
  3R        3/1
  1.5R      15/10
  1R/3      1/3

Simple predictable behaviour, and no-one gets rationals unless they
want them.

Symbolic calculations could be handled just as easily as a new type...

  5S / 3
  k * symbolicmath.exp (q)

In fact, doesn't Python now support overloaded operators for classes -
if so, all but the literals could be handled by a library right now if
someone writes it ;-)


Heres a thought - how about a set of declarations along the lines
of...

from __pragmas__ import numeric_literals_default_integer
from __pragmas__ import numeric_literals_default_longinteger
from __pragmas__ import numeric_literals_default_float

That way, anyone who is worried about accidental use of integers can
simply request their numberic literals default to floats and add a
suffix that specifies integer when they need an integer.

-- 
Steve Horne
Home : steve at lurking.demon.co.uk
Work : sh at ttsoftware.co.uk



More information about the Python-list mailing list