Skip to content

Use Schubfach algorithm for float repr - #2128

Merged
slozier merged 3 commits into
IronLanguages:mainfrom
slozier:float_repr
Nov 2, 2026
Merged

slozier merged 3 commits into
IronLanguages:mainfrom
slozier:float_repr

Conversation

@slozier

@slozier slozier commented Nov 1, 2026

Copy link
Copy Markdown
Contributor

Use the Schubfach algorithm for float repr, ported from https://github.com/vitaut/schubfach. This now gives the shortest round-tripping string, matching CPython. I opted to use this across the board (including for .NET since it's faster and .NET 10 has a bug with its "R" formatting).

Resolves #1626

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The upstream MIT notice is missing from the ports and binary packages, and the non-NET formatting implementations lack independent boundary-value coverage.

2 open issues
What changed in this PR

Replaces float representation formatting with a Schubfach implementation to produce shortest round-tripping strings across supported runtimes, addressing the excess digits reported in #1626.

Changes:

  • Adds the converter and a source generator for its power-of-ten lookup table.
  • Routes float representations through Schubfach and reports "short" representation style on all targets.
  • Updates formatting, array and pickle expectations, and enables additional float tests on .NET Core.
File Description
tests/​suite/​test_formatting.py Removes legacy float representation expected failures.
tests/​suite/​stdlib/​test_float.py Enables two float tests on .NET Core.
tests/​suite/​modules/​type_related/​test_array.py Uses shortest float representations in array expectations.
tests/​suite/​modules/​io_related/​test_cPickle.py Updates selection of legacy pickle expectations.
src/​roslyn/​IronPython.SourceGenerator/​SchubfachGenerator.cs Generates power-of-ten significands.
src/​roslyn/​IronPython.SourceGenerator/​IronPython.SourceGenerator.csproj Defines the source-generator project.
src/​roslyn/​IronPython.Analyzer/​IronPython.Analyzer.csproj Marks the analyzer as a Roslyn component.
src/​core/​IronPython/​Runtime/​Schubfach.cs Implements conversion and Python-style formatting.
src/​core/​IronPython/​Runtime/​Operations/​FloatOps.cs Delegates float representation to Schubfach.
src/​core/​IronPython/​Modules/​sys.cs Reports short float representation style universally.
src/​core/​IronPython/​IronPython.csproj References the source generator as an analyzer.
IronPython.slnx Adds the source-generator project to the solution.

Review effort: Balanced

Comment on lines +3 to +5
// Copyright (c) 2025 Victor Zverovich
// Distributed under the MIT license.
// Ported from schubfach.cc of https://github.com/vitaut/schubfach.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Licensing is a general issue which probably warrants its own PR...

@BCSharp

BCSharp commented Nov 1, 2026

Copy link
Copy Markdown
Member

Testing Copilot… 😃

@slozier
slozier merged commit cd81441 into IronLanguages:main Nov 2, 2026
34 of 35 checks passed
@slozier
slozier deleted the float_repr branch November 2, 2026 03:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unexpected output precision and/or rounding results in IronPython 3.4.0

3 participants