Comment on PEP-0238

Bengt Richter bokr at accessone.com
Sun Jul 8 18:45:12 EDT 2001


On Sun, 08 Jul 2001 12:27:11 GMT, Guido van Rossum <guido at python.org>
wrote:

>"Emile van Sebille" <emile at fenx.com> writes:
>
>> # default setting
>> precision 0    # everything behaves as it does now
>> print 2 / 7    # -> 0, ie to precision 0 decimal places
>> print 2/7.0   # -> 0.285714285714
>> 
>> precision 1    # all calculations yield 1 decimal point precision interim
>> results
>> print 2 / 7    # -> 0.2, ie to precision 1 decimal places
>> 
>> precision 2
>> print 2 / 7    # -> 0.28, ie to precision 2 decimal places
>> 
>> precision 3
>> print 2 / 7    # -> 0.285, ie to precision 3 decimal places
>> 
>> This seems to me to break no existing code other than where precision is
>> currently used as a label, and allows us to teach others, when they get
>> stung by unexpected results, that they need to set precision appropriately.
>> Once explained, integer division would not be a surprise to newbies, nor
>> would the precision of floating point be lost to those who understand and
>> benefit from its use.  And those of us that just want to write business
>> accounting applications will be able to set precision 2 at the top of each
>> module and be done with it.
>
>Your examples don't show what should happen in this example:
>
>  precision 0
>  x = 2/7
>  precision 4
>  print x
>
>In other words, is the precision a property of the printing or a
>property of the value stored?
>
>There are lots of other ways to get this effect for printing ("%.4f"%x
>comes to mind); if you intended the effect to apply to computation
>results, there are questions about the scope of the calculation.  If I
>write
>
>  precision 3
>  x = f()	# some function that uses int division
>
>should every calculation in f() be affected by the precision
>statement?
>
>Etc., etc.
>
>This is exactly why I was asking you to write a PEP.  I don't mean
>that you have to write an implementation.  (Despite the seeming
>requirement in PEP 2, that's not a requirement before a PEP is
>accepted for review -- that's only required before a standard PEP is
>finally approved for inclusion in the language.  There are plenty of
>PEPs without a shred of implementation.)
>
ISTM the precision statement is a special case of what might be
called an interpreter directive applicable for some defined scope.

Why not parameterize the concept with a single block-scope-introducing
keyword, e.g., 'semantics' or 'special' e.g., rewriting from above:

semantics PRECISION, 0: # everything behaves as it does now
    print 2 / 7    # -> 0, ie to precision 0 decimal places
    print 2/7.0   # -> 0.285714285714
 
semantics PRECISION 1:   # all calculations yield 1 decimal point 
    print 2 / 7    # -> 0.2, ie to precision 1 decimal places

semantics PRECISION 2:
    print 2 / 7    # -> 0.28, ie to precision 2 decimal places

semantics PRECISION 3:
    print 2 / 7    # -> 0.285, ie to precision 3 decimal places

# and then other things are possible too:
semantics INTEGER_DIV:
    print 1/2      # -> 0
semantics FLOAT_DIV:	#new default
    print 1/2	   # -> 0.5
semantics RATIONAL_DIV:
    print 1/2      # -> 1/2 ( embedded '/' with no white space )

The upper case constants would be defined somewhere to make
this work ;-)




More information about the Python-list mailing list