Skip to content

Fix argmin/argmax to return the forward (ravel) index#29

Merged
sigilante merged 1 commit into
mainfrom
sigilante/fix-argmin-argmax-index
May 30, 2026
Merged

Fix argmin/argmax to return the forward (ravel) index#29
sigilante merged 1 commit into
mainfrom
sigilante/fix-argmin-argmax-index

Conversation

@sigilante
Copy link
Copy Markdown
Collaborator

Summary

The Hoon reference returns +:(find ~[(get-item (min a) ...)] (ravel a)) — the forward index into the row-major ravel. ravel is (snip (rip bloq data)) (least-significant element first), and the C jet reads its byte buffer least-significant-first too, so x_bytes[i] == ravel[i]. But the jet stored min_idx = len_x - i - 1 (and likewise max_idx), the reversed index — contradicting its own Hoon spec for any non-symmetric array.

Fix

  • Store the forward index i in argmin/argmax, all four precisions, across the three jet files.
  • Widen the index from c3_w (32-bit) to c3_d: it is assigned from a c3_d loop counter and passed to u3i_chub, so the 32-bit type was a latent truncation for arrays above 2³² elements.

Strict </> comparisons mean the first occurrence wins, matching Hoon's find tie-handling.

Test

Added a property-based regression test that is robust to baum-ordering assumptions: the element fetched at the argmin/argmax index must equal the min/max value. A reversed index fetches the wrong element for the non-symmetric array [3,1,4,2].

Note

This is reasoned from the ravel/rip ordering in source (I can't run Urbit here). It should be confirmed against the Hoon reference in a Vere build — the regression test will validate it there.

🤖 Generated with Claude Code

The Hoon reference returns `+:(find ~[min/max] (ravel a))` — the forward
index into the row-major ravel. ravel is `(snip (rip bloq data))`, i.e.
least-significant element first, and the C jet reads the byte buffer
least-significant first too, so x_bytes[i] == ravel[i]. But the jet
stored `len_x - i - 1`, the reversed index, contradicting its own Hoon
spec for any non-symmetric array.

Store the forward index `i` instead, in argmin and argmax across all
three jet files. Also widen the index from c3_w (32-bit) to c3_d, since
it is assigned from a c3_d loop counter and passed to u3i_chub
(truncation would only bite arrays above 2^32 elements, but the type is
now correct).

Add a property-based regression test: the element at the argmin/argmax
index must equal the min/max value.

Reasoned from source (ravel/rip ordering); should be confirmed against
the Hoon reference in a Vere build.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@sigilante
Copy link
Copy Markdown
Collaborator Author

Replica of #22 fix.

@sigilante sigilante merged commit 19dc818 into main May 30, 2026
@sigilante sigilante deleted the sigilante/fix-argmin-argmax-index branch May 30, 2026 14:49
@sigilante sigilante mentioned this pull request May 30, 2026
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