division

Steven Taschuk staschuk at telusplanet.net
Tue Jun 24 20:24:29 CEST 2003


Quoth Sean Ross:
  [...]
> > >>> txt = pattern.sub(replaceint, "((4 * 5) -2) / 5  ")
  [...]
> > it doesnt raise an exception even though im dividing
> > 18 / 5 which doesnt go evenly .
> 
> Adding the following method to the myint class solves this issue:
> 
>      def __rdiv__(self, other):
>             return self.__div__(other)

Now what if the denominator is computed as well?

    ((4*5) - 2)/(4+1)

You need the values produced by + etc. to be myints as well;
merely inheriting the arithmetic methods from int is inadequate,
since these methods return ints.  So you need to extend __add__,
__sub__, __radd__, ...

Expressions are pretty easy to parse and evaluate, btw; it seems
to me simpler to write your own parser/evaluator, rather than
using eval.

-- 
Steven Taschuk             "The world will end if you get this wrong."
staschuk at telusplanet.net     -- "Typesetting Mathematics -- User's Guide",
                                 Brian Kernighan and Lorrinda Cherry





More information about the Python-list mailing list