Fall back from unsafe_trunc in UniformStep kernel for non-float ratio types#101
Merged
ChrisRackauckas merged 2 commits intoJul 13, 2026
Conversation
… types
The closed-form UniformStep kernels compute an integer index offset with
`unsafe_trunc(Int, floor(f))` / `unsafe_trunc(Int, ceil(f))`. `unsafe_trunc`
is only defined for hardware floats and `BigFloat`; for a `Rational` ratio
type it has no method, so a uniform `Rational` vector — which `Auto` resolves
to `KIND_UNIFORM_STEP` — threw a `MethodError` on every search.
`f` is already clamped to the in-range interval before truncation, so replace
the raw `unsafe_trunc` with a small dispatch helper: keep `unsafe_trunc` on
the `Base.IEEEFloat` hot path (in-range, so the range check is redundant) and
fall back to the checked `floor(Int, ·)` / `ceil(Int, ·)` for every other
ordered `Real` — which those types support and which is exact for the
clamped `f`. The IEEEFloat hot path is unchanged.
Adds a UniformStep regression testset over uniform `Rational{Int}`,
`Rational{BigInt}`, and `BigFloat` vectors, checking search results against
`Base`.
Co-Authored-By: Chris Rackauckas <accounts@chrisrackauckas.com>
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FvBXVKGujjeaCB3iLwsDeG
ExplicitImports' `all_qualified_accesses_are_public` check flags
`Base.IEEEFloat` — it is neither exported nor declared `public` in Base.
Spell out the hardware-float union `Union{Float16, Float32, Float64}`
(each of which is public) via a local `const` instead. No behavior change.
Co-Authored-By: Chris Rackauckas <accounts@chrisrackauckas.com>
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FvBXVKGujjeaCB3iLwsDeG
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.
Problem
A uniform
Rationalvector crashes on every search:Autoresolves a uniform vector toKIND_UNIFORM_STEP, and the closed-form kernels compute the index offset withunsafe_trunc(Int, floor(f))/unsafe_trunc(Int, ceil(f)).unsafe_truncis only defined for hardware floats andBigFloat; for aRationalratio type it has no method. This is a pre-existing bug onmain, independent of AD — found while testingSearchProperties/search robustness across element types (Float16/32/64, BigFloat, Rational, BigInt, Dual).Fix
fis already clamped to the in-range interval before truncation, so the rawunsafe_truncis replaced by a small dispatch helper:Base.IEEEFloathot path keepsunsafe_trunc(in-range, so its range check is redundant) — unchanged.Real(Rational,BigFloat, ADDual, …) falls back to the checkedfloor(Int, ·)/ceil(Int, ·), which those types support and which is exact for the clampedf.Tests
Adds a UniformStep regression testset over uniform
Rational{Int},Rational{BigInt}, andBigFloatvectors, checking search results (exact-knot, between-knot, out-of-range) againstBase.GROUP=Coresuite: 172,183 pass locally.Independence
Independent of the AD probe PR (#100) — touches
src/kernels.jland a disjoint region of the test file, no shared hunks, noForwardDiffdependency. Either can merge first.