You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Divide is quadratic in the power of 5 in the denominator: dividing by 3·0.5^100000 takes 5.2 s vs 27 ms for a similar-size 3·0.7^100000 #187
For a denominator containing 5^b with n digits, that is O(b·n) work. The loop always runs to completion, even when the quotient turns out not to terminate. Any binary fraction 2^-n (repeated halving, bisection, 0.5.Pow(n)) is stored with a significand of 5^n, so ordinary values hit this path.
Divide and operator / sit underneath almost every transcendental function. A 10-digit answer should cost roughly the same however many factors of 5 the denominator has. Instead the cost grows with the square of the operand size, for exactly the values that binary-fraction workloads produce.
Suggested fix / acceptance criteria
Don't count the fives one at a time.
If the quotient terminates, remaining must be exactly 5^b, with b ≈ bitLength / log2(5). Compute that candidate with BigInteger.Pow(5, b) (checking b±1) and compare.
Or remove fives by repeated squaring or binary search on 5^k, the way CountTrailingZeros handles tens.
Add a benchmark or timed test: Divide(1, 3·0.5^100000, 10) finishes in roughly the same time as Divide(1, 3·0.7^100000, 10), and exact quotients like 1 / 0.5^n still come back exact.
Priority: Medium. Divide sits under most transcendental functions, and binary fractions (2^-n, which are stored as 5^n) are ordinary values. The slowdown only becomes noticeable at large n, though, and results stay correct.
Area / suggested assignment: Core arithmetic, TryDivideExactly in PreciseNumber/PreciseNumber.cs. Whoever owns the division and normalisation paths should take it.
Next step: Use the bit-length estimate of b with BigInteger.Pow(5, b) (checking b±1), and add the timed comparison against 3·0.7^n described in the issue.
Activity
matt-edmondson commentedon Nov 2, 2026
Triage
Dividesits under most transcendental functions, and binary fractions (2^-n, which are stored as 5^n) are ordinary values. The slowdown only becomes noticeable at large n, though, and results stay correct.TryDivideExactlyinPreciseNumber/PreciseNumber.cs. Whoever owns the division and normalisation paths should take it.Divide) is related and may share a benchmark. No open PR touchesTryDivideExactly; Trim only ASCII white space under AllowLeadingWhite and AllowTrailingWhite [patch] #182–Honour AllowParentheses, AllowTrailingSign and AllowCurrencySymbol in Parse [patch] #184 are about parsing andLog.BigInteger.Pow(5, b)(checking b±1), and add the timed comparison against3·0.7^ndescribed in the issue.Generated by Claude Code