Skip to content

Commit 9f32884

Browse files
authored
Merge pull request #621 from JuliaHomotopyContinuation/conversion_bug
Fix the bug in convert(type, Expression) from #615
2 parents 1638f9c + f89da5a commit 9f32884

File tree

2 files changed

+44
-10
lines changed

2 files changed

+44
-10
lines changed

src/model_kit/symengine.jl

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -701,19 +701,29 @@ function Base.convert(::Type{BigInt}, c::Basic)
701701
end
702702

703703
function Base.convert(::Type{BigFloat}, c::Basic)
704-
a = BigFloat()
705-
ccall(
706-
(:real_mpfr_get, libsymengine),
707-
Nothing,
708-
(Ref{BigFloat}, Ref{ExpressionRef}),
709-
a,
710-
c,
711-
)
712-
return a
704+
if class(c) == :Integer
705+
c1 = convert(Int, c)
706+
return convert(BigFloat, c1)
707+
else
708+
a = BigFloat()
709+
ccall(
710+
(:real_mpfr_get, libsymengine),
711+
Nothing,
712+
(Ref{BigFloat}, Ref{ExpressionRef}),
713+
a,
714+
c,
715+
)
716+
return a
717+
end
713718
end
714719

715720
function Base.convert(::Type{Float64}, c::Basic)
716-
return ccall((:real_double_get_d, libsymengine), Cdouble, (Ref{ExpressionRef},), c)
721+
if class(c) == :Integer
722+
c1 = convert(Int, c)
723+
return convert(Float64, c1)
724+
else
725+
return ccall((:real_double_get_d, libsymengine), Cdouble, (Ref{ExpressionRef},), c)
726+
end
717727
end
718728

719729
function Base.real(x::Basic)

test/model_kit/symbolic_test.jl

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,4 +329,28 @@
329329
H_any = Homotopy(convert(Vector{Any}, h), [x, y, z], t)
330330
@test H_any isa Homotopy
331331
end
332+
333+
@testset "Convert" begin
334+
335+
@var x
336+
337+
s = x + 1 - x
338+
@test convert(Int, s) == 1
339+
@test convert(Int32, s) == Int32(1)
340+
@test convert(Int64, s) == Int64(1)
341+
@test convert(BigInt, s) == BigInt(1)
342+
@test convert(BigFloat, s) == BigFloat(1)
343+
@test convert(Float64, s) == Float64(1)
344+
@test convert(ComplexF64, s) == ComplexF64(1)
345+
346+
r = x + 1.0 - x
347+
@test convert(Float64, r) == Float64(1)
348+
@test convert(ComplexF64, r) == ComplexF64(1)
349+
350+
u = x + BigFloat(1.0) - x
351+
@test convert(BigFloat, u) == BigFloat(1.0)
352+
353+
t = x + (1.0 + 0.0im) - x
354+
@test convert(ComplexF64, t) == ComplexF64(1)
355+
end
332356
end

0 commit comments

Comments
 (0)