Skip to content

Commit eb964f6

Browse files
authored
add AbstractFloat conversion fallback (#140)
1 parent 96e2e23 commit eb964f6

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

src/DecFP.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -753,6 +753,10 @@ Base.convert(::Type{F}, x::Float16) where {F<:DecimalFloatingPoint} = F(Float32(
753753
promote_rule(::Type{F}, ::Type{Float16}) where {F<:DecimalFloatingPoint} = F
754754
promote_rule(::Type{F}, ::Type{T}) where {F<:DecimalFloatingPoint,T<:Union{Int8,UInt8,Int16,UInt16,Int32,UInt32,Int64,UInt64,Int128,UInt128}} = F
755755

756+
# fallback floating-point conversion via strings
757+
Base.convert(::Type{F}, x::AbstractFloat) where {F<:DecimalFloatingPoint} = F(string(x))
758+
Base.convert(::Type{F}, x::F) where {F<:DecimalFloatingPoint} = x # don't call AbstractFloat method!
759+
756760
# so that mathconsts get promoted to Dec32, not Dec64, like Float32
757761
promote_rule(::Type{Irrational{s}}, ::Type{F}) where {s,F<:DecimalFloatingPoint} = F
758762
promote_rule(::Type{Irrational{s}}, T::Type{Complex{F}}) where {s,F<:DecimalFloatingPoint} = T

test/runtests.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,3 +488,6 @@ end
488488
# issue #93
489489
@test parse(Dec64, "3935767060.093896713") == d64"3.935767060093897e9" ==
490490
Dec64(d128"3935767060.093896713")
491+
492+
# issue #139
493+
@test convert(Dec64, big"2.5") == d"2.5"

0 commit comments

Comments
 (0)