From 6100f3e9476816c06f54ffff0029e3badbb4f14e Mon Sep 17 00:00:00 2001 From: Max Horn Date: Sun, 13 Oct 2024 01:22:45 +0200 Subject: [PATCH] Add conversion CalciumField, QQBar -> Float64 Also avoid ccalls in conversion to ComplexF64 --- src/calcium/ca.jl | 12 ++++++++---- src/calcium/qqbar.jl | 16 ++++++++++------ test/calcium/ca-test.jl | 8 ++++++++ test/calcium/qqbar-test.jl | 8 ++++++++ 4 files changed, 34 insertions(+), 10 deletions(-) diff --git a/src/calcium/ca.jl b/src/calcium/ca.jl index 9b6b4af33..30dc50c25 100644 --- a/src/calcium/ca.jl +++ b/src/calcium/ca.jl @@ -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 diff --git a/src/calcium/qqbar.jl b/src/calcium/qqbar.jl index eba280ce0..ad25225f7 100644 --- a/src/calcium/qqbar.jl +++ b/src/calcium/qqbar.jl @@ -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 diff --git a/test/calcium/ca-test.jl b/test/calcium/ca-test.jl index f82e12fcf..62ffc7549 100644 --- a/test/calcium/ca-test.jl +++ b/test/calcium/ca-test.jl @@ -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) diff --git a/test/calcium/qqbar-test.jl b/test/calcium/qqbar-test.jl index e094b624d..398959652 100644 --- a/test/calcium/qqbar-test.jl +++ b/test/calcium/qqbar-test.jl @@ -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)