Is there a pure python implementation of fractions.Fraction that supports long as a numerator and denominator? Unfortunately, the exponential is apparently encoded to return a float (ack !!!), which should at least support using decimal.Decimal .
If this is not the case, I suggest that perhaps I can make a copy of the library and try to replace the occurrences of float() with something suitable from Decimal , but I would prefer something that was previously verified by others.
Here is a sample code:
base = Fraction.from_decimal(Decimal(1).exp()) a = Fraction(69885L, 53L) x = Fraction(9L, 10L) print base**(-a*x), type(base**(-a*x))
leads to 0.0 <type 'float'> where the answer must be a really small decimal number.
Update . At the moment, I have the following work (assuming for ** b that both are fractions, of course, I will need another function when exp_ is float or decimal itself):
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 would still be wondering if there is a better way to do this without additional features (I assume that if I work with the Fraction class, I will find other floats that will work in my results).
python decimal long-integer arbitrary-precision
Noah
source share