Skip to content

Commit

Permalink
Add conversion CalciumField, QQBar -> Float64 (#1893)
Browse files Browse the repository at this point in the history
Also avoid ccalls in conversion to ComplexF64
  • Loading branch information
fingolfin authored Oct 31, 2024
1 parent 0271fc0 commit 33043ef
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 10 deletions.
12 changes: 8 additions & 4 deletions src/calcium/ca.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1363,15 +1363,19 @@ end

function (::Type{ComplexF64})(x::CalciumFieldElem)
z = AcbField(53, cached = false)(x)
x = ArbFieldElem()
ccall((:acb_get_real, libflint), Nothing, (Ref{ArbFieldElem}, Ref{AcbFieldElem}), x, z)
x = real(z)
xx = Float64(x)
y = ArbFieldElem()
ccall((:acb_get_imag, libflint), Nothing, (Ref{ArbFieldElem}, Ref{AcbFieldElem}), y, z)
y = imag(z)
yy = Float64(y)
return ComplexF64(xx, yy)
end

function (::Type{Float64})(a::CalciumFieldElem)
isreal(a) || throw(InexactError(:Float64, Float64, a))
x = ArbField(53, cached = false)(a)
return Float64(x)
end

###############################################################################
#
# Unsafe functions
Expand Down
16 changes: 10 additions & 6 deletions src/calcium/qqbar.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1476,17 +1476,21 @@ function ZZRingElem(a::QQBarFieldElem)
return z
end

function (::Type{ComplexF64})(x::QQBarFieldElem)
z = AcbField(53, cached = false)(x)
x = ArbFieldElem()
ccall((:acb_get_real, libflint), Nothing, (Ref{ArbFieldElem}, Ref{AcbFieldElem}), x, z)
function (::Type{ComplexF64})(a::QQBarFieldElem)
z = AcbField(53, cached = false)(a)
x = real(z)
xx = Float64(x)
y = ArbFieldElem()
ccall((:acb_get_imag, libflint), Nothing, (Ref{ArbFieldElem}, Ref{AcbFieldElem}), y, z)
y = imag(z)
yy = Float64(y)
return ComplexF64(xx, yy)
end

function (::Type{Float64})(a::QQBarFieldElem)
isreal(a) || throw(InexactError(:Float64, Float64, a))
x = ArbField(53, cached = false)(a)
return Float64(x)
end

###############################################################################
#
# Unsafe functions
Expand Down
8 changes: 8 additions & 0 deletions test/calcium/ca-test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,14 @@ end
s = 1 * one(C) + 2 * onei(C)
@test ComplexF64(s) == 1 + 2 * im

x = sqrt(C(2))
@test isapprox(Float64(x), sqrt(2))
@test isapprox(ComplexF64(x), sqrt(2))

x = sqrt(C(-2))
@test_throws InexactError Float64(x)
@test isapprox(ComplexF64(x), sqrt(complex(-2)))

# verify precision bug https://github.com/Nemocas/Nemo.jl/issues/1580 is fixed
x = C(pi)
F = ArbField(333)
Expand Down
8 changes: 8 additions & 0 deletions test/calcium/qqbar-test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,14 @@ end
@test contains(ComplexField()(a), ComplexField()(b))
end
end

x = sqrt(R(2))
@test isapprox(Float64(x), sqrt(2))
@test isapprox(ComplexF64(x), sqrt(2))

x = sqrt(R(-2))
@test_throws InexactError Float64(x)
@test isapprox(ComplexF64(x),sqrt(complex(-2)))
end

function test_elem(R::QQBarField)
Expand Down

0 comments on commit 33043ef

Please sign in to comment.