Skip to content

Commit

Permalink
Add conversion QQBar -> Float64
Browse files Browse the repository at this point in the history
  • Loading branch information
fingolfin committed Oct 15, 2024
1 parent 8fac1af commit f354cc7
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
6 changes: 2 additions & 4 deletions src/calcium/ca.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1363,11 +1363,9 @@ 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
Expand Down
15 changes: 10 additions & 5 deletions src/calcium/qqbar.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1485,16 +1485,21 @@ function ZZRingElem(a::QQBarFieldElem)
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)
z = AcbField(57, cached = false)(x)
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})(x::QQBarFieldElem)
isreal(x) || throw(InexactError(:Float64, Float64, x))
z = AcbField(57, cached = false)(x)
x = real(z)
return Float64(x)
end

###############################################################################
#
# Unsafe functions
Expand Down
6 changes: 6 additions & 0 deletions test/calcium/qqbar-test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,12 @@ end
@test contains(ComplexField()(a), ComplexField()(b))
end
end

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

x = sqrt(R(-2))
@test_throws InexactError Float64(x)
end

function test_elem(R::QQBarField)
Expand Down

0 comments on commit f354cc7

Please sign in to comment.