Repository navigation
Fix divisor computation: use highest power of radix <= rule value - #17
Merged
synesthesiam merged 1 commit intoNov 19, 2026
Merged
Conversation
The rule divisor was derived from a leading-digit-normalized proxy via float log(), and that proxy was written back onto the shared cached rule. A rule keyed 2001 (e.g. Italian <<mila<<) therefore split on 10000 instead of 1000, dropping a digit and repeating the multiplier word for values >= 10000, and the cache mutation made output order-dependent. Compute the divisor as ICU does (NFRule::expectedExponent): the highest power of the radix that is <= the rule's base value, via an integer loop with no mutation. Remove the now-dead ceil/floor/log imports. Differential vs PyICU 68.1 over 88 languages: 24 cells go wrong -> exact ICU match, zero regress, 84 languages byte-identical. Adds three tests.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix divisor computation: use highest power of radix <= rule value
What
The divisor used to split a number across a rule (the
//quotient and%%remainder substitutions) was computed from a leading-digit-normalized proxy vialog(), instead of ICU's actual rule. Two user-reachable effects:2001(<<mila<<). Its divisor must be1000, but the old code normalized2001's leading digit to1(→1001), tookceil(log10(1001)) = 4, and divided by10000. Soformat_number(123456789, ruleset_names=["spellout-ordinal-masculine"])foritdropped a digit in the 456-thousand group and then repeated the multiplier word, speaking it as...quarantacinquemilaseimila...instead of...quattrocentocinquantaseimila....rule.value = int("1" + str(rule.value)[1:])mutated the shared, cached rule object. A300rule became100after being used once, so after formatting321a subsequentformat_number(350)returned the wrong spellout. Results depended on call order.Why
In ICU RBNF the divisor for a non-special rule is
NFRule::expectedExponent()— the highest power of the radix that is<= rule.value. For a rule keyed2001with radix10that is1000, not10000. The previous implementation approximated this with floating-pointlog()on a digit-normalized value, which (a) picked the wrong power whenever the base value had a non-zero digit after the first, and (b) mutated the cached rule to do so.Fix
Compute the divisor with an integer loop and no mutation:
No float
log()(so exact powers like1000are never pushed up by float error), no write-back torule.value. The now-deadceil,floor,logimports are removed.Verification
Differential vs ICU oracle
Swept 88 bundled languages against a PyICU 68.1 oracle over a value grid (22,040 cells), before and after the change:
it,hu,vec,zu.Tests
python3 -m pytest -q→ 134 passed (131 pre-existing + 3 new).unicode_rbnf/engine.py(keeping the tests) → exactly the 3 new tests fail (test_italian_ordinal_thousands,test_divisor_uses_highest_power_not_leading_digit,test_divisor_does_not_mutate_cached_rule), nothing else.Lint / types
mypyclean,pylint10.00/10 on the changed file.Honest note: residual
it/huordinal suffix spelling differences vs ICU still exist. They are a separate pre-existing issue in how the ordinal suffix is appended, unrelated to the divisor, and are not addressed here.Impact
This library backs Rhasspy / Home Assistant TTS, so an incorrect spellout is read aloud to users. Before this change, any value ≥ 10000 in the affected languages spoke a wrong number (dropped digit plus a duplicated multiplier word). The cache-mutation half also made cardinal output order-dependent, so a correct result could silently become wrong after an unrelated earlier call. The fix brings the affected cells to exact ICU parity while leaving every other language byte-identical. It touches only the divisor computation, with no API change.