Skip to content

Commit a7a416c

Browse files
committed
fix rand_tangent(::BigFloat)
1 parent 460fc3d commit a7a416c

File tree

2 files changed

+4
-5
lines changed

2 files changed

+4
-5
lines changed

src/rand_tangent.jl

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,15 @@ rand_tangent(rng::AbstractRNG, x::Integer) = NoTangent()
1616
# Try and make nice numbers with short decimal representations for good error messages
1717
# while also not biasing the sample space too much
1818
function rand_tangent(rng::AbstractRNG, x::T) where {T<:Number}
19-
return round(8randn(rng, T), sigdigits=6, base=2)
19+
return round(8randn(rng, T), sigdigits=5, base=2)
2020
end
2121
rand_tangent(rng::AbstractRNG, x::Float64) = rand(rng, -9:0.01:9)
2222
function rand_tangent(rng::AbstractRNG, x::ComplexF64)
2323
return ComplexF64(rand(rng, -9:0.1:9), rand(rng, -9:0.1:9))
2424
end
2525

26-
27-
# TODO: right now Julia don't allow `randn(rng, BigFloat)`
28-
# see: https://github.com/JuliaLang/julia/issues/17629
29-
rand_tangent(rng::AbstractRNG, ::BigFloat) = big(rand_tangent(rng, Float64))
26+
#BigFloat/MPFR is finicky about short numbers, this doesn't always work as well as it should
27+
rand_tangent(rng::AbstractRNG, ::BigFloat) = round(big(8randn(rng)), sigdigits=5, base=2)
3028

3129
rand_tangent(rng::AbstractRNG, x::StridedArray) = rand_tangent.(Ref(rng), x)
3230
rand_tangent(rng::AbstractRNG, x::Adjoint) = adjoint(rand_tangent(rng, parent(x)))

test/rand_tangent.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ using FiniteDifferences: rand_tangent
9292
@test length(string(rand_tangent(1.0))) <= 6
9393
@test length(string(rand_tangent(1.0 + 1.0im))) <= 12
9494
@test length(string(rand_tangent(1f0))) <= 12
95+
@test length(string(rand_tangent(big"1.0"))) <= 8
9596
end
9697
end
9798
end

0 commit comments

Comments
 (0)