Is there a pure python implementation of fractions.Fraction
that supports long
s as numerator and denominator? Unfortunately, exponentiation appears to be coded in to return a float (ack!!!), which should at least support using decimal.Decimal
.
If there isn't, I suppose I can probably make a copy of the library and try to replace occurrences of float()
with something appropriate from Decimal
but I'd rather something that's been tested by others before.
Here's a code example:
base = Fraction.from_decimal(Decimal(1).exp())
a = Fraction(69885L, 53L)
x = Fraction(9L, 10L)
print base**(-a*x), type(base**(-a*x))
results in 0.0 <type 'float'>
where the answer should be a really small decimal.
Update: I've got the following work-around for now (assuming, for a**b, that both are fractions; of course, I'll need another function when exp_ is a float or is itself a Decimal):
def fracpow(base, exp_):
base = Decimal(base.numerator)/Decimal(base.denominator)
exp_ = Decimal(exp_.numerator)/Decimal(exp_.denominator)
return base**exp_
which gives the answer 4.08569925773896097019795484811E-516.
I'd still be interested if there's a better way of doing this without the extra functions (I'm guessing if I work with the Fraction
class enough, I'll find other floats working their way into my results).