This is a bug.
Here's why this happens:
The error condition described in this method is hit:
"Unlike other operations, if the length of the coefficient after the quantize operation would be greater than precision, then an InvalidOperation is signaled. This guarantees that, unless there is an error condition, the quantized exponent is always equal to that of the right-hand operand."
What's happening is that the precision automatically inferred from the numbers you've provided (the ones with the large fraction) exceed that of the context.
e.g. with this patch it runs:
bergamot [git|v2]:~/p/beancount$ git diff
diff --git a/beancount/core/display_context.py b/beancount/core/display_context.py
index cfed0188..9a9dbfce 100644
--- a/beancount/core/display_context.py
+++ b/beancount/core/display_context.py
@@ -217,6 +217,10 @@ class DisplayContext:
# Note: We could probably logging.warn() this situation here.
return number
qdigit = Decimal(1).scaleb(-num_fractional_digits)
+
+ import decimal; decimal.getcontext().prec = 30
+ print("XXX", qdigit, decimal.getcontext().prec)
+
return number.quantize(qdigit)
I'll have to find a solution.