-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Converting between integer types #22235
Changes from 2 commits
25fa6f1
4a59f65
27d9634
ed37037
6efde61
fcd526e
9a8a73c
2243307
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,9 +41,11 @@ end | |
|
||
""" | ||
clamp(x, lo, hi) | ||
clamp(x, T) | ||
|
||
Return `x` if `lo <= x <= hi`. If `x < lo`, return `lo`. If `x > hi`, return `hi`. Arguments | ||
are promoted to a common type. | ||
are promoted to a common type. If the type `T` is given `lo` and `hi` are typemin(T) | ||
and typemax(T) respectively and the result is converted to `T` | ||
|
||
```jldoctest | ||
julia> clamp.([pi, 1.0, big(10.)], 2., 9.) | ||
|
@@ -59,10 +61,13 @@ clamp(x::X, lo::L, hi::H) where {X,L,H} = | |
convert(promote_type(X,L,H), lo), | ||
convert(promote_type(X,L,H), x))) | ||
|
||
clamp( x, ::Type{T} ) where {T} = clamp( x, typemin(T), typemax(T) ) % T | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No spaces around parens (here and elsewhere) |
||
""" | ||
clamp!(array::AbstractArray, lo, hi) | ||
clamp!(array::AbstractArray, T) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This line should be removed since you aren't defining this method |
||
|
||
Restrict values in `array` to the specified range, in-place. | ||
Restrict values in `array` to the specified range, in-place. If the type `T` is | ||
given `lo` and `hi` are typemin(T) and typemax(T) respectively | ||
See also [`clamp`](@ref). | ||
""" | ||
function clamp!(x::AbstractArray, lo, hi) | ||
|
@@ -72,6 +77,13 @@ function clamp!(x::AbstractArray, lo, hi) | |
x | ||
end | ||
|
||
function clamp!(x::AbstractArray, ::Type{T}) where {T} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not strictly related, but is the
instead of
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I completely agree. It seemed strange that the vector version of clamp has been deprecated but the in place version hadn't. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't that the case with multiple in place functions right now, e.g:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What does "intended to work on the array as a whole" mean? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess I think of |
||
@inbounds for i in eachindex(x) | ||
x[i] = clamp(x[i], T ) | ||
end | ||
x | ||
end | ||
|
||
# evaluate p[1] + x * (p[2] + x * (....)), i.e. a polynomial via Horner's rule | ||
macro horner(x, p...) | ||
ex = esc(p[end]) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typically in Base APIs, the type argument comes first. So this should be
clamp(T, x)
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Documentation wasn't updated.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I pushed a commit to fix that.