Re: [Python-Dev] [Python-checkins] python/nondist/peps pep-0343.txt, 1.11, 1.12

2005-05-19 Thread Greg Ewing
Tim Peters wrote:
 [Raymond Hettinger]

from decimal import getcontext, Decimal as D
getcontext().prec = 3
D('3.104') + D('2.104')

Decimal(5.21)

D('3.104') + D('0.000') + D('2.104')

Decimal(5.20)
 
 the results differ here because D(whatever)
 ignores context settings; having a common operation ignore context is
 ugly and error-prone).

I don't see it's because of that. Even if D(whatever)
didn't ignore the context settings, you'd get the same
oddity if the numbers came from somewhere else with a
different precision.

I'm very uncomfortable about the whole idea of a
context-dependent precision. It just seems to be
asking for trouble.

-- 
Greg Ewing, Computer Science Dept, +--+
University of Canterbury,  | A citizen of NewZealandCorp, a   |
Christchurch, New Zealand  | wholly-owned subsidiary of USA Inc.  |
[EMAIL PROTECTED]  +--+
___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] [Python-checkins] python/nondist/peps pep-0343.txt, 1.11, 1.12

2005-05-19 Thread Tim Peters
[Greg Ewing]
 I don't see it's because of that. Even if D(whatever)
 didn't ignore the context settings, you'd get the same
 oddity if the numbers came from somewhere else with a
 different precision.

Most users don't change context precision, and in that case there is
no operation defined in the standard that can _create_ a decimal with
different precision.  Python's Decimal constructor, however, can
(Python's Decimal constructor performs an operation that's not in the
standard -- it's a Python-unique extension to the standard).

 I'm very uncomfortable about the whole idea of a
 context-dependent precision. It just seems to be
 asking for trouble.

If you're running on a Pentium box, you're using context-dependent
precision a few million times per second.  Most users will be as
blissfully unaware of decimal's context precsion as you are of the
Pentium FPU's context precision.  Most features in fp standards are
there for the benefit of experts.  You're not required to change
context; those who need such features need them desperately, and don't
care whether you think they should wink.

An alternative is a God-awful API that passes a context object
explicitly to every operation.  You can, e.g., kiss infix + goodbye
then.  Some implementations of the standard do exactly that.

You might want to read the standard before getting carried off by gut reactions:

http://www2.hursley.ibm.com/decimal/
___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] [Python-checkins] python/nondist/peps pep-0343.txt, 1.11, 1.12

2005-05-17 Thread Raymond Hettinger
 +def sin(x):
 +Return the sine of x as measured in radians.
 +do with_extra_precision():
 +i, lasts, s, fact, num, sign = 1, 0, x, 1, x, 1
 +while s != lasts:
 +lasts = s
 +i += 2
 +fact *= i * (i-1)
 +num *= x * x
 +sign *= -1
 +s += num / fact * sign
 +return +s

One more change:  The final return +s should be unindented.  It should
be at the same level as the do with_extra_precision().  The purpose of
the +s is to force the result to be rounded back to the *original*
precision.

This nuance is likely to be the bane of folks who shift back and forth
between different levels of precision.  The following example shows the
kind of oddity that can arise when working with quantities that have not
been rounded to the current precision:

 from decimal import getcontext, Decimal as D
 getcontext().prec = 3
 D('3.104') + D('2.104')
Decimal(5.21)
 D('3.104') + D('0.000') + D('2.104')
Decimal(5.20)



Raymond
___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] [Python-checkins] python/nondist/peps pep-0343.txt, 1.11, 1.12

2005-05-17 Thread Tim Peters
[Raymond Hettinger]
 ...
 One more change:  The final return +s should be unindented.  It should
 be at the same level as the do with_extra_precision().  The purpose of
 the +s is to force the result to be rounded back to the *original*
 precision.

 This nuance is likely to be the bane of folks who shift back and forth
 between different levels of precision.

Well, a typical user will never change precision most of the time.  Of
the remaining uses, most will set precision once at the start of the
program, and never change it again.  Library authors may change
precision frequently, but they should be experts.

 The following example shows the kind of oddity that can arise when
 working with quantities that have not been rounded to the current precision:

  from decimal import getcontext, Decimal as D
  getcontext().prec = 3
  D('3.104') + D('2.104')
 Decimal(5.21)
  D('3.104') + D('0.000') + D('2.104')
 Decimal(5.20)

I think it shows more why it was a mistake for the decimal constructor
to extend the standard (the string-decimal operation in the standard
respects context settings; the results differ here because D(whatever)
ignores context settings; having a common operation ignore context is
ugly and error-prone).
___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com