Skip to content

Commit 1fd7dfe

Browse files
authored
Merge pull request #9 from putianyi889/version-0.0.2.1
fix #7 and #8
2 parents 744ebae + 508a684 commit 1fd7dfe

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

src/EltypeExtensions.jl

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,12 @@ _to_precisiontype(::Type{T}, ::Type{<:Rational}) where T<:Integer = Rational{T}
116116
_to_precisiontype(::Type{T}, ::Type{S}) where {T,S} = eltype(S) == S ? T : _to_eltype(_to_precisiontype(T, eltype(S)), S)
117117

118118
"""
119-
precisionconvert(T::Type, A, prec=precision(T))
119+
precisionconvert(T::Type, A, prec)
120120
121-
Convert `A` to have the [`precisiontype`](@ref) of `T`. If `T` has adjustable precision such as `BigFloat`, the precision can be specified by `prec`, otherwise `prec` takes no effect.
121+
Convert `A` to have the [`precisiontype`](@ref) of `T`. `prec` is optional.
122+
- When `T` has static precision (e.g. `Float64`), `prec` has no effect.
123+
- When `T` has dynamic precision (e.g. `BigFloat`), `prec` specifies the precision of conversion. When `prec` is not provided, the precision is decided by the external setup from `T`.
124+
- When `T` is an integer, the conversion will dig into `Rational` as well. In contrast, since `Rational` as a whole is more "precise" than an integer, [`precisiontype`](@ref) doesn't unwrap `Rational`.
122125
123126
# Examples
124127
```jldoctest; setup = :(using EltypeExtensions: precisionconvert)
@@ -132,9 +135,9 @@ julia> precisionconvert(Float16, [[m/n for n in 1:3] for m in 1:3])
132135
[3.0, 1.5, 1.0]
133136
```
134137
"""
135-
precisionconvert(T,A) = precisionconvert(T,A,precision(T))
136-
precisionconvert(::Type{T}, A::S, prec) where {T,S} = convert(_to_precisiontype(T,S), A)
137-
precisionconvert(::Type{BigFloat}, A::S) where {S} = convert(_to_precisiontype(BigFloat,S), A) # not ideal. just a workaround
138+
precisionconvert(::Type{T}, A::S) where {T,S} = convert(_to_precisiontype(T,S), A)
139+
precisionconvert(::Type{T}, A::S, prec) where {T,S} = precisionconvert(T, A)
140+
precisionconvert(::Type{BigFloat}, A::S) where {S} = convert(_to_precisiontype(BigFloat,S), A)
138141
precisionconvert(::Type{BigFloat}, x::Real, prec) = bigfloatconvert(x, prec)
139142
precisionconvert(::Type{BigFloat}, x::Complex, prec) = Complex(bigfloatconvert(real(x), prec), bigfloatconvert(imag(x), prec))
140143
precisionconvert(::Type{BigFloat}, A, prec) = precisionconvert.(BigFloat, A, prec)

test/runtests.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,19 @@ using Aqua
77
@testset "bugs" begin
88
@test _to_precisiontype(Float64, Complex) == Complex{Float64}
99
@test precisionconvert(BigFloat, rand(ComplexF64, 3)) isa Vector{Complex{BigFloat}}
10+
11+
@testset "#7" begin
12+
setprecision(256)
13+
f(x) = precisionconvert(BigFloat, x, 256)
14+
g(x) = precisionconvert(BigFloat, x)
15+
setprecision(128)
16+
@test precision(f(π)) == 256 # static precision
17+
@test precision(g(π)) == 128 # precision varies with the global setting
18+
end
19+
20+
@testset "#8" begin
21+
@test precisionconvert(Int128, Int8(1)//Int8(2)) isa Rational{Int128}
22+
end
1023
end
1124

1225
@testset "Doctest" begin

0 commit comments

Comments
 (0)