Skip to content

feat: Repr prints exactly what NumPy 2.5.3's repr prints - #49

Merged
tannevaled merged 1 commit into
mainfrom
repr-numpy-exact
Oct 11, 2026
Merged

tannevaled merged 1 commit into
mainfrom
repr-numpy-exact

Conversation

@tannevaled

Copy link
Copy Markdown
Contributor

Repr wrote each element in its shortest form and right-aligned them, so its digits and padding differed from NumPy's:

array before NumPy 2.5.3 (now)
[1.5, 2, 3] float32 array([1.5, 2., 3.], dtype=float32) array([1.5, 2. , 3. ], dtype=float32)
[1e20, 0.5] array([1e+20, 0.5]) array([1.e+20, 5.e-01])
[True, True] array([True, True]) array([ True, True])
arange(2000.) array([ 0., 1., … array([0.000e+00, 1.000e+00, … (max/min > 1000)

display.go is now a port of numpy/_core/arrayprint.py at v2.5.3:

  • Formatters: FloatingFormat (maxprec, precision 8), ComplexFloatingFormat, IntegerFormat and BoolFormat.
  • Layout: _formatArray and _extendLine with 75 columns.
  • Suffixes: the shape= and dtype= extras, including their wrapping onto a new line.
  • Digits: taken from dragon4.c: the shortest that read back, cut to 8 and rounded half to even on the exact value. That is also how strconv rounds.
  • Scientific cut-off: it depends on the dtype, as in 2.5.3: 1e8 for float64, 1e6 for float32.

Oracle. NumPy's own expected strings, copied verbatim from test_arrayprint.py (v2.5.3):

  • 26 repr cases, from test_nan_inf, test_float_spacing, test_bool_spacing, test_sign_spacing, test_floatmode (maxprec), test_summarize_1d/2d and test_summarize_2d_dtype;
  • 72 complex special values (TestComplexArray.test_str, complex64 and complex128);
  • gh-28679's float32/complex64 cut-off elements.

Four more cases have no default-options case in NumPy's suite (empty arrays, the 8-digit cut in scientific mode, a negative tiny value). For those, the expectation is derived from the rules, and the rule is written beside each case.

Checks:

  • Setting the float32 cut-off back to 1e8 fails the tests, and so does dropping the bool padding.
  • Three old expectations encoded the previous, wrong output; they are corrected, with the reason in a comment.
  • Coverage is 100% for the root package.
  • All packages pass, and the tests compile for linux/386 and arm.

Downstream: go-embedded-ruby's NDArray#inspect prints this Repr. Its tests that compare against go-ndarray computed in Go will follow on their own. Literal strings in its tests will need the NumPy forms when it moves to this release.

🤖 Generated with Claude Code

Repr wrote each element in its shortest form and right-aligned them, so
its digits and padding differed from NumPy's: array([1.5,  2.,  3.])
where NumPy prints array([1.5, 2. , 3. ]), 1e+20 where it prints
1.e+20, " True" missing its space when no False is present, and no
scientific notation for arange(2000.).

display.go is now a port of numpy/_core/arrayprint.py at v2.5.3
(FloatingFormat in maxprec mode, precision 8; ComplexFloatingFormat;
IntegerFormat; BoolFormat; _formatArray and _extendLine; the repr's
shape=/dtype= extras and their wrapping), with dragon4.c's digits: the
shortest that read back, cut to 8 and rounded half to even on the exact
value, which is how strconv rounds. The scientific cut-off depends on
the dtype as in 2.5.3: 1e8 for float64, 1e6 for float32.

Checked against NumPy's own expected strings (test_arrayprint.py,
v2.5.3): 26 repr cases, the 72 complex special values of
TestComplexArray.test_str, and gh-28679's float32/complex64 cut-off
elements. Breaking the float32 cut-off or the bool padding fails them.
Three old expectations encoded the previous, wrong output; corrected.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@tannevaled
tannevaled merged commit b41dd2d into main Oct 11, 2026
12 checks passed
@tannevaled
tannevaled deleted the repr-numpy-exact branch October 11, 2026 18:23
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.

1 participant