Decimal precision fractions

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).

+6
python decimal long-integer arbitrary-precision
source share
2 answers

"Raise to power" is not a closed operation on rational operations (in contrast to the usual four arithmetic operations): there is no rational number r such that r == 2 ** 0.5 . Legend has it that Pythagoras (from whose theorem this fact so simply follows) was killed by his disciple Gippas for a terrible crime proving this; it looks like you sympathize with the Pythagorean alleged reaction ;-), given your strange use of "should."

Python fractions must be accurate, so there is inevitably a case where increasing the fraction to another fraction power will be absolutely unable to return the fraction as a result; and β€œmust” simply cannot be reasonably applied to mathematical impossibility.

So, best of all, you can come close to your desired result, for example. getting the result that the exact fraction is not (the floats are usually considered sufficient for this purpose), and then further approximates it with a fraction. Most of the existing pure-Python implementations (there are many rationals.py files found around the network ;-) prefer not to implement the ** operator at all, but of course there is nothing to prevent you from making another design decision in your own implementation! -)

+6
source share

You can write your own "pow" function for fractions that does not use floating point elevation. Is this what you are trying to do?

This will lead to an increase in the fraction to integer power with a return to float.

 def pow( fract, exp ): if exp == 0: return fract elif exp % 2 == 0: t = pow( fract, exp//2 ) return t*t else: return fract*pos( fract, exp-1 ) 
0
source share

All Articles