Repository navigation
Convert bases to 36 exactly, and refuse a digit not in the base - #2351
Merged
Merged
Conversation
FromBaseN added its digits up in an int with (int)Math.Pow(N, i) for the
place value, so a number past 2^31 wrapped ("80000000" in base 16 was
-2147483648), and it looked each digit up without checking it against the
base ("102" in base 2 was 6). It adds them by Horner's rule in EInteger,
sums the fraction exactly and rounds once, and throws
InvalidNumericSystemException for a digit not below the base or a base
outside 2 to 36.
The digit table stopped at F, where the catalogue's to_base and from_base
and the MCP server's am_represent offer bases to 36: ToBaseN(35, 36) threw
and "ff" was a KeyNotFoundException. It goes to Z now, read in either
case.
And ToBaseN of a rational went through its decimal, rounded to the
precision and the rounding multiplied up digit by digit, so 1/7 in base 7
ran to 119 digits ending on a wrong one. A rational is divided out in the
base, exactly; a repeating expansion is cut where it was.
Closes #2343
Closes #2344
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #2351 +/- ##
==========================================
- Coverage 86.78% 86.77% -0.02%
==========================================
Files 312 312
Lines 68675 68733 +58
Branches 19988 20014 +26
==========================================
+ Hits 59602 59643 +41
- Misses 4905 4917 +12
- Partials 4168 4173 +5
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
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.
Closes #2343. Closes #2344.
MathS.FromBaseN("80000000", 16)-21474836482147483648MathS.FromBaseN("10000000000", 10)214748364710000000000MathS.FromBaseN("102", 2)6InvalidNumericSystemExceptionMathS.FromBaseN("ff", 16)KeyNotFoundException255MathS.FromBaseN("G", 17)KeyNotFoundException16MathS.ToBaseN(35, 36)InvalidNumericSystemExceptionZMathS.ToBaseN(1/7, 7)0.1MathS.ToBaseN(1/3, 3)0.0222…2, 210 digits0.1FromBaseNadds the whole part by Horner's rule inEInteger, sums the fraction exactly as a rational, and rounds once into the precision context. A digit that is not below the base, a character that is not a digit, an empty string and a base outside 2 to 36 throwInvalidNumericSystemException.Z, read in either case. The catalogue'sto_baseandfrom_base, and the MCP server'sam_represent, already said "a base from 2 to 36".ToBaseNof aRationaldivides it out in the base, digit by digit, exactly. A real that is not rational goes through its decimal as before. A repeating expansion is still cut at the precision (0.333…to 100 digits in base 10), as Hangs on imprecise number base conversions #584 left it. The issue raises marking the period as an option, and that is a display decision this leaves open.Tests:
ABaseConversionToThirtySixTest, 25 cases. 20 fail on master; the 5 that already worked pass on both. The existing base tests and the documentation examples pass unchanged. The full suite, AOT and the nine harnesses pass. There is a BREAKING-CHANGES entry, measured on 2.6.0.🤖 Generated with Claude Code