How to get largest possible precision? (Python – Decimal)

Trying to do this is a mistake. Throwing more precision at a problem is a tempting trap for newcomers to floating-point, but it’s not that useful, especially to this extreme.

Your operations wouldn’t actually require the “largest possible” precision even if that was a well-defined notion. Either they require exact arithmetic, in which case decimal.Decimal is the wrong tool entirely and you should look into something like fractions.Fraction or symbolic computation, or they don’t require that much precision, and you should determine how much precision you actually need and use that.

If you still want to throw all the precision you can at your problem, then how much precision that actually is will depend on what kind of math you’re doing, and how many absurdly precise numbers you’re attempting to store in memory at once. This can be determined by analyzing your program and the memory requirements of Decimal objects, or you can instead take the precision as a parameter and binary search for the largest precision that doesn’t cause a crash.

Leave a Comment