[Patches] import floatdivision # 1/2==0.5
Greg Stein
gstein@lyra.org
Tue, 13 Jun 2000 00:47:34 -0700
On Tue, Jun 13, 2000 at 07:29:31AM +0300, Moshe Zadka wrote:
> On Mon, 12 Jun 2000, David Scherer wrote:
>
> > This patch provides a way to make 1/2==0.5 optional without introducing any
> > backward incompatibilities. It provides a pseudomodule "floatdivision"
> > that, when imported, changes the behavior of the importing script at compile
> > time:
> >
> > import floatdivision
> > print 3/4 # 0.75
>
> -1 on that. I don't like language to be dependant on modules: no "library"
> can import this module, because it might break other code. On the other
> hand, no "application" can import this module, because it might break
> library code. Plus a general -1 on thrusting floating point in innocent
> people's faces.
Careful with your impressions there... :-) There isn't a module, and your
statement of its scope is incorrect.
The patch is actually much more subtle. The "import floatdivision" is not
really an import, and it doesn't affect anything outside of the
"compound_stmt" that it occurs in (and the sub-statements). Effectively, the
flag is scoped.
def foo():
import floatdivision
print 3/4
foo()
print 3/4
The answer is 0.75 and 0. Pretty neat, huh?
What happens is when the *compiler* sees "import floatdivision", it enables
a flag. All further divisions in that compilation unit (and sub-units) use a
new opcode for fractional division. Any other compile that doesn't have a
similar import will insert the regular division bytecode.
While this is a really slick way to accomplish this, I'm not all that
comfortable with the "magic" that makes it work.
Cheers,
-g
--
Greg Stein, http://www.lyra.org/