Skip to content
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

RFC: Restricts parameter type for randn & randexp to Float types for #17608. #17627

Merged
merged 3 commits into from
Jul 29, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions base/random.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1200,7 +1200,7 @@ let Floats = Union{Float16,Float32,Float64}
@eval begin
# scalars
$randfun{T<:$Floats}(rng::AbstractRNG, ::Type{T}) = convert(T, $randfun(rng))
$randfun{T<:$Floats}(::Type{T}) = $randfun(GLOBAL_RNG, T)
$randfun{T}(::Type{T}) = $randfun(GLOBAL_RNG, T)

# filling arrays
function $randfun!{T}(rng::AbstractRNG, A::AbstractArray{T})
Expand All @@ -1213,14 +1213,15 @@ let Floats = Union{Float16,Float32,Float64}
$randfun!(A::AbstractArray) = $randfun!(GLOBAL_RNG, A)

# generating arrays
$randfun{T}(rng::AbstractRNG, ::Type{T}, dims::Dims) = $randfun!(rng, Array{T}(dims))
$randfun{T}(rng::AbstractRNG, ::Type{T}, dims::Integer...) = $randfun!(rng, Array{T}(dims...))
$randfun{T}( ::Type{T}, dims::Dims) = $randfun(GLOBAL_RNG, T, dims)
$randfun{T}( ::Type{T}, dims::Integer...) = $randfun(GLOBAL_RNG, T, dims...)
$randfun( rng::AbstractRNG, dims::Dims) = $randfun(rng, Float64, dims)
$randfun( rng::AbstractRNG, dims::Integer...) = $randfun(rng, Float64, dims...)
$randfun( dims::Dims) = $randfun(GLOBAL_RNG, Float64, dims)
$randfun( dims::Integer...) = $randfun(GLOBAL_RNG, Float64, dims...)
$randfun{T}(rng::AbstractRNG, ::Type{T}, dims::Dims ) = $randfun!(rng, Array{T}(dims))
# Note that this method explicitly does not define $randfun(rng, T), in order to prevent an infinite recursion.
$randfun{T}(rng::AbstractRNG, ::Type{T}, dim1::Integer, dims::Integer...) = $randfun!(rng, Array{T}(dim1, dims...))
$randfun{T}( ::Type{T}, dims::Dims ) = $randfun(GLOBAL_RNG, T, dims)
$randfun{T}( ::Type{T}, dims::Integer... ) = $randfun(GLOBAL_RNG, T, dims...)
$randfun( rng::AbstractRNG, dims::Dims ) = $randfun(rng, Float64, dims)
$randfun( rng::AbstractRNG, dims::Integer... ) = $randfun(rng, Float64, dims...)
$randfun( dims::Dims ) = $randfun(GLOBAL_RNG, Float64, dims)
$randfun( dims::Integer... ) = $randfun(GLOBAL_RNG, Float64, dims...)
end
end
end
Expand Down
17 changes: 17 additions & 0 deletions test/random.jl
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,23 @@ for rng in ([], [MersenneTwister()], [RandomDevice()])
bitrand(rng..., 2, 3) ::BitArray{2}
rand!(rng..., BitArray(5)) ::BitArray{1}
rand!(rng..., BitArray(2, 3)) ::BitArray{2}

# Test that you cannot call randn or randexp with non-Float types.
for r in [randn, randexp, randn!, randexp!]
@test_throws MethodError r(Int)
@test_throws MethodError r(Int32)
@test_throws MethodError r(Bool)
@test_throws MethodError r(String)
@test_throws MethodError r(AbstractFloat)
# TODO(#17627): Consider adding support for randn(BigFloat) and removing this test.
@test_throws MethodError r(BigFloat)

@test_throws MethodError r(Int64, (2,3))
@test_throws MethodError r(String, 1)

@test_throws MethodError r(rng..., Number, (2,3))
@test_throws MethodError r(rng..., Any, 1)
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would add a second loop for concision:

for T in [Int, Int32, String, AbstractFloat, BigFloat, Number, Any]
    @test_throws MethodError r(rng..., T)
    @test_throws MethodError r(rng..., T, 1)
    @test_throws MethodError r(rng..., T, (2, 3))
end

(and, why not, a loop over the dimensions: for d in ([], [1], [(2, 3)])).

end

function hist(X,n)
Expand Down