Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add conversion CalciumField, QQBar -> Float64 #1893

Merged
merged 1 commit into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
fingolfin marked this conversation as resolved.
Show resolved Hide resolved
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
Loading