Repository navigation
fix: Arange follows numpy's algorithm; Linspace and Log2 round like numpy - #9
Merged
Merged
Conversation
…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>
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.
Arange accumulated
v += step:Arange(1, 1.3, 0.1)Arange(0, 2.9000000000000004, 0.1)Arange(1e16, 1e16+6, 1)start+step == start)Arange(0, +Inf, 1),Arange(-Inf, 0, 1)Arange(0, 1, NaN)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 fillstart + 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 gaveLinspace(0.1, 16.150000000000002, 6)[3] == 9.73while amd64 and numpy give9.729999999999999. An explicitfloat64()conversion now rounds the product first.Log2 used
math.Log2, which computeslog(frac)/ln2 + expwithfracin [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. Withfracreduced 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