diff --git a/src/FixedPointNumbers.jl b/src/FixedPointNumbers.jl index 758ad833..cf5432cf 100644 --- a/src/FixedPointNumbers.jl +++ b/src/FixedPointNumbers.jl @@ -10,7 +10,7 @@ import Base: ==, <, <=, -, +, *, /, ~, isapprox, zero, one, typemin, typemax, realmin, realmax, eps, sizeof, reinterpret, float, trunc, round, floor, ceil, bswap, div, fld, rem, mod, mod1, rem1, fld1, min, max, minmax, - start, next, done, r_promote, reducedim_init + start, next, done, r_promote, reducedim_init, rand if VERSION <= v"0.5.0-dev+755" macro pure(ex) @@ -161,4 +161,7 @@ showcompact{T,f}(io::IO, x::FixedPoint{T,f}) = show(io, round(Float64(x), ceil(I throw(ArgumentError("$T is $bitstring type representing $n values from $Tmin to $Tmax; cannot represent $x")) end +rand{T<:FixedPoint}(::Type{T}) = reinterpret(T, rand(rawtype(T))) +rand{T<:FixedPoint}(::Type{T}, sz::Dims) = reinterpret(T, rand(rawtype(T), sz)) + end # module diff --git a/src/fixed.jl b/src/fixed.jl index 3ec39398..fb7a4980 100644 --- a/src/fixed.jl +++ b/src/fixed.jl @@ -11,6 +11,8 @@ end typealias Fixed16 Fixed{Int32, 16} +reinterpret{T<:Signed, f}(::Type{Fixed{T,f}}, x::T) = Fixed{T,f}(x, 0) + rawtype{T,f}(::Type{Fixed{T,f}}) = T nbitsfrac{T,f}(::Type{Fixed{T,f}}) = f floattype{T<:Fixed}(::Type{T}) = floattype(supertype(T)) diff --git a/test/fixed.jl b/test/fixed.jl index ac2b277e..f3e4d440 100644 --- a/test/fixed.jl +++ b/test/fixed.jl @@ -113,3 +113,11 @@ show(iob, x) str = takebuf_string(iob) @test startswith(str, "Fixed{Int32,3}(") @test eval(parse(str)) == x + +for T in (Fixed{Int8,8}, Fixed{Int16,8}, Fixed{Int16,10}, Fixed{Int32,16}) + a = rand(T) + @test isa(a, T) + a = rand(T, (3, 5)) + @test isa(a, Array{T,2}) + @test size(a) == (3,5) +end diff --git a/test/ufixed.jl b/test/ufixed.jl index b2211911..6c2a3aba 100644 --- a/test/ufixed.jl +++ b/test/ufixed.jl @@ -277,3 +277,14 @@ for T in (Float16, Float32, Float64, BigFloat) y = convert(T, x) @test isa(y, T) end + +for T in (UFixed{UInt8,8}, UFixed{UInt8,6}, + UFixed{UInt16,16}, UFixed{UInt16,14}, + UFixed{UInt32,32}, UFixed{UInt32,30}, + UFixed{UInt64,64}, UFixed{UInt64,62}) + a = rand(T) + @test isa(a, T) + a = rand(T, (3, 5)) + @test isa(a, Array{T,2}) + @test size(a) == (3,5) +end