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 … Read more

Is the most significant decimal digits precision that can be converted to binary and back to decimal without loss of significance 6 or 7.225?

These are talking about two slightly different things. The 7.2251 digits is the precision with which a number can be stored internally. For one example, if you did a computation with a double precision number (so you were starting with something like 15 digits of precision), then rounded it to a single precision number, the … Read more

Calculate System.Decimal Precision and Scale

Yes, you’d need to use Decimal.GetBits. Unfortunately, you then have to work with a 96-bit integer, and there are no simple integer type in .NET which copes with 96 bits. On the other hand, it’s possible that you could use Decimal itself… Here’s some code which produces the same numbers as your examples. Hope you … Read more

Python to JSON Serialization fails on Decimal [duplicate]

It is not (no longer) recommended you create a subclass; the json.dump() and json.dumps() functions take a default function: def decimal_default(obj): if isinstance(obj, decimal.Decimal): return float(obj) raise TypeError json.dumps({‘x’: decimal.Decimal(‘5.5’)}, default=decimal_default) Demo: >>> def decimal_default(obj): … if isinstance(obj, decimal.Decimal): … return float(obj) … raise TypeError … >>> json.dumps({‘x’: decimal.Decimal(‘5.5’)}, default=decimal_default) ‘{“x”: 5.5}’ The code you … Read more