On Fri, Oct 9, 2020 at 3:43 AM Steven D'Aprano <steve@pearwood.info> wrote:
On Thu, Oct 08, 2020 at 11:58:04AM -0400, Random832 wrote:
I was making a "convert Fraction to Decimal, exactly if possible" function and ran into a wall: it's not possible to do some of the necessary operations with exact precision in decimal:
Of course you can't do them with *exact* precision in Decimal, because that only has a fixed precision. (User-configurable, but fixed.)
py> from decimal import Decimal py> from fractions import Fraction py> x = Fraction(1, 3) py> Decimal(x.numerator)/x.denominator Decimal('0.3333333333333333333333333333')
You trimmed off the part where the OP said that the denominator would be only multiples of 2 and/or 5 :)
Fraction(1, 2) ** 53 * Fraction(1, 5) ** 47 Fraction(1, 6400000000000000000000000000000000000000000000000) Decimal("314159265358979323") / _.denominator Decimal('4.908738521234051921875E-32')
In theory, this should be precisely representable in decimal, but how many digits would you need? ChrisA