We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
2 parents ccb4267 + ed63302 commit 8eb2fb6Copy full SHA for 8eb2fb6
4 files changed
src/FixedPointNumbers.jl
@@ -10,7 +10,7 @@ import Base: ==, <, <=, -, +, *, /, ~, isapprox,
10
zero, one, typemin, typemax, realmin, realmax, eps, sizeof, reinterpret,
11
float, trunc, round, floor, ceil, bswap,
12
div, fld, rem, mod, mod1, rem1, fld1, min, max, minmax,
13
- start, next, done, r_promote, reducedim_init
+ start, next, done, r_promote, reducedim_init, rand
14
15
if VERSION <= v"0.5.0-dev+755"
16
macro pure(ex)
@@ -161,4 +161,7 @@ showcompact{T,f}(io::IO, x::FixedPoint{T,f}) = show(io, round(Float64(x), ceil(I
161
throw(ArgumentError("$T is $bitstring type representing $n values from $Tmin to $Tmax; cannot represent $x"))
162
end
163
164
+rand{T<:FixedPoint}(::Type{T}) = reinterpret(T, rand(rawtype(T)))
165
+rand{T<:FixedPoint}(::Type{T}, sz::Dims) = reinterpret(T, rand(rawtype(T), sz))
166
+
167
end # module
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
17
nbitsfrac{T,f}(::Type{Fixed{T,f}}) = f
18
floattype{T<:Fixed}(::Type{T}) = floattype(supertype(T))
test/fixed.jl
@@ -113,3 +113,11 @@ show(iob, x)
113
str = takebuf_string(iob)
114
@test startswith(str, "Fixed{Int32,3}(")
115
@test eval(parse(str)) == x
116
117
+for T in (Fixed{Int8,8}, Fixed{Int16,8}, Fixed{Int16,10}, Fixed{Int32,16})
118
+ a = rand(T)
119
+ @test isa(a, T)
120
+ a = rand(T, (3, 5))
121
+ @test isa(a, Array{T,2})
122
+ @test size(a) == (3,5)
123
+end
test/ufixed.jl
@@ -277,3 +277,14 @@ for T in (Float16, Float32, Float64, BigFloat)
277
y = convert(T, x)
278
@test isa(y, T)
279
280
281
+for T in (UFixed{UInt8,8}, UFixed{UInt8,6},
282
+ UFixed{UInt16,16}, UFixed{UInt16,14},
283
+ UFixed{UInt32,32}, UFixed{UInt32,30},
284
+ UFixed{UInt64,64}, UFixed{UInt64,62})
285
286
287
288
289
290
0 commit comments