@@ -23,7 +23,7 @@ import .Base: log, exp, sin, cos, tan, sinh, cosh, tanh, asin,
2323using . Base: sign_mask, exponent_mask, exponent_one,
2424 exponent_half, uinttype, significand_mask,
2525 significand_bits, exponent_bits, exponent_bias,
26- exponent_max, exponent_raw_max
26+ exponent_max, exponent_raw_max, clamp, clamp!
2727
2828using Core. Intrinsics: sqrt_llvm
2929
6969 return Txy, T (xy- Txy)
7070end
7171
72- """
73- clamp(x, lo, hi)
74-
75- Return `x` if `lo <= x <= hi`. If `x > hi`, return `hi`. If `x < lo`, return `lo`. Arguments
76- are promoted to a common type.
77-
78- See also [`clamp!`](@ref), [`min`](@ref), [`max`](@ref).
79-
80- !!! compat "Julia 1.3"
81- `missing` as the first argument requires at least Julia 1.3.
82-
83- # Examples
84- ```jldoctest
85- julia> clamp.([pi, 1.0, big(10)], 2.0, 9.0)
86- 3-element Vector{BigFloat}:
87- 3.141592653589793238462643383279502884197169399375105820974944592307816406286198
88- 2.0
89- 9.0
90-
91- julia> clamp.([11, 8, 5], 10, 6) # an example where lo > hi
92- 3-element Vector{Int64}:
93- 6
94- 6
95- 10
96- ```
97- """
98- function clamp (x:: X , lo:: L , hi:: H ) where {X,L,H}
99- T = promote_type (X, L, H)
100- return (x > hi) ? convert (T, hi) : (x < lo) ? convert (T, lo) : convert (T, x)
101- end
102-
103- """
104- clamp(x, T)::T
105-
106- Clamp `x` between `typemin(T)` and `typemax(T)` and convert the result to type `T`.
107-
108- See also [`trunc`](@ref).
109-
110- # Examples
111- ```jldoctest
112- julia> clamp(200, Int8)
113- 127
114-
115- julia> clamp(-200, Int8)
116- -128
117-
118- julia> trunc(Int, 4pi^2)
119- 39
120- ```
121- """
122- function clamp (x, :: Type{T} ) where {T<: Integer }
123- # delegating to clamp(x, typemin(T), typemax(T)) would promote types
124- # this way, we avoid unnecessary conversions
125- # think of, e.g., clamp(big(2) ^ 200, Int16)
126- lo = typemin (T)
127- hi = typemax (T)
128- return (x > hi) ? hi : (x < lo) ? lo : convert (T, x)
129- end
130-
131-
132- """
133- clamp!(array::AbstractArray, lo, hi)
134-
135- Restrict values in `array` to the specified range, in-place.
136- See also [`clamp`](@ref).
137-
138- !!! compat "Julia 1.3"
139- `missing` entries in `array` require at least Julia 1.3.
140-
141- # Examples
142- ```jldoctest
143- julia> row = collect(-4:4)';
144-
145- julia> clamp!(row, 0, Inf)
146- 1×9 adjoint(::Vector{Int64}) with eltype Int64:
147- 0 0 0 0 0 1 2 3 4
148-
149- julia> clamp.((-4:4)', 0, Inf)
150- 1×9 Matrix{Float64}:
151- 0.0 0.0 0.0 0.0 0.0 1.0 2.0 3.0 4.0
152- ```
153- """
154- function clamp! (x:: AbstractArray , lo, hi)
155- @inbounds for i in eachindex (x)
156- x[i] = clamp (x[i], lo, hi)
157- end
158- x
159- end
160-
161- """
162- clamp(x::Integer, r::AbstractUnitRange)
163-
164- Clamp `x` to lie within range `r`.
165-
166- !!! compat "Julia 1.6"
167- This method requires at least Julia 1.6.
168- """
169- clamp (x:: Integer , r:: AbstractUnitRange{<:Integer} ) = clamp (x, first (r), last (r))
17072
17173"""
17274 evalpoly(x, p)
@@ -1690,7 +1592,6 @@ end
16901592
16911593exp2 (x:: AbstractFloat ) = 2 ^ x
16921594exp10 (x:: AbstractFloat ) = 10 ^ x
1693- clamp (:: Missing , lo, hi) = missing
16941595fourthroot (:: Missing ) = missing
16951596
16961597end # module
0 commit comments