Skip to content

Commit 9d446dd

Browse files
authored
fix bigint conversion (#15)
1 parent 3de62a8 commit 9d446dd

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

src/Quadmath.jl

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -354,20 +354,18 @@ end
354354

355355
function BigInt(x::Float128)
356356
!isinteger(x) && throw(InexactError(BigInt, x))
357-
357+
BigInt(BigFloat(x, precision=precision(Float128)))
358+
end
359+
function Float128(x::BigInt)
358360
@static if VERSION < v"1.1"
359-
y = setprecision(BigFloat, max(precision, precision(Float128))) do
360-
BigFloat()
361+
y = setprecision(BigFloat, precision(Float128)) do
362+
BigFloat(x)
361363
end
362-
ccall((:mpfr_set, :libmpfr), Int32, (Ref{BigFloat}, Ref{BigFloat}, Int32),
363-
y, x, Base.MPFR.ROUNDING_MODE[])
364364
else
365365
y = BigFloat(x, precision=precision(Float128))
366366
end
367-
368-
return BigInt(y)
367+
Float128(y)
369368
end
370-
Float128(x::BigInt) = Float128(BigFloat(x, precision=precision(Float128)))
371369

372370
promote_rule(::Type{Float128}, ::Type{Float16}) = Float128
373371
promote_rule(::Type{Float128}, ::Type{Float32}) = Float128

test/runtests.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
using Test
22
using Quadmath
33

4-
@testset "conversion $T" for T in (Float64, Int32, Int64, BigFloat)
4+
@testset "conversion $T" for T in (Float64, Int32, Int64, BigFloat, BigInt)
55
@test Float128(T(1)) + Float128(T(2)) == Float128(T(3))
66
@test Float128(T(1)) + Float128(T(2)) <= Float128(T(3))
77
@test Float128(T(1)) + Float128(T(2)) != Float128(T(4))
88
@test Float128(T(1)) + Float128(T(2)) < Float128(T(4))
99
if isbitstype(T)
1010
@test T(Float128(T(1)) + Float128(T(2))) === T(3)
11+
else
12+
@test T(Float128(T(1)) + Float128(T(2))) == T(3)
1113
end
1214
end
1315

0 commit comments

Comments
 (0)