Skip to content

Commit

Permalink
Add some convert method to/from QQBarFieldElem
Browse files Browse the repository at this point in the history
  • Loading branch information
fingolfin committed Dec 5, 2024
1 parent 578b513 commit 92d86ce
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/calcium/qqbar.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1414,6 +1414,17 @@ function (::Type{Float64})(a::QQBarFieldElem)
return Float64(x)
end

function (::Type{T})(a::QQBarFieldElem) where {T <: Integer}
return T(ZZRingElem(a))
end

function (::Type{T})(a::QQBarFieldElem) where {T <: Rational}
return T(QQFieldElem(a))
end

convert(::Type{QQBarFieldElem}, a::RationalUnion) = QQBarFieldElem(a)
convert(::Type{T}, a::QQBarFieldElem) where {T <: RationalUnion} = T(a)

###############################################################################
#
# Unsafe functions
Expand Down
31 changes: 31 additions & 0 deletions test/calcium/qqbar-test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -418,4 +418,35 @@ end
x = sqrt(R(-2))
@test_throws InexactError Float64(x)
@test isapprox(ComplexF64(x),sqrt(complex(-2)))


x = R(2)
for T in [Int16, UInt32, Int, BigInt, Rational{Int}, Rational{BigInt}, ZZRingElem, QQFieldElem]
y = convert(T, x)
@test y::T == 2

y = T(x)
@test y::T == 2

z = convert(QQBarFieldElem, y)
@test z::QQBarFieldElem == x

z = QQBarFieldElem(y)
@test z::QQBarFieldElem == x
end

x = R(2//3)
for T in [Rational{Int}, Rational{BigInt}, QQFieldElem]
y = convert(T, x)
@test y::T == 2//3

y = T(x)
@test y::T == 2//3

z = convert(QQBarFieldElem, y)
@test z::QQBarFieldElem == x

z = QQBarFieldElem(y)
@test z::QQBarFieldElem == x
end
end

0 comments on commit 92d86ce

Please sign in to comment.