Skip to content

Commit 93283e5

Browse files
authored
fix: clamp (#1248)
1 parent 6833e9d commit 93283e5

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

src/TracedRNumber.jl

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,14 @@ function Base.iseven(x::TracedRNumber{<:Real})
427427
end
428428

429429
for (minT, maxT) in Iterators.product((Number, TracedRNumber), (Number, TracedRNumber))
430-
@eval Base.clamp(x::TracedRNumber, min::$(minT), max::$(maxT)) = Ops.clamp(min, x, max)
430+
@eval function Base.clamp(x::TracedRNumber, min::$(minT), max::$(maxT))
431+
T = promote_type(unwrapped_eltype(x), unwrapped_eltype(min), unwrapped_eltype(max))
432+
return Ops.clamp(
433+
TracedUtils.promote_to(TracedRNumber{T}, min),
434+
TracedUtils.promote_to(TracedRNumber{T}, x),
435+
TracedUtils.promote_to(TracedRNumber{T}, max),
436+
)
437+
end
431438
end
432439

433440
function Base.fill(x::TracedRNumber, dims::NTuple{N,Integer}) where {N}

test/basic.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,10 @@ end
509509
@test minimum(y) 0.0
510510
@test x_ra x
511511
end
512+
513+
x_ra = ConcreteRNumber(3.0)
514+
y = @jit(clamp(x_ra, 0.0, 0.25))
515+
@test y isa ConcreteRNumber{Float64}
512516
end
513517

514518
@testset for op in [round, ceil, floor]

0 commit comments

Comments
 (0)