Skip to content

fix: Arange follows numpy's algorithm; Linspace and Log2 round like numpy - #9

Merged
tannevaled merged 1 commit into
mainfrom
fix/arange-linspace
Oct 4, 2026
Merged

tannevaled merged 1 commit into
mainfrom
fix/arange-linspace

Conversation

@tannevaled

Copy link
Copy Markdown
Contributor

Arange accumulated v += step:

call before numpy / after
Arange(1, 1.3, 0.1) 3 elements 4 elements
Arange(0, 2.9000000000000004, 0.1) 29 elements 30 elements
Arange(1e16, 1e16+6, 1) never returns (start+step == start) 6 elements
Arange(0, +Inf, 1), Arange(-Inf, 0, 1) loops until memory runs out error
Arange(0, 1, NaN) empty array error
Arange(0, 1, +Inf) [0] [0] (unchanged)

It now uses numpy's length ceil((stop-start)/step) (including numpy's rule when that quotient underflows to 0) and numpy's fill start + i*((start+step)-start). The values match numpy 2.0.2 on x86_64 bit for bit. numpy's arm64 builds compile that fill as an FMA and can differ by one ULP, so numpy is not consistent across architectures here; the Go side now gives the same result on every architecture.

Linspace computed start + float64(i)*step. Go may fuse that into an FMA on arm64, ppc64le and s390x, so arm64 gave Linspace(0.1, 16.150000000000002, 6)[3] == 9.73 while amd64 and numpy give 9.729999999999999. An explicit float64() conversion now rounds the product first.

Log2 used math.Log2, which computes log(frac)/ln2 + exp with frac in [0.5, 1) and so cancels just above 1: log2(1+1e-10) was off by a relative 3e-7, and 12% of 900k random inputs were 4 or more ULP from numpy. With frac reduced to [sqrt(1/2), sqrt(2)) first, all of them are within 2 ULP (88% exact). numpy's log2 gives the same values on arm64 and x86_64 for the test inputs.

How these were found: a differential sweep of 9900 random cases (NaN/Inf, strided/transposed views, broadcasting), checked against numpy on both arm64 (2.5.3) and x86_64 under Rosetta (2.0.2). The harness is not committed. Against x86_64 numpy the sweep now reports 0 mismatches; against arm64 numpy the remaining ones are the arange FMA ULPs described above.

Sabotage checks (each change reverted in turn, test red): the old Arange, the missing underflow rule, math.Log2, and the fused Linspace/Arange expressions. The fused-expression checks can only fail on an FMA architecture, so on the amd64 runners those two tests pass either way; the macOS arm64 runner is the one that checks them.

🤖 Generated with Claude Code

…umpy

- Arange accumulated v += step. That drifted (Arange(1, 1.3, 0.1) had 3
  elements, numpy 4; (0, 2.9000000000000004, 0.1) had 29, numpy 30), never
  terminated when start+step == start (Arange(1e16, 1e16+6, 1)) or when stop
  was infinite, and returned an empty array for a NaN operand. It now uses
  numpy's length ceil((stop-start)/step), numpy's fill
  start + i*((start+step)-start), and errors on a NaN or unaddressable length.
- Linspace computed start + float64(i)*step, which Go may fuse into an FMA on
  arm64/ppc64le/s390x, so results differed from amd64 and from numpy by an
  ULP (Linspace(0.1, 16.150000000000002, 6)[3] was 9.73, numpy
  9.729999999999999). The product is now rounded first. Arange does the same.
- Log2 used math.Log2, which cancels catastrophically just above 1
  (log2(1+1e-10) had a relative error of 3e-7; 12% of 900k random inputs
  were 4+ ULP from numpy). It now reduces the mantissa to [sqrt(1/2),
  sqrt(2)) first and stays within 2 ULP.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@tannevaled
tannevaled merged commit 74aa22f into main Oct 4, 2026
9 checks passed
@tannevaled
tannevaled deleted the fix/arange-linspace branch October 4, 2026 09:13
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