Skip to content

Commit

Permalink
Implement rand() (#137)
Browse files Browse the repository at this point in the history
* Implement rand()

* Pass RNG arg

* Move comment
  • Loading branch information
jmkuhn authored Jun 9, 2020
1 parent 228e308 commit 10dfa6b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ version = "0.4.10"
[deps]
DecFP_jll = "47200ebd-12ce-5be5-abb7-8e082af23329"
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b"

[compat]
Expand Down
4 changes: 3 additions & 1 deletion src/DecFP.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module DecFP

using DecFP_jll

import Printf, SpecialFunctions
import Printf, Random, SpecialFunctions

export Dec32, Dec64, Dec128, @d_str, @d32_str, @d64_str, @d128_str, exponent10, ldexp10

Expand Down Expand Up @@ -497,6 +497,8 @@ for w in (32,64,128)

@eval SpecialFunctions.gamma(x::$BID) = @xchk(ccall(($(bidsym(w,:tgamma)), libbid), $BID, ($BID,Cuint,Ref{Cuint}), x, roundingmode[Threads.threadid()], RefArray(flags, Threads.threadid())), DomainError, x, mask=INVALID)

@eval Random.rand(r::Random.AbstractRNG, ::Random.SamplerTrivial{Random.CloseOpen01{$BID}}) = $BID(1, rand(r, zero($Ti):$Ti(maxintfloat($BID)) - one($Ti)), -(9 * $w ÷ 32 - 2))

for (r,c) in ((RoundingMode{:Nearest},"round_integral_nearest_even"), (RoundingMode{:NearestTiesAway},"round_integral_nearest_away"), (RoundingMode{:ToZero},"round_integral_zero"), (RoundingMode{:Up},"round_integral_positive"), (RoundingMode{:Down},"round_integral_negative"))
@eval Base.round(x::$BID, ::$r) = @xchk(ccall(($(bidsym(w,c)), libbid), $BID, ($BID,Ref{Cuint}), x, RefArray(flags, Threads.threadid())), DomainError, x, mask=INVALID)
end
Expand Down
31 changes: 30 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
using DecFP, Test, Printf, Base.MathConstants, SpecialFunctions
using DecFP, Test, Printf, Random, Base.MathConstants, SpecialFunctions

@test DecFP.flags[Threads.threadid()] == 0

import DecFP.isnanstr
@test isnanstr("nan") && isnanstr(" +NAN") && isnanstr("-NaN") && !isnanstr("nano")

function hist(X, n)
v = zeros(Int, n)
for x in X
v[floor(Int, x*n) + 1] += 1
end
v
end

function testthreads(T, i, mode)
@test @sprintf("%.0f", T(i)) == string(i)
@test rounding(T) == RoundNearest
Expand Down Expand Up @@ -236,6 +244,27 @@ for T in (Dec32, Dec64, Dec128)
@test gamma(T(x)) gamma(x)
end

Random.seed!(1234)
@test rand(T) != rand(T)
@test 0 <= rand(T) < 1
r = rand(convert(T, 97):convert(T, 122))
@test typeof(r) == T
@test 97 <= r <= 122
r = rand(convert(T, 97):convert(T, 2):convert(T, 122), 2)[1]
@test typeof(r) == T
@test 97 <= r <= 122
@test mod(r, 2)==1
A = Vector{T}(undef, 16)
rand!(A)
@test all(0 .<= A .< 1)
# test uniform distribution
# array version
counts = hist(rand(T, 2000), 4)
@test minimum(counts) > 300 # should fail with proba < 1e-26
# scalar version
counts = hist([rand(T) for i in 1:2000], 4)
@test minimum(counts) > 300

for c in (π, e, γ, catalan, φ)
@test T(c) Float64(c)
end
Expand Down

0 comments on commit 10dfa6b

Please sign in to comment.