Comment on PEP-0238
Tim Peters
tim.one at home.com
Tue Jul 10 01:59:58 EDT 2001
[Tim]
> The last time we solved a "it's *hard* to make sure temporary context
> changes get undone!" problem in Python was via adding ">>" to
> "print" <0.8 wink>.
[Greg Ewing]
> In other words, the solution was to get rid of the
> context and make it a parameter to the operation
> instead. I think there is an important lesson to
> be learned there somewhere <0.000 wink>.
If you want to turn every + - * / etc into a long-winded function call
explicitly passing and updating a context object, you can already do so in
Java (lightly edited from
<http://www2.hursley.ibm.com/decimalj/decdemo.java>, to avoid split lines):
/* Demonstration program showing use of the enhanced BigDecimal class. */
/* */
/* This shows the results of repeatedly dividing by ten, using: */
/* BigDecimal (decimal floating point) */
/* float (binary floating point) */
/* datatypes. */
import com.ibm.math.*; // import BigDecimal and MathContext
public class decdemo
{
public static void main(String args[])
{
String s; // work variable
float f=9; // binary floating point var
BigDecimal d=new BigDecimal(9); // decimal floating point var
MathContext def=MathContext.DEFAULT;// standard arithmetic context
for (int i=0; i<10; i++)
{
d=d.divide(BigDecimal.TEN, def); // divide using decimal fp
f=f/10; // divide using binary fp
// pad the first result with blanks for display, then display both
s=d.toString();
s=s.concat(new String(new char[10-s.length()])).replace('\000',' ');
System.out.println(s+" "+String.valueOf(f));
}
}
}
Does anybody in their right mind want to endure (no, it doesn't look a *lot*
better in Python <wink>)
d = d.divide(BigDecimal.TEN, def)
for routine use? Context is an inherent part of finite arithmetic; it can't
be wished away. Although if it's made an implicit part of thread-local
state it can be *ignored* by most people most of the time (just like *you*
likely ignore the 6 implicit inputs to, and 5 implicit outputs of, every
binary 754 fp operation you perform today).
practicality-beats-purity-ly y'rs - tim
More information about the Python-list
mailing list